Tag:Complexity

  • [leetcode] 136. A number that appears only once

    Time:2021-6-18

    Given a non empty integer array, each element appears twice except one. Find the element that appears only once. explain: Your algorithm should have linear time complexity. Can you do it without extra space? Example 1: Input: [2,2,1] Output: 1 Example 2: Input: [4,1,2,1,2] Output: 4 How to solve the problem:According to the meaning of […]

  • Everything you ask about a HashMap interview

    Time:2021-6-17

    preface HashMap is a common data structure in both work and interview. For example, the optimal solution of leetcode’s first question two sum variant needs HashMap, and the high-frequency examination question LRU cache needs LinkedHashMap. HashMap is very simple to use, and the underlying implementation is not complicated. Let’s take a look at some common […]

  • Introduction to redis data structure part 2 – table skipping

    Time:2021-6-14

    This article uses the “signature 4.0 International (CC by 4.0)” license agreement. You are welcome to reprint or modify it, but you need to indicate the source.Signature 4.0 International (CC by 4.0) Author: nicksxs Created on: January 4, 2020 Link to this article:Introduction to redis data structure part 2 – table skipping Skip list Jump […]

  • Leetcode-0108 transforms an ordered array into a binary search tree

    Time:2021-6-12

    Title addresshttps://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/ Middle order traversal (DFS) First of all, we need to know that the results of a binary search tree are in the order from small to large. According to the array given by the topic, we can naturally associate with the middle order traversal.For traversing DFs in medium order, we can consider the […]

  • The maximum depth of leetcode-0104 binary tree

    Time:2021-6-11

    Title addresshttps://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ 1. Recursion Time complexity O (n) space complexity O (H), space complexity is mainly used for recursive stack depth HIt’s very simple to use recursion to solve the problem locally. The first condition of recursion termination is recursion to the current node is null. var maxDepth = function(root) { if (root === null) […]

  • The diameter of leetcode-0543 binary tree

    Time:2021-6-9

    Title addresshttps://leetcode-cn.com/problems/diameter-of-binary-tree/ Recursion + BFS (violent solution) We can calculate the maximum heights of the left and right subtrees of each node. This will involve a lot of repeated calculations. Time complexity O (n ^ 2) space complexity O (n) class Solution { public int diameterOfBinaryTree(TreeNode root) { int result = 0; Queue<TreeNode> queue = […]

  • Leetcode-0101 symmetric binary tree

    Time:2021-6-8

    Title addresshttps://leetcode-cn.com/problems/symmetric-tree/ 1. Recursion As like as two peas, the two most identical trees are mirrored. The relationship between them is satisfiednode1.left == node2.rightAndnode1.right == node2.leftThe time complexity O (n) n is the number of nodes; The space complexity O (H) H is the maximum depth of binary tree class Solution { public boolean isSymmetric(TreeNode […]

  • Binary search tree

    Time:2021-6-7

    1、 Introduction Binary search tree is a dynamic data structure, which has a unique root node. Each node has two word nodes at most and one parent node at most; The value of each node in binary search tree is greater than that of its left subtree and less than that of its right subtree; […]

  • The largest rectangle in a histogram (monotone stack)

    Time:2021-5-31

    84. The largest rectangle in the histogram violence thinking Enumerate all the left and right boundaries, and then find the minimum value in the interval. Complexity O (n ^ 3) It takes too long, so the code is omitted Violence optimization Select a column as the height, and then spread left and right to ensure […]

  • Leetcode 136. A number that appears only once | python

    Time:2021-5-29

    136. A number that appears only once Title Source: https://leetcode-cn.com/problems/single-number subject Given a non empty integer array, each element appears twice except one. Find the element that appears only once. explain: Your algorithm should have linear time complexity. Can you do it without extra space? Example 1: Input: [2,2,1] Output: 1 Example 2: Input: [4,1,2,1,2] […]

  • Data structure questions in front end interview

    Time:2021-5-13

    1、 Single linked list flipping (leetcode 206) 1. Iterative method var reverseList = function(head) { if(!head || !head.next){ return head; }else{ //Initialize prev and curr first var prev = null; var curr = head; //Continuously point the next pointer of the latter node to the precursor node until the node is null, which means the […]

  • Prime pair conjecture

    Time:2021-5-13

    1、 Title: Topic link   Let’s define DnIt is: Dn​​=p​n+1​​−p​nWhere piIs the i-th prime. Obviously there is diFor n > 1, D ﹣ n ﹣ is even“ The prime pair conjecture holds that there are infinite pairs of adjacent prime numbers with difference of 2. Now any positive integer n (< 10) is given5), please calculate […]