Tag：Complexity

Time：2021618
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 […]

Time：2021617
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 highfrequency 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 […]

Time：2021614
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 […]

Time：2021612
Title addresshttps://leetcodecn.com/problems/convertsortedarraytobinarysearchtree/ 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 […]

Time：2021611
Title addresshttps://leetcodecn.com/problems/maximumdepthofbinarytree/ 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) […]

Time：202169
Title addresshttps://leetcodecn.com/problems/diameterofbinarytree/ 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 = […]

Time：202168
Title addresshttps://leetcodecn.com/problems/symmetrictree/ 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 […]

Time：202167
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; […]

Time：2021531
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 […]

Time：2021529
136. A number that appears only once Title Source: https://leetcodecn.com/problems/singlenumber 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] […]

Time：2021513
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 […]

Time：2021513
1、 Title: Topic link Let’s define DnIt is: Dn=pn+1−pnWhere piIs the ith 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 […]