Linkage
https://leetcode.com/problems/longest-consecutive-sequence/description/
Code
1 | class Solution { |
Why don't you come to your senses?
https://leetcode.com/problems/longest-consecutive-sequence/description/
1 | class Solution { |
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
This is a cliche question, we can use recursion to solve it, each time we will check the left child and right child, if each ancestor is not null, the current node must be the LCA, else return the non-null node.
1 | /** |
https://leetcode.com/problems/spiral-matrix/description/
Easy to implement the alogithm, but pay attention to the bounds.
1 | class Solution { |
https://leetcode.com/problems/spiral-matrix-ii/description/
In this problem, we found the fact that the direction change is regular, namely, it follows right, down, left, up, right, down, left, up. So we can simplify the code from switch case to simple loop.This polishment also add the speed of calculation compared to the switch way.
1 | class Solution { |
https://leetcode.com/problems/set-matrix-zeroes/description/
A traverse of will help us to mark which column or row should be marked as all zero. In this implementation, we will use O(m+n) space.
1 | class Solution { |
Recently, I learned the shared_pointer in C++11, but things is much complicated then I expected before, in this article, I will go through the an “easy” implementation of binary seach tree, then I will throw out a new concept- weak pointer.
First, let me implement a tree node using shared pointer
1 | #include <iostream>x |
You can see the nodes are deleted safely.
1 | // |
Result:
1 | Contructor |
You can see through output that the three node is not properly deallocated;
Let me take a look at what happens during the process. When we reach the end of main function, we will delete the local variable, namely, ptr
, but when we check the reference count of the ptr
, it is 2,(The reason is that the current ptr is being pointed by the Node(2) and Node(5), so the deconstructor of the Node will not be implemented. This is how things work.
All we need to do is to hand set the type pf parent_ptr as weak_ptr
1 | #include <iostream> |
https://leetcode.com/problems/word-ladder-ii/description/
The idea of this problem is to combine the BFS(forward) and the DFS(backward) to traverse the visited node. To be more specific, when we are searching the endWord, we should use BFS, which can trim a lot of branch compared to DFS.For reforming the ladder in backward from endWord, we need to maintain a reverse adjacent table, which will record the parent nodes of a child nodes in the BFS process.
1 | public class Solution { |
1 | class Solution { |
The example is showing below.
1 | #include <iostream> |
https://leetcode.com/problems/word-ladder/description/
In the naive implementation, we will consider all words as the node in graph, then using BFS to record the shortest depth. Once we visit endWord, then we return the current depth.
1 | import java.util.Queue; |
In the naive implementation. each time to find the next word on the chain ,we need to traverse all the node in the all graph, which is time-consuming(The test cases improve this). The wiser way is to dectect new node by substitute each letter in the current word,(The consumed time is word_length*26)
1 | import java.util.Queue; |
https://leetcode.com/problems/sort-colors/description/
0……0 1……1 x1 x2 …. xm 2…..2
| | |
left i right
1 | class Solution { |