├── 0020-Valid-Parentheses ├── java-0020 │ └── src │ │ ├── Main.java │ │ └── Solution.java └── cpp-0020 │ ├── CMakeLists.txt │ └── main.cpp ├── 0001-Two-Sum ├── cpp-0001 │ ├── CMakeLists.txt │ ├── main.cpp │ ├── main3.cpp │ └── main2.cpp └── java-0001 │ └── src │ ├── Solution1.java │ ├── Solution3.java │ └── Solution2.java ├── 0094-Binary-Tree-Inorder-Traversal ├── java-0094 │ └── src │ │ ├── TreeNode.java │ │ ├── Solution1.java │ │ ├── Solution3.java │ │ ├── Solution4.java │ │ ├── Solution5.java │ │ └── Solution2.java └── cpp-0094 │ ├── CMakeLists.txt │ ├── main.cpp │ ├── main3.cpp │ ├── main4.cpp │ ├── main5.cpp │ └── main2.cpp ├── 0102-Binary-Tree-Level-Order-Traversal ├── java-0102 │ └── src │ │ ├── TreeNode.java │ │ ├── Solution.java │ │ └── Solution2.java └── cpp-0102 │ ├── CMakeLists.txt │ ├── main.cpp │ └── main2.cpp ├── 0144-Binary-Tree-Preorder-Traversal ├── java-0144 │ └── src │ │ ├── TreeNode.java │ │ ├── Solution1.java │ │ ├── Solution4.java │ │ ├── Solution3.java │ │ ├── Solution5.java │ │ ├── Solution6.java │ │ └── Solution2.java └── cpp-0144 │ ├── CMakeLists.txt │ ├── main.cpp │ ├── main3.cpp │ ├── main6.cpp │ ├── main4.cpp │ ├── main5.cpp │ └── main2.cpp ├── 0145-Binary-Tree-Postorder-Traversal ├── java-0145 │ └── src │ │ ├── TreeNode.java │ │ ├── Solution1.java │ │ ├── Solution4.java │ │ ├── Solution5.java │ │ ├── Solution7.java │ │ ├── Solution8.java │ │ ├── Solution6.java │ │ ├── Solution2.java │ │ ├── Solution3.java │ │ └── Solution9.java └── cpp-0145 │ ├── CMakeLists.txt │ ├── main.cpp │ ├── main4.cpp │ ├── main5.cpp │ ├── main2.cpp │ ├── main3.cpp │ ├── main7.cpp │ ├── main8.cpp │ ├── main6.cpp │ └── main9.cpp ├── 0454-4Sum-II ├── cpp-0454 │ ├── CMakeLists.txt │ ├── main.cpp │ └── main2.cpp └── java-0454 │ └── src │ ├── Solution1.java │ └── Solution2.java ├── 0075-Sort-Colors ├── cpp-0075 │ ├── CMakeLists.txt │ ├── main.cpp │ └── main2.cpp └── java-0075 │ └── src │ ├── Solution1.java │ └── Solution2.java ├── 0086-Partition-List └── cpp-0086 │ ├── CMakeLists.txt │ └── main.cpp ├── 0283-Move-Zeroes ├── cpp-0283 │ ├── CMakeLists.txt │ ├── main3.cpp │ ├── main2.cpp │ ├── main4.cpp │ └── main.cpp └── java-0283 │ └── src │ ├── Solution2.java │ ├── Solution3.java │ ├── Solution4.java │ └── Solution1.java ├── 0344-Reverse-String └── cpp-0344 │ ├── CMakeLists.txt │ └── main.cpp ├── 0002-Add-Two-Numbers └── cpp-0002 │ ├── CMakeLists.txt │ ├── main.cpp │ ├── main2.cpp │ └── main3.cpp ├── 0024-Swap-Nodes-in-Pairs ├── cpp-0024 │ ├── CMakeLists.txt │ └── main.cpp └── java-0024 │ └── src │ ├── ListNode.java │ └── Solution.java ├── 0279-Perfect-Squares ├── cpp-0279 │ ├── CMakeLists.txt │ ├── main3.cpp │ ├── main2.cpp │ └── main.cpp └── java-0279 │ └── src │ ├── Solution3.java │ ├── Solution2.java │ └── Solution1.java ├── 0092-Reverse-Linked-List-II └── cpp-0092 │ ├── CMakeLists.txt │ └── main.cpp ├── 0219-Contains-Duplicate-II ├── cpp-0219 │ ├── CMakeLists.txt │ └── main.cpp └── java-0219 │ └── src │ └── Solution.java ├── 0328-Odd-Even-Linked-List └── cpp-0328 │ ├── CMakeLists.txt │ ├── main2.cpp │ └── main.cpp ├── 0447-Number-of-Boomerangs ├── cpp-0447 │ ├── CMakeLists.txt │ └── main.cpp └── java-0447 │ └── src │ └── Solution.java ├── 0203-Remove-Linked-List-Elements ├── cpp-0203 │ ├── CMakeLists.txt │ ├── main2.cpp │ └── main.cpp └── java-0203 │ └── src │ ├── Solution2.java │ ├── ListNode.java │ └── Solution.java ├── 0237-Delete-Node-in-a-Linked-List ├── cpp-0237 │ ├── CMakeLists.txt │ └── main.cpp └── java-0237 │ └── src │ ├── Solution.java │ └── ListNode.java ├── 0349-Intersection-of-Two-Arrays ├── cpp-0349 │ ├── CMakeLists.txt │ └── main.cpp └── java-0349 │ └── src │ └── Solution.java ├── 0350-Intersection-of-Two-Arrays-II ├── cpp-0350 │ ├── CMakeLists.txt │ ├── main2.cpp │ └── main.cpp └── java-0350 │ └── src │ └── Solution.java ├── 0019-Remove-Nth-Node-From-End-of-List ├── cpp-0019 │ ├── CMakeLists.txt │ ├── main2.cpp │ └── main.cpp └── java-0019 │ └── src │ ├── Solution1.java │ ├── Solution2.java │ └── ListNode.java ├── 0150-Evaluate-Reverse-Polish-Notation └── cpp-0150 │ ├── CMakeLists.txt │ └── main.cpp ├── 0167-Two-Sum-II-Input-array-is-sorted ├── cpp-0167 │ ├── CMakeLists.txt │ ├── main.cpp │ ├── main3.cpp │ └── main2.cpp └── java-0167 │ └── src │ ├── Solution1.java │ ├── Solution3.java │ └── Solution2.java ├── 0026-Remove-Duplicates-from-Sorted-Array └── cpp-0026 │ ├── CMakeLists.txt │ └── main.cpp ├── Readme.md └── README-En.md /0020-Valid-Parentheses/java-0020/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | // write your code here 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /0001-Two-Sum/cpp-0001/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0001) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main2.cpp) 7 | add_executable(cpp_0001 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/java-0094/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | // Definition for a binary tree node. 2 | public class TreeNode { 3 | int val; 4 | TreeNode left; 5 | TreeNode right; 6 | TreeNode(int x) { val = x; } 7 | } -------------------------------------------------------------------------------- /0102-Binary-Tree-Level-Order-Traversal/java-0102/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | // Definition for a binary tree node. 2 | public class TreeNode { 3 | int val; 4 | TreeNode left; 5 | TreeNode right; 6 | TreeNode(int x) { val = x; } 7 | } -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/java-0144/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | // Definition for a binary tree node. 2 | public class TreeNode { 3 | int val; 4 | TreeNode left; 5 | TreeNode right; 6 | TreeNode(int x) { val = x; } 7 | } -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/TreeNode.java: -------------------------------------------------------------------------------- 1 | // Definition for a binary tree node. 2 | public class TreeNode { 3 | int val; 4 | TreeNode left; 5 | TreeNode right; 6 | TreeNode(int x) { val = x; } 7 | } -------------------------------------------------------------------------------- /0454-4Sum-II/cpp-0454/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0454) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0454 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0075-Sort-Colors/cpp-0075/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0075) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main2.cpp) 7 | add_executable(cpp_0075 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0086-Partition-List/cpp-0086/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0086) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0086 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0283-Move-Zeroes/cpp-0283/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0283) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0283 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0344-Reverse-String/cpp-0344/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0344) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0344 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0002-Add-Two-Numbers/cpp-0002/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0002) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main2.cpp) 7 | add_executable(cpp_0002 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0020-Valid-Parentheses/cpp-0020/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0020) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0020 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0024-Swap-Nodes-in-Pairs/cpp-0024/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0024) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0024 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0279-Perfect-Squares/cpp-0279/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0279) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main3.cpp) 7 | add_executable(cpp_0279 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0092-Reverse-Linked-List-II/cpp-0092/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0092) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0092 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0219-Contains-Duplicate-II/cpp-0219/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0219) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0219 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0328-Odd-Even-Linked-List/cpp-0328/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0328) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main2.cpp) 7 | add_executable(cpp_0328 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0447-Number-of-Boomerangs/cpp-0447/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0447) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0447 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0203-Remove-Linked-List-Elements/cpp-0203/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0203) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main2.cpp) 7 | add_executable(cpp_0203 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0237-Delete-Node-in-a-Linked-List/cpp-0237/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0237) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0237 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0349-Intersection-of-Two-Arrays/cpp-0349/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0349) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0349 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0350-Intersection-of-Two-Arrays-II/cpp-0350/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0350) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0350 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0019-Remove-Nth-Node-From-End-of-List/cpp-0019/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0019) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0019 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/cpp-0094/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0094) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main3.cpp) 7 | add_executable(cpp_0094 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0102-Binary-Tree-Level-Order-Traversal/cpp-0102/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0102) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main2.cpp) 7 | add_executable(cpp_0102 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/cpp-0144/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0144) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main5.cpp) 7 | add_executable(cpp_0144 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0145) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main8.cpp) 7 | add_executable(cpp_0145 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0150-Evaluate-Reverse-Polish-Notation/cpp-0150/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_0150) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_0150 ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(04_Two_Sum_II) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main3.cpp) 7 | add_executable(04_Two_Sum_II ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(cpp_Remove_Duplicates_from_Sorted_Array) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_Remove_Duplicates_from_Sorted_Array ${SOURCE_FILES}) -------------------------------------------------------------------------------- /0203-Remove-Linked-List-Elements/java-0203/src/Solution2.java: -------------------------------------------------------------------------------- 1 | public class Solution2 { 2 | 3 | public ListNode removeElements(ListNode head, int val) { 4 | 5 | if(head == null) 6 | return head; 7 | 8 | ListNode node = removeElements(head.next, val); 9 | head.next = node; 10 | return head.val == val ? node : head; 11 | } 12 | 13 | public static void main(String[] args) { 14 | 15 | int[] arr = {1, 2, 6, 3, 4, 5, 6}; 16 | int val = 6; 17 | 18 | ListNode head = new ListNode(arr); 19 | System.out.println(head); 20 | 21 | (new Solution()).removeElements(head, val); 22 | System.out.println(head); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0344-Reverse-String/cpp-0344/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/reverse-string/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-06-04 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | // 344. Reverse String 10 | // https://leetcode.com/problems/reverse-string/description/ 11 | // Two Pointers 12 | // 时间复杂度: O(n) 13 | // 空间复杂度: O(1) 14 | class Solution { 15 | public: 16 | string reverseString(string s) { 17 | 18 | int i = 0, j = s.size() - 1; 19 | while(i < j){ 20 | swap(s[i], s[j]); 21 | i ++; 22 | j --; 23 | } 24 | 25 | return s; 26 | } 27 | }; 28 | 29 | 30 | int main() { 31 | 32 | cout << Solution().reverseString("hello") << endl; 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /0279-Perfect-Squares/cpp-0279/main3.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/perfect-squares/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | /// Dynamic Programming 11 | /// Time Complexity: O(n) 12 | /// Space Complexity: O(n) 13 | class Solution { 14 | 15 | public: 16 | int numSquares(int n) { 17 | 18 | vector mem(n + 1, INT_MAX); 19 | mem[0] = 0; 20 | for(int i = 1; i <= n ; i ++) 21 | for(int j = 1 ; i - j * j >= 0 ; j ++) 22 | mem[i] = min(mem[i], 1 + mem[i - j * j]); 23 | 24 | return mem[n]; 25 | } 26 | }; 27 | 28 | int main() { 29 | 30 | cout << Solution().numSquares(12) << endl; 31 | cout << Solution().numSquares(13) << endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /0279-Perfect-Squares/java-0279/src/Solution3.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/perfect-squares/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.Arrays; 6 | 7 | /// Dynamic Programming 8 | /// Time Complexity: O(n) 9 | /// Space Complexity: O(n) 10 | public class Solution3 { 11 | 12 | public int numSquares(int n) { 13 | 14 | int[] mem = new int[n+1]; 15 | Arrays.fill(mem, Integer.MAX_VALUE); 16 | mem[0] = 0; 17 | for(int i = 1; i <= n ; i ++) 18 | for(int j = 1 ; i - j * j >= 0 ; j ++) 19 | mem[i] = Math.min(mem[i], 1 + mem[i - j * j]); 20 | 21 | return mem[n]; 22 | } 23 | 24 | public static void main(String[] args) { 25 | 26 | System.out.println((new Solution3()).numSquares(12)); 27 | System.out.println((new Solution3()).numSquares(13)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | // Recursive 9 | // Time Complexity: O(n), n is the node number in the tree 10 | // Space Complexity: O(h), h is the height of the tree 11 | public class Solution1 { 12 | 13 | public List inorderTraversal(TreeNode root) { 14 | 15 | ArrayList res = new ArrayList(); 16 | inorderTraversal(root, res); 17 | return res; 18 | } 19 | 20 | private void inorderTraversal(TreeNode node, List list){ 21 | if(node != null){ 22 | inorderTraversal(node.left, list); 23 | list.add(node.val); 24 | inorderTraversal(node.right, list); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0237-Delete-Node-in-a-Linked-List/java-0237/src/Solution.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/delete-node-in-a-linked-list/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | /// 5 | /// 时间复杂度: O(1) 6 | /// 空间复杂度: O(1) 7 | public class Solution { 8 | 9 | public void deleteNode(ListNode node) { 10 | 11 | if(node == null || node.next == null) 12 | throw new IllegalArgumentException("node should be valid and can not be the tail node."); 13 | 14 | node.val = node.next.val; 15 | node.next = node.next.next; 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | int[] arr = {1, 2, 3, 4}; 21 | 22 | ListNode head = new ListNode(arr); 23 | System.out.println(head); 24 | 25 | ListNode node2 = head.findNode(2); 26 | (new Solution()).deleteNode(node2); 27 | System.out.println(head); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | // Recursive 8 | // Time Complexity: O(n), n is the node number in the tree 9 | // Space Complexity: O(h), h is the height of the tree 10 | public class Solution1 { 11 | 12 | public List preorderTraversal(TreeNode root) { 13 | 14 | ArrayList res = new ArrayList(); 15 | preorderTraversal(root, res); 16 | return res; 17 | } 18 | 19 | private void preorderTraversal(TreeNode node, List list){ 20 | if(node != null){ 21 | list.add(node.val); 22 | preorderTraversal(node.left, list); 23 | preorderTraversal(node.right, list); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | // Recursive 9 | // Time Complexity: O(n), n is the node number in the tree 10 | // Space Complexity: O(h), h is the height of the tree 11 | public class Solution1 { 12 | 13 | public List postorderTraversal(TreeNode root) { 14 | 15 | ArrayList res = new ArrayList(); 16 | postorderTraversal(root, res); 17 | return res; 18 | } 19 | 20 | private void postorderTraversal(TreeNode node, List list){ 21 | if(node != null){ 22 | postorderTraversal(node.left, list); 23 | postorderTraversal(node.right, list); 24 | list.add(node.val); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0283-Move-Zeroes/cpp-0283/main3.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/move-zeroes/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-02-09 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // Time Complexity: O(n) 11 | // Space Complexity: O(1) 12 | class Solution { 13 | public: 14 | void moveZeroes(vector& nums) { 15 | 16 | int k = 0; // keep nums[0...k) are all zero elements 17 | for(int i = 0 ; i < nums.size() ; i ++) 18 | if(nums[i]) 19 | swap(nums[k++] , nums[i]); 20 | } 21 | }; 22 | 23 | 24 | void printVec(const vector& vec){ 25 | for(int e: vec) 26 | cout << e << " "; 27 | cout << endl; 28 | } 29 | 30 | int main() { 31 | 32 | int arr[] = {0, 1, 0, 3, 12}; 33 | vector vec(arr, arr + sizeof(arr) / sizeof(int)); 34 | Solution().moveZeroes(vec); 35 | printVec(vec); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /0203-Remove-Linked-List-Elements/cpp-0203/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/remove-linked-list-elements/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-09-17 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | ///Definition for singly-linked list. 10 | struct ListNode { 11 | int val; 12 | ListNode *next; 13 | ListNode(int x) : val(x), next(NULL) {} 14 | }; 15 | 16 | 17 | /// Recursive 18 | /// Time Complexity: O(n) 19 | /// Space Complexity: O(n) 20 | class Solution { 21 | public: 22 | ListNode* removeElements(ListNode* head, int val) { 23 | 24 | if(!head) 25 | return head; 26 | 27 | if(head->val == val){ 28 | ListNode* node = head->next; 29 | delete head; 30 | return removeElements(node, val); 31 | } 32 | 33 | head->next = removeElements(head->next, val); 34 | return head; 35 | } 36 | }; 37 | 38 | 39 | int main() { 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /0024-Swap-Nodes-in-Pairs/java-0024/src/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | 3 | public int val; 4 | public ListNode next = null; 5 | 6 | public ListNode(int x) { 7 | val = x; 8 | } 9 | 10 | public ListNode (int[] arr){ 11 | 12 | if(arr == null || arr.length == 0) 13 | throw new IllegalArgumentException("arr can not be empty"); 14 | 15 | this.val = arr[0]; 16 | ListNode curNode = this; 17 | for(int i = 1 ; i < arr.length ; i ++){ 18 | curNode.next = new ListNode(arr[i]); 19 | curNode = curNode.next; 20 | } 21 | } 22 | 23 | @Override 24 | public String toString(){ 25 | 26 | StringBuilder s = new StringBuilder(""); 27 | ListNode curNode = this; 28 | while(curNode != null){ 29 | s.append(Integer.toString(curNode.val)); 30 | s.append(" -> "); 31 | curNode = curNode.next; 32 | } 33 | s.append("NULL"); 34 | return s.toString(); 35 | } 36 | } -------------------------------------------------------------------------------- /0203-Remove-Linked-List-Elements/java-0203/src/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | 3 | public int val; 4 | public ListNode next = null; 5 | 6 | public ListNode(int x) { 7 | val = x; 8 | } 9 | 10 | public ListNode (int[] arr){ 11 | 12 | if(arr == null || arr.length == 0) 13 | throw new IllegalArgumentException("arr can not be empty"); 14 | 15 | this.val = arr[0]; 16 | ListNode curNode = this; 17 | for(int i = 1 ; i < arr.length ; i ++){ 18 | curNode.next = new ListNode(arr[i]); 19 | curNode = curNode.next; 20 | } 21 | } 22 | 23 | @Override 24 | public String toString(){ 25 | 26 | StringBuilder s = new StringBuilder(""); 27 | ListNode curNode = this; 28 | while(curNode != null){ 29 | s.append(Integer.toString(curNode.val)); 30 | s.append(" -> "); 31 | curNode = curNode.next; 32 | } 33 | s.append("NULL"); 34 | return s.toString(); 35 | } 36 | } -------------------------------------------------------------------------------- /0283-Move-Zeroes/java-0283/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/move-zeroes/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-02-09 4 | 5 | import java.util.*; 6 | 7 | // Time Complexity: O(n) 8 | // Space Complexity: O(n) 9 | class Solution2 { 10 | 11 | public void moveZeroes(int[] nums) { 12 | 13 | int k = 0; // keep nums[0...k) are all zero elements 14 | for(int i = 0 ; i < nums.length ; i ++) 15 | if(nums[i] != 0) 16 | nums[k++] = nums[i]; 17 | 18 | // make the nums[k...end) zeros 19 | for(int i = k ; i < nums.length ; i ++) 20 | nums[i] = 0; 21 | } 22 | 23 | private static void printArr(int[] arr){ 24 | for(int i = 0 ; i < arr.length ; i ++) 25 | System.out.print(arr[i] + " "); 26 | System.out.println(); 27 | } 28 | 29 | public static void main(String args[]){ 30 | 31 | int[] arr = {0, 1, 0, 3, 12}; 32 | (new Solution2()).moveZeroes(arr); 33 | printArr(arr); 34 | } 35 | } -------------------------------------------------------------------------------- /0279-Perfect-Squares/cpp-0279/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/perfect-squares/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | /// Memory Search 11 | /// Time Complexity: O(n) 12 | /// Space Complexity: O(n) 13 | class Solution { 14 | 15 | public: 16 | int numSquares(int n) { 17 | 18 | vector mem(n + 1, -1); 19 | 20 | return numSquares(n, mem); 21 | } 22 | 23 | private: 24 | int numSquares(int n, vector& mem){ 25 | 26 | if(n == 0) 27 | return 0; 28 | 29 | if(mem[n] != -1) 30 | return mem[n]; 31 | 32 | int res = INT_MAX; 33 | for(int i = 1; n - i * i >= 0; i ++ ) 34 | res = min(res, 1 + numSquares(n - i * i, mem)); 35 | return mem[n] = res; 36 | } 37 | }; 38 | 39 | int main() { 40 | 41 | cout << Solution().numSquares(12) << endl; 42 | cout << Solution().numSquares(13) << endl; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /0283-Move-Zeroes/cpp-0283/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/move-zeroes/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-02-09 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // Time Complexity: O(n) 11 | // Space Complexity: O(1) 12 | class Solution { 13 | public: 14 | void moveZeroes(vector& nums) { 15 | 16 | int k = 0; // keep nums[0...k) are all zero elements 17 | for(int i = 0 ; i < nums.size() ; i ++ ) 18 | if(nums[i]) 19 | nums[k++] = nums[i]; 20 | 21 | // make the nums[k...end) zeros 22 | for(int i = k ; i < nums.size() ; i ++) 23 | nums[i] = 0; 24 | } 25 | }; 26 | 27 | 28 | void printVec(const vector& vec){ 29 | for(int e: vec) 30 | cout << e << " "; 31 | cout << endl; 32 | } 33 | 34 | int main() { 35 | 36 | int arr[] = {0, 1, 0, 3, 12}; 37 | vector vec(arr, arr + sizeof(arr) / sizeof(int)); 38 | Solution().moveZeroes(vec); 39 | printVec(vec); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /0283-Move-Zeroes/java-0283/src/Solution3.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/move-zeroes/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-02-09 4 | 5 | import java.util.*; 6 | 7 | // Time Complexity: O(n) 8 | // Space Complexity: O(n) 9 | class Solution3 { 10 | 11 | public void moveZeroes(int[] nums) { 12 | 13 | int k = 0; // keep nums[0...k) are all zero elements 14 | for(int i = 0 ; i < nums.length ; i ++) 15 | if(nums[i] != 0) 16 | swap(nums, k++, i); 17 | } 18 | 19 | private void swap(int[] nums, int i, int j){ 20 | int t = nums[i]; 21 | nums[i] = nums[j]; 22 | nums[j] = t; 23 | } 24 | 25 | private static void printArr(int[] arr){ 26 | for(int i = 0 ; i < arr.length ; i ++) 27 | System.out.print(arr[i] + " "); 28 | System.out.println(); 29 | } 30 | 31 | public static void main(String args[]){ 32 | 33 | int[] arr = {0, 1, 0, 3, 12}; 34 | (new Solution3()).moveZeroes(arr); 35 | printArr(arr); 36 | } 37 | } -------------------------------------------------------------------------------- /0283-Move-Zeroes/cpp-0283/main4.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/move-zeroes/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-02-09 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // Time Complexity: O(n) 11 | // Space Complexity: O(1) 12 | class Solution { 13 | public: 14 | void moveZeroes(vector& nums) { 15 | 16 | int k = 0; // keep nums[0...k) are all zero elements 17 | for(int i = 0 ; i < nums.size() ; i ++ ) 18 | if(nums[i]) 19 | // avoid self swapping 20 | if(k != i) 21 | swap(nums[k++], nums[i]); 22 | else 23 | k ++; 24 | } 25 | }; 26 | 27 | 28 | void printVec(const vector& vec){ 29 | for(int e: vec) 30 | cout << e << " "; 31 | cout << endl; 32 | } 33 | 34 | int main() { 35 | 36 | int arr[] = {0, 1, 0, 3, 12}; 37 | vector vec(arr, arr + sizeof(arr) / sizeof(int)); 38 | Solution().moveZeroes(vec); 39 | printVec(vec); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /0279-Perfect-Squares/java-0279/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/perfect-squares/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.Arrays; 6 | 7 | /// Memory Search 8 | /// Time Complexity: O(n) 9 | /// Space Complexity: O(n) 10 | public class Solution2 { 11 | 12 | public int numSquares(int n) { 13 | 14 | int[] mem = new int[n+1]; 15 | Arrays.fill(mem, -1); 16 | 17 | return numSquares(n, mem); 18 | } 19 | 20 | private int numSquares(int n, int[] mem){ 21 | 22 | if(n == 0) 23 | return 0; 24 | 25 | if(mem[n] != -1) 26 | return mem[n]; 27 | 28 | int res = Integer.MAX_VALUE; 29 | for(int i = 1; n - i * i >= 0; i ++ ) 30 | res = Math.min(res, 1 + numSquares(n - i * i, mem)); 31 | return mem[n] = res; 32 | } 33 | 34 | public static void main(String[] args) { 35 | 36 | System.out.println((new Solution2()).numSquares(12)); 37 | System.out.println((new Solution2()).numSquares(13)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /0001-Two-Sum/java-0001/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | import java.util.HashMap; 6 | 7 | /// Brute Force 8 | /// Time Complexity: O(n^2) 9 | /// Space Complexity: O(1) 10 | public class Solution1 { 11 | 12 | public int[] twoSum(int[] nums, int target) { 13 | 14 | for(int i = 0 ; i < nums.length; i ++) 15 | for(int j = 0 ; j < nums.length ; j ++) 16 | if(nums[i] + nums[j] == target){ 17 | int[] res = {i, j}; 18 | return res; 19 | } 20 | 21 | throw new IllegalStateException("the input has no solution"); 22 | } 23 | 24 | private static void printArr(int[] nums){ 25 | for(int num: nums) 26 | System.out.print(num + " "); 27 | System.out.println(); 28 | } 29 | 30 | public static void main(String[] args) { 31 | 32 | int[] nums = {0, 4, 3, 0}; 33 | int target = 0; 34 | printArr((new Solution1()).twoSum(nums, target)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution3.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Classic Non-Recursive algorithm for inorder traversal 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(h), h is the height of the tree 12 | public class Solution3 { 13 | 14 | public List inorderTraversal(TreeNode root) { 15 | 16 | ArrayList res = new ArrayList(); 17 | if(root == null) 18 | return res; 19 | 20 | Stack stack = new Stack<>(); 21 | TreeNode cur = root; 22 | while(cur != null || !stack.empty()){ 23 | 24 | while(cur != null){ 25 | stack.push(cur); 26 | cur = cur.left; 27 | } 28 | 29 | cur = stack.pop(); 30 | res.add(cur.val); 31 | cur = cur.right; 32 | } 33 | return res; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0024-Swap-Nodes-in-Pairs/java-0024/src/Solution.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | /// Time Complexity: O(n) 6 | /// Space Complexity: O(1) 7 | public class Solution { 8 | 9 | public ListNode swapPairs(ListNode head) { 10 | 11 | ListNode dummyHead = new ListNode(0); 12 | dummyHead.next = head; 13 | 14 | ListNode p = dummyHead; 15 | while(p.next != null && p.next.next != null ){ 16 | ListNode node1 = p.next; 17 | ListNode node2 = node1.next; 18 | ListNode next = node2.next; 19 | node2.next = node1; 20 | node1.next = next; 21 | p.next = node2; 22 | p = node1; 23 | } 24 | 25 | return dummyHead.next; 26 | } 27 | 28 | public static void main(String[] args) { 29 | 30 | int[] arr = {1, 2, 3, 4}; 31 | 32 | ListNode head = new ListNode(arr); 33 | System.out.println(head); 34 | 35 | head = (new Solution()).swapPairs(head); 36 | System.out.println(head); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /0203-Remove-Linked-List-Elements/java-0203/src/Solution.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/remove-linked-list-elements/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | /// Time Complexity: O(n) 6 | /// Space Complexity: O(1) 7 | public class Solution { 8 | 9 | public ListNode removeElements(ListNode head, int val) { 10 | 11 | ListNode dummyHead = new ListNode(0); 12 | dummyHead.next = head; 13 | 14 | ListNode cur = dummyHead; 15 | while(cur.next != null){ 16 | if(cur.next.val == val ){ 17 | ListNode delNode = cur.next; 18 | cur.next = delNode.next; 19 | } 20 | else 21 | cur = cur.next; 22 | } 23 | 24 | return dummyHead.next; 25 | } 26 | 27 | public static void main(String[] args) { 28 | 29 | int[] arr = {1, 2, 6, 3, 4, 5, 6}; 30 | int val = 6; 31 | 32 | ListNode head = new ListNode(arr); 33 | System.out.println(head); 34 | 35 | (new Solution()).removeElements(head, val); 36 | System.out.println(head); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution4.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Another Classic Non-Recursive algorithm for preorder traversal 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(h), h is the height of the tree 12 | public class Solution4 { 13 | 14 | public List preorderTraversal(TreeNode root) { 15 | 16 | ArrayList res = new ArrayList(); 17 | if(root == null) 18 | return res; 19 | 20 | Stack stack = new Stack(); 21 | TreeNode cur = root; 22 | while(cur != null || !stack.isEmpty()){ 23 | while(cur != null){ 24 | res.add(cur.val); 25 | stack.push(cur); 26 | cur = cur.left; 27 | } 28 | 29 | cur = stack.pop(); 30 | cur = cur.right; 31 | } 32 | return res; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution3.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Classic Non-Recursive algorithm for preorder traversal 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(h), h is the height of the tree 12 | public class Solution3 { 13 | 14 | public List preorderTraversal(TreeNode root) { 15 | 16 | ArrayList res = new ArrayList(); 17 | if(root == null) 18 | return res; 19 | 20 | Stack stack = new Stack(); 21 | stack.push(root); 22 | while(!stack.empty()){ 23 | TreeNode curNode = stack.pop(); 24 | res.add(curNode.val); 25 | 26 | if(curNode.right != null) 27 | stack.push(curNode.right); 28 | if(curNode.left != null) 29 | stack.push(curNode.left); 30 | } 31 | return res; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/cpp-0094/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | // Recursive 20 | // Time Complexity: O(n), n is the node number in the tree 21 | // Space Complexity: O(h), h is the height of the tree 22 | class Solution { 23 | public: 24 | vector inorderTraversal(TreeNode* root) { 25 | 26 | vector res; 27 | __inorderTraversal(root, res); 28 | return res; 29 | } 30 | 31 | private: 32 | void __inorderTraversal(TreeNode* node, vector &res){ 33 | 34 | if( node ){ 35 | __inorderTraversal(node->left, res); 36 | res.push_back( node->val ); 37 | __inorderTraversal(node->right, res); 38 | } 39 | } 40 | }; 41 | 42 | int main() { 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /0283-Move-Zeroes/java-0283/src/Solution4.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/move-zeroes/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-02-09 4 | 5 | import java.util.*; 6 | 7 | // Time Complexity: O(n) 8 | // Space Complexity: O(n) 9 | class Solution4 { 10 | 11 | public void moveZeroes(int[] nums) { 12 | 13 | int k = 0; // keep nums[0...k) are all zero elements 14 | for(int i = 0 ; i < nums.length ; i ++) 15 | if(nums[i] != 0) 16 | if(k != i) 17 | swap(nums, k ++, i); 18 | else 19 | k ++; 20 | } 21 | 22 | private void swap(int[] nums, int i, int j){ 23 | int t = nums[i]; 24 | nums[i] = nums[j]; 25 | nums[j] = t; 26 | } 27 | 28 | private static void printArr(int[] arr){ 29 | for(int i = 0 ; i < arr.length ; i ++) 30 | System.out.print(arr[i] + " "); 31 | System.out.println(); 32 | } 33 | 34 | public static void main(String args[]){ 35 | 36 | int[] arr = {0, 1, 0, 3, 12}; 37 | (new Solution4()).moveZeroes(arr); 38 | printArr(arr); 39 | } 40 | } -------------------------------------------------------------------------------- /0001-Two-Sum/cpp-0001/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | /// Brute Force 11 | /// Time Complexity: O(n^2) 12 | /// Space Complexity: O(1) 13 | class Solution { 14 | public: 15 | vector twoSum(vector& nums, int target) { 16 | 17 | for(int i = 0 ; i < nums.size() ; i ++) 18 | for(int j = i + 1 ; j < nums.size() ; j ++) 19 | if(nums[i] + nums[j] == target){ 20 | int res[] = {i, j}; 21 | return vector(res, res + 2); 22 | } 23 | 24 | throw invalid_argument("the input has no solution"); 25 | } 26 | }; 27 | 28 | 29 | void printVec(const vector& vec){ 30 | for(int e: vec) 31 | cout << e << " "; 32 | cout << endl; 33 | } 34 | 35 | int main() { 36 | 37 | const int nums[] = {0,4,3,0}; 38 | vector nums_vec( nums, nums + sizeof(nums)/sizeof(int) ); 39 | int target = 0; 40 | printVec(Solution().twoSum(nums_vec, target)); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution4.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Another Classic Non-Recursive algorithm for inorder traversal 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(h), h is the height of the tree 12 | public class Solution4 { 13 | 14 | public List inorderTraversal(TreeNode root) { 15 | 16 | ArrayList res = new ArrayList(); 17 | if(root == null) 18 | return res; 19 | 20 | Stack stack = new Stack<>(); 21 | TreeNode cur = root; 22 | while(cur != null || !stack.empty()){ 23 | 24 | if(cur != null){ 25 | stack.push(cur); 26 | cur = cur.left; 27 | } 28 | else{ 29 | cur = stack.pop(); 30 | res.add(cur.val); 31 | cur = cur.right; 32 | } 33 | } 34 | return res; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/cpp-0144/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | // Recursive 20 | // Time Complexity: O(n), n is the node number in the tree 21 | // Space Complexity: O(h), h is the height of the tree 22 | class Solution { 23 | public: 24 | vector preorderTraversal(TreeNode* root) { 25 | 26 | vector res; 27 | preorderTraversal(root, res); 28 | return res; 29 | } 30 | 31 | private: 32 | void preorderTraversal(TreeNode* node, vector &res){ 33 | 34 | if(node){ 35 | res.push_back(node->val); 36 | preorderTraversal(node->left, res); 37 | preorderTraversal(node->right, res); 38 | } 39 | } 40 | }; 41 | 42 | int main() { 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | // 5 | // Time Complexity: O(n) 6 | // Space Complexity: O(1) 7 | public class Solution1 { 8 | 9 | public ListNode removeNthFromEnd(ListNode head, int n) { 10 | 11 | ListNode dummyHead = new ListNode(0); 12 | dummyHead.next = head; 13 | 14 | int length = 0; 15 | for(ListNode cur = dummyHead.next ; cur != null ; cur = cur.next) 16 | length ++; 17 | 18 | int k = length - n; 19 | assert k >= 0; 20 | ListNode cur = dummyHead; 21 | for(int i = 0 ; i < k ; i ++) 22 | cur = cur.next; 23 | 24 | cur.next = cur.next.next; 25 | 26 | return dummyHead.next; 27 | } 28 | 29 | public static void main(String[] args) { 30 | 31 | int arr[] = {1, 2, 3, 4, 5}; 32 | ListNode head = new ListNode(arr); 33 | System.out.println(head); 34 | 35 | head = (new Solution1()).removeNthFromEnd(head, 2); 36 | System.out.println(head); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /0075-Sort-Colors/java-0075/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/sort-colors/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | // Counting 6 | // Time Complexity: O(n) 7 | // Space Complexity: O(3) 8 | public class Solution1 { 9 | 10 | public void sortColors(int[] nums) { 11 | 12 | int[] count = {0, 0, 0}; 13 | for(int i = 0 ; i < nums.length ; i ++){ 14 | assert nums[i] >= 0 && nums[i] <= 2; 15 | count[nums[i]] ++; 16 | } 17 | 18 | int index = 0; 19 | for(int i = 0 ; i < count[0] ; i ++) 20 | nums[index++] = 0; 21 | for(int i = 0 ; i < count[1] ; i ++) 22 | nums[index++] = 1; 23 | for(int i = 0 ; i < count[2] ; i ++) 24 | nums[index++] = 2; 25 | } 26 | 27 | public static void printArr(int[] nums){ 28 | for(int num: nums) 29 | System.out.print(num + " "); 30 | System.out.println(); 31 | } 32 | 33 | public static void main(String[] args) { 34 | 35 | int[] nums = {2, 2, 2, 1, 1, 0}; 36 | (new Solution1()).sortColors(nums); 37 | printArr(nums); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | // Recursive 20 | // Time Complexity: O(n), n is the node number in the tree 21 | // Space Complexity: O(h), h is the height of the tree 22 | class Solution { 23 | public: 24 | vector postorderTraversal(TreeNode* root) { 25 | 26 | vector res; 27 | __postorderTraversal(root, res); 28 | return res; 29 | } 30 | 31 | private: 32 | void __postorderTraversal(TreeNode* node, vector &res){ 33 | 34 | if( node ){ 35 | __postorderTraversal(node->left, res); 36 | __postorderTraversal(node->right, res); 37 | res.push_back(node->val); 38 | } 39 | } 40 | }; 41 | 42 | int main() { 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution5.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Another Classic Non-Recursive algorithm for preorder traversal 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(h), h is the height of the tree 12 | public class Solution5 { 13 | 14 | public List preorderTraversal(TreeNode root) { 15 | 16 | ArrayList res = new ArrayList(); 17 | if(root == null) 18 | return res; 19 | 20 | Stack stack = new Stack(); 21 | TreeNode cur = root; 22 | while(cur != null || !stack.isEmpty()){ 23 | if(cur != null){ 24 | res.add(cur.val); 25 | stack.push(cur); 26 | cur = cur.left; 27 | } 28 | else{ 29 | cur = stack.pop(); 30 | cur = cur.right; 31 | } 32 | } 33 | return res; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | // 5 | // Two Pointers - One Pass Algorithm 6 | // Time Complexity: O(n) 7 | // Space Complexity: O(1) 8 | public class Solution2 { 9 | 10 | public ListNode removeNthFromEnd(ListNode head, int n) { 11 | 12 | ListNode dummyHead = new ListNode(0); 13 | dummyHead.next = head; 14 | 15 | ListNode p = dummyHead; 16 | ListNode q = dummyHead; 17 | for( int i = 0 ; i < n + 1 ; i ++ ){ 18 | assert q != null; 19 | q = q.next; 20 | } 21 | 22 | while(q != null){ 23 | p = p.next; 24 | q = q.next; 25 | } 26 | 27 | p.next = p.next.next; 28 | 29 | return dummyHead.next; 30 | } 31 | 32 | public static void main(String[] args) { 33 | 34 | int arr[] = {1, 2, 3, 4, 5}; 35 | ListNode head = new ListNode(arr); 36 | System.out.println(head); 37 | 38 | head = (new Solution2()).removeNthFromEnd(head, 2); 39 | System.out.println(head); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution4.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Non-Recursive 10 | // Using two stacks, Reverse the Preorder Traversal! 11 | // 12 | // Time Complexity: O(n) 13 | // Space Complexity: O(n) 14 | public class Solution4 { 15 | 16 | public List postorderTraversal(TreeNode root) { 17 | 18 | ArrayList res = new ArrayList(); 19 | if(root == null) 20 | return res; 21 | 22 | Stack stack = new Stack<>(); 23 | Stack output = new Stack<>(); 24 | 25 | stack.push(root); 26 | while(!stack.empty()){ 27 | 28 | TreeNode cur = stack.pop(); 29 | output.push(cur.val); 30 | 31 | if(cur.left != null) 32 | stack.push(cur.left); 33 | if(cur.right != null) 34 | stack.push(cur.right); 35 | } 36 | 37 | while(!output.empty()) 38 | res.add(output.pop()); 39 | return res; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution5.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | import java.util.LinkedList; 9 | 10 | // Non-Recursive 11 | // Using two stacks, Reverse the Preorder Traversal! 12 | // 13 | // Time Complexity: O(n) 14 | // Space Complexity: O(n) 15 | public class Solution5 { 16 | 17 | public List postorderTraversal(TreeNode root){ 18 | 19 | Stack stack = new Stack<>(); 20 | LinkedList output = new LinkedList<>(); 21 | 22 | TreeNode p = root; 23 | while(p != null || !stack.isEmpty()){ 24 | if(p != null){ 25 | stack.push(p); 26 | output.push(p); 27 | p = p.right; 28 | } 29 | else{ 30 | p = stack.pop(); 31 | p = p.left; 32 | } 33 | } 34 | 35 | List res = new ArrayList<>(); 36 | while(!output.isEmpty()) 37 | res.add(output.pop().val); 38 | return res; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /0001-Two-Sum/java-0001/src/Solution3.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | import java.util.HashMap; 6 | 7 | /// One-Pass Hash Table 8 | /// Time Complexity: O(n) 9 | /// Space Complexity: O(n) 10 | public class Solution3 { 11 | 12 | public int[] twoSum(int[] nums, int target) { 13 | 14 | HashMap record = new HashMap(); 15 | for(int i = 0 ; i < nums.length; i ++){ 16 | 17 | int complement = target - nums[i]; 18 | if(record.containsKey(complement)){ 19 | int[] res = {i, record.get(complement)}; 20 | return res; 21 | } 22 | 23 | record.put(nums[i], i); 24 | } 25 | 26 | throw new IllegalStateException("the input has no solution"); 27 | } 28 | 29 | private static void printArr(int[] nums){ 30 | for(int num: nums) 31 | System.out.print(num + " "); 32 | System.out.println(); 33 | } 34 | 35 | public static void main(String[] args) { 36 | 37 | int[] nums = {0, 4, 3, 0}; 38 | int target = 0; 39 | printArr((new Solution3()).twoSum(nums, target)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /0219-Contains-Duplicate-II/java-0219/src/Solution.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/contains-duplicate-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | import java.util.HashSet; 6 | 7 | /// Using Hash Set 8 | /// Time Complexity: O(n) 9 | /// Space Complexity: O(k) 10 | public class Solution { 11 | 12 | public boolean containsNearbyDuplicate(int[] nums, int k) { 13 | 14 | if(nums == null || nums.length <= 1) 15 | return false; 16 | 17 | if(k <= 0) 18 | return false; 19 | 20 | HashSet record = new HashSet(); 21 | for(int i = 0 ; i < nums.length; i ++){ 22 | if(record.contains(nums[i])) 23 | return true; 24 | 25 | record.add(nums[i]); 26 | if(record.size() == k + 1) 27 | record.remove(nums[i-k]); 28 | } 29 | 30 | return false; 31 | } 32 | 33 | private static void printBool(boolean b){ 34 | System.out.println(b ? "True" : "False"); 35 | } 36 | 37 | public static void main(String[] args) { 38 | 39 | int[] nums = {1, 2, 1}; 40 | int k = 1; 41 | printBool((new Solution()).containsNearbyDuplicate(nums, k)); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0002-Add-Two-Numbers/cpp-0002/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/add-two-numbers/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-08-09 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | /// Definition for singly-linked list. 10 | struct ListNode { 11 | int val; 12 | ListNode *next; 13 | ListNode(int x) : val(x), next(NULL) {} 14 | }; 15 | 16 | 17 | /// 时间复杂度: O(n) 18 | /// 空间复杂度: O(n) 19 | class Solution { 20 | public: 21 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 22 | 23 | ListNode *p1 = l1, *p2 = l2; 24 | ListNode *dummyHead = new ListNode(-1); 25 | ListNode* cur = dummyHead; 26 | int carried = 0; 27 | while(p1 || p2 ){ 28 | int a = p1 ? p1->val : 0; 29 | int b = p2 ? p2->val : 0; 30 | cur->next = new ListNode((a + b + carried) % 10); 31 | carried = (a + b + carried) / 10; 32 | 33 | cur = cur->next; 34 | p1 = p1 ? p1->next : NULL; 35 | p2 = p2 ? p2->next : NULL; 36 | } 37 | 38 | cur->next = carried ? new ListNode(1) : NULL; 39 | ListNode* ret = dummyHead->next; 40 | delete dummyHead; 41 | return ret; 42 | } 43 | }; 44 | 45 | 46 | int main() { 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 2 | /// Author : liuyubobobo 3 | /// Time : 2016-12-06 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | /// Two pointers 14 | /// Time Complexity: O(n) 15 | /// Space Complexity: O(1) 16 | class Solution { 17 | public: 18 | int removeDuplicates(vector& nums) { 19 | 20 | if(nums.size() == 0) 21 | return 0; 22 | 23 | int res = 1; 24 | int index = nextDifferentCharacterIndex(nums, 1); 25 | int i = 1; 26 | while(index < nums.size()){ 27 | res ++; 28 | nums[i++] = nums[index]; 29 | index = nextDifferentCharacterIndex(nums, index + 1); 30 | } 31 | 32 | return res; 33 | } 34 | 35 | private: 36 | int nextDifferentCharacterIndex(const vector &nums, int p){ 37 | for( ; p < nums.size() ; p ++ ) 38 | if( nums[p] != nums[p - 1] ) 39 | break; 40 | return p; 41 | } 42 | }; 43 | 44 | 45 | int main() { 46 | 47 | vector nums1 = {1, 1, 2}; 48 | cout << Solution().removeDuplicates(nums1) << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /0075-Sort-Colors/cpp-0075/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/sort-colors/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // Counting 12 | // Time Complexity: O(n) 13 | // Space Complexity: O(3) 14 | class Solution { 15 | public: 16 | void sortColors(vector &nums) { 17 | 18 | int count[3] = {0}; 19 | for(int i = 0 ; i < nums.size() ; i ++){ 20 | assert(nums[i] >= 0 && nums[i] <= 2); 21 | count[nums[i]] ++; 22 | } 23 | 24 | int index = 0; 25 | for(int i = 0 ; i < count[0] ; i ++) 26 | nums[index++] = 0; 27 | for(int i = 0 ; i < count[1] ; i ++) 28 | nums[index++] = 1; 29 | for(int i = 0 ; i < count[2] ; i ++) 30 | nums[index++] = 2; 31 | } 32 | }; 33 | 34 | 35 | void printArr(const vector& vec){ 36 | for(int e: vec) 37 | cout << e << " "; 38 | cout << endl; 39 | } 40 | 41 | int main() { 42 | 43 | vector vec1 = {2, 2, 2, 1, 1, 0}; 44 | Solution().sortColors(vec1); 45 | printArr(vec1); 46 | 47 | vector vec2 = {2}; 48 | Solution().sortColors(vec2); 49 | printArr(vec2); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /0075-Sort-Colors/cpp-0075/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/sort-colors/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // Three Way Quick Sort 12 | // Time Complexity: O(n) 13 | // Space Complexity: O(1) 14 | class Solution { 15 | public: 16 | void sortColors(vector &nums) { 17 | 18 | int zero = -1; // [0...zero] == 0 19 | int two = nums.size(); // [two...n-1] == 2 20 | for(int i = 0 ; i < two ; ){ 21 | if(nums[i] == 1) 22 | i ++; 23 | else if (nums[i] == 2) 24 | swap( nums[i] , nums[--two]); 25 | else{ // nums[i] == 0 26 | assert(nums[i] == 0); 27 | swap(nums[++zero] , nums[i++]); 28 | } 29 | } 30 | } 31 | }; 32 | 33 | 34 | void printArr(const vector& vec){ 35 | for(int e: vec) 36 | cout << e << " "; 37 | cout << endl; 38 | } 39 | 40 | int main() { 41 | 42 | vector vec1 = {2, 2, 2, 1, 1, 0}; 43 | Solution().sortColors(vec1); 44 | printArr(vec1); 45 | 46 | vector vec2 = {2}; 47 | Solution().sortColors(vec2); 48 | printArr(vec2); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /0237-Delete-Node-in-a-Linked-List/java-0237/src/ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | 3 | public int val; 4 | public ListNode next = null; 5 | 6 | public ListNode(int x) { 7 | val = x; 8 | } 9 | 10 | public ListNode (int[] arr){ 11 | 12 | if(arr == null || arr.length == 0) 13 | throw new IllegalArgumentException("arr can not be empty"); 14 | 15 | this.val = arr[0]; 16 | ListNode curNode = this; 17 | for(int i = 1 ; i < arr.length ; i ++){ 18 | curNode.next = new ListNode(arr[i]); 19 | curNode = curNode.next; 20 | } 21 | } 22 | 23 | ListNode findNode(int x){ 24 | 25 | ListNode curNode = this; 26 | while(curNode != null){ 27 | if(curNode.val == x) 28 | return curNode; 29 | curNode = curNode.next; 30 | } 31 | return null; 32 | } 33 | 34 | @Override 35 | public String toString(){ 36 | 37 | StringBuilder s = new StringBuilder(""); 38 | ListNode curNode = this; 39 | while(curNode != null){ 40 | s.append(Integer.toString(curNode.val)); 41 | s.append(" -> "); 42 | curNode = curNode.next; 43 | } 44 | s.append("NULL"); 45 | return s.toString(); 46 | } 47 | } -------------------------------------------------------------------------------- /0075-Sort-Colors/java-0075/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/sort-colors/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | // Three Way Quick Sort 6 | // Time Complexity: O(n) 7 | // Space Complexity: O(1) 8 | public class Solution2 { 9 | 10 | public void sortColors(int[] nums) { 11 | 12 | int zero = -1; // [0...zero] == 0 13 | int two = nums.length; // [two...n-1] == 2 14 | for(int i = 0 ; i < two ; ){ 15 | if(nums[i] == 1) 16 | i ++; 17 | else if (nums[i] == 2) 18 | swap(nums, i, --two); 19 | else{ // nums[i] == 0 20 | assert nums[i] == 0; 21 | swap(nums, ++zero, i++); 22 | } 23 | } 24 | } 25 | 26 | private void swap(int[] nums, int i, int j){ 27 | int t = nums[i]; 28 | nums[i]= nums[j]; 29 | nums[j] = t; 30 | } 31 | 32 | public static void printArr(int[] nums){ 33 | for(int num: nums) 34 | System.out.print(num + " "); 35 | System.out.println(); 36 | } 37 | 38 | public static void main(String[] args) { 39 | 40 | int[] nums = {2, 2, 2, 1, 1, 0}; 41 | (new Solution2()).sortColors(nums); 42 | printArr(nums); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0350-Intersection-of-Two-Arrays-II/cpp-0350/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-14 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Using Hash Map 12 | /// Time Complexity: O(len(nums1) + len(nums2)*log(len(nums1))) 13 | /// Space Complexity: O(len(nums1)) 14 | class Solution { 15 | public: 16 | vector intersect(vector& nums1, vector& nums2) { 17 | 18 | multiset record; 19 | for(int num: nums1) 20 | record.insert(num); 21 | 22 | multiset result; 23 | for(int num: nums2){ 24 | multiset::iterator iter = record.find(num); 25 | if( iter != record.end()){ 26 | result.insert(num); 27 | record.erase(iter); 28 | } 29 | } 30 | 31 | return vector(result.begin(), result.end()); 32 | } 33 | }; 34 | 35 | int main() { 36 | 37 | int nums1[] = {1, 2, 2, 1}; 38 | vector vec1(nums1, nums1 + sizeof(nums1)/sizeof(int)); 39 | 40 | int nums2[] = {2, 2}; 41 | vector vec2(nums2, nums2 + sizeof(nums2)/sizeof(int)); 42 | 43 | printVec(Solution().intersect(vec1, vec2)); 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /0001-Two-Sum/cpp-0001/main3.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// One-Pass Hash Table 13 | /// Time Complexity: O(n) 14 | /// Space Complexity: O(n) 15 | class Solution { 16 | public: 17 | vector twoSum(vector& nums, int target) { 18 | 19 | unordered_map record; 20 | for(int i = 0 ; i < nums.size() ; i ++){ 21 | 22 | int complement = target - nums[i]; 23 | if(record.find(complement) != record.end()){ 24 | int res[] = {i, record[complement]}; 25 | return vector(res, res + 2); 26 | } 27 | 28 | record[nums[i]] = i; 29 | } 30 | 31 | throw invalid_argument("the input has no solution"); 32 | } 33 | }; 34 | 35 | 36 | void printVec(const vector& vec){ 37 | for(int e: vec) 38 | cout << e << " "; 39 | cout << endl; 40 | } 41 | 42 | int main() { 43 | 44 | const int nums[] = {0,4,3,0}; 45 | vector nums_vec( nums, nums + sizeof(nums)/sizeof(int) ); 46 | int target = 0; 47 | printVec(Solution().twoSum(nums_vec, target)); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /0001-Two-Sum/java-0001/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | import java.util.HashMap; 6 | 7 | /// Two-Pass Hash Table 8 | /// Time Complexity: O(n) 9 | /// Space Complexity: O(n) 10 | public class Solution2 { 11 | 12 | public int[] twoSum(int[] nums, int target) { 13 | 14 | HashMap record = new HashMap(); 15 | for(int i = 0 ; i < nums.length ; i ++) 16 | record.put(nums[i], i); 17 | 18 | for(int i = 0 ; i < nums.length; i ++){ 19 | 20 | Integer index = record.get(target - nums[i]); 21 | if(index != null && index != i){ 22 | int[] res = {i, index}; 23 | return res; 24 | } 25 | 26 | record.put(nums[i], i); 27 | } 28 | 29 | throw new IllegalStateException("the input has no solution"); 30 | } 31 | 32 | private static void printArr(int[] nums){ 33 | for(int num: nums) 34 | System.out.print(num + " "); 35 | System.out.println(); 36 | } 37 | 38 | public static void main(String[] args) { 39 | 40 | int[] nums = {0, 4, 3, 0}; 41 | int target = 0; 42 | printArr((new Solution2()).twoSum(nums, target)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0349-Intersection-of-Two-Arrays/cpp-0349/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/intersection-of-two-arrays/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-07-12 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Hash Set 12 | /// Time complexity: O(len(nums1) + len(nums2)) 13 | /// Space Complexity: O(len(nums1)) 14 | class Solution { 15 | public: 16 | vector intersection(vector& nums1, vector& nums2) { 17 | 18 | unordered_set record(nums1.begin(), nums1.end()); 19 | 20 | unordered_set resultSet; 21 | for( int i = 0 ; i < nums2.size() ; i ++ ) 22 | if( record.find(nums2[i]) != record.end() ) 23 | resultSet.insert( nums2[i] ); 24 | 25 | return vector(resultSet.begin(), resultSet.end()); 26 | } 27 | }; 28 | 29 | 30 | void printVec(const vector& vec){ 31 | for(int e: vec) 32 | cout << e << " "; 33 | cout << endl; 34 | } 35 | 36 | int main() { 37 | 38 | int nums1[] = {1, 2, 2, 1}; 39 | vector vec1(nums1, nums1 + sizeof(nums1)/sizeof(int)); 40 | 41 | int nums2[] = {2, 2}; 42 | vector vec2(nums2, nums2 + sizeof(nums2)/sizeof(int)); 43 | 44 | printVec(Solution().intersection(vec1, vec2)); 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /0349-Intersection-of-Two-Arrays/java-0349/src/Solution.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/intersection-of-two-arrays/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-07-12 4 | 5 | import java.util.HashSet; 6 | 7 | /// Hash Set 8 | /// Time complexity: O(len(nums1) + len(nums2)) 9 | /// Space Complexity: O(len(nums1)) 10 | public class Solution { 11 | 12 | public int[] intersection(int[] nums1, int[] nums2) { 13 | 14 | HashSet record = new HashSet(); 15 | for(int num: nums1) 16 | record.add(num); 17 | 18 | HashSet resultSet = new HashSet(); 19 | for(int num: nums2) 20 | if(record.contains(num)) 21 | resultSet.add(num); 22 | 23 | int[] res = new int[resultSet.size()]; 24 | int index = 0; 25 | for(Integer num: resultSet) 26 | res[index++] = num; 27 | 28 | return res; 29 | } 30 | 31 | private static void printArr(int[] arr){ 32 | for(int e: arr) 33 | System.out.print(e + " "); 34 | System.out.println(); 35 | } 36 | 37 | public static void main(String[] args) { 38 | 39 | int[] nums1 = {1, 2, 2, 1}; 40 | int[] nums2 = {2, 2}; 41 | int[] res = (new Solution()).intersection(nums1, nums2); 42 | printArr(res); 43 | } 44 | } -------------------------------------------------------------------------------- /0150-Evaluate-Reverse-Polish-Notation/cpp-0150/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/evaluate-reverse-polish-notation/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-08-29 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | 12 | /// Two stacks 13 | /// Time Complexity: O(n) 14 | /// Space Complexity: O(n) 15 | class Solution { 16 | public: 17 | int evalRPN(vector& tokens) { 18 | 19 | stack nums; 20 | stack ops; 21 | for(const string& s: tokens){ 22 | if(s == "+" || s == "-" || s == "*" || s == "/"){ 23 | int a = nums.top(); 24 | nums.pop(); 25 | int b = nums.top(); 26 | nums.pop(); 27 | 28 | if(s == "+"){ 29 | nums.push(b + a); 30 | }else if(s == "-"){ 31 | nums.push(b - a); 32 | } else if(s == "*"){ 33 | nums.push(b * a); 34 | }else if(s == "/"){ 35 | nums.push(b / a); 36 | } 37 | } 38 | else{ 39 | nums.push(atoi(s.c_str())); 40 | } 41 | } 42 | return nums.top(); 43 | } 44 | }; 45 | 46 | 47 | int main() { 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /0002-Add-Two-Numbers/cpp-0002/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/add-two-numbers/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-08-09 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | /// Definition for singly-linked list. 10 | struct ListNode { 11 | int val; 12 | ListNode *next; 13 | ListNode(int x) : val(x), next(NULL) {} 14 | }; 15 | 16 | 17 | /// Using l1 as the result list 18 | /// Time Complexity: O(n) 19 | /// Space Complexity: O(n) 20 | class Solution { 21 | 22 | public: 23 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 24 | 25 | ListNode *p1 = l1, *p2 = l2; 26 | ListNode* pre = NULL; 27 | int carried = 0; 28 | 29 | while(p1 || p2){ 30 | int a = p1 ? p1->val : 0; 31 | int b = p2 ? p2->val : 0; 32 | if(p1) 33 | p1->val = (a + b + carried) % 10; 34 | else{ 35 | pre->next = new ListNode((a + b + carried) % 10); 36 | p1 = pre->next; 37 | } 38 | carried = (a + b + carried) / 10; 39 | 40 | pre = p1; 41 | p1 = p1->next; 42 | if(p2) p2 = p2->next; 43 | } 44 | 45 | pre->next = carried ? new ListNode(1) : NULL; 46 | return l1; 47 | } 48 | }; 49 | 50 | 51 | int main() { 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /0283-Move-Zeroes/cpp-0283/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/move-zeroes/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-02-09 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // Time Complexity: O(n) 11 | // Space Complexity: O(n) 12 | class Solution { 13 | public: 14 | void moveZeroes(vector& nums) { 15 | 16 | vector nonZeroElements; 17 | 18 | // put all the non zero elements into a new vector 19 | for(int i = 0 ; i < nums.size() ; i ++) 20 | if(nums[i]) 21 | nonZeroElements.push_back(nums[i]); 22 | 23 | // make nums[0...nonZeroElements.size()) all non zero elements 24 | for(int i = 0 ; i < nonZeroElements.size() ; i ++) 25 | nums[i] = nonZeroElements[i]; 26 | 27 | // make nums[nonZeroElements.size()...nums.size()) all zero elements 28 | for(int i = nonZeroElements.size() ; i < nums.size() ; i ++) 29 | nums[i] = 0; 30 | } 31 | }; 32 | 33 | 34 | void printVec(const vector& vec){ 35 | for(int e: vec) 36 | cout << e << " "; 37 | cout << endl; 38 | } 39 | 40 | int main() { 41 | 42 | int arr[] = {0, 1, 0, 3, 12}; 43 | vector vec(arr, arr + sizeof(arr) / sizeof(int)); 44 | Solution().moveZeroes(vec); 45 | printVec(vec); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution7.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-31 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Classic Non-Recursive 10 | // Using a pre pointer to record the last visted node 11 | // 12 | // Time Complexity: O(n) 13 | // Space Complexity: O(h) 14 | public class Solution7 { 15 | 16 | public List postorderTraversal(TreeNode root) { 17 | 18 | ArrayList res = new ArrayList(); 19 | if(root == null) 20 | return res; 21 | 22 | Stack stack = new Stack<>(); 23 | TreeNode pre = null; 24 | TreeNode cur = root; 25 | 26 | while(cur != null || !stack.empty()){ 27 | 28 | while(cur != null){ 29 | stack.push(cur); 30 | cur = cur.left; 31 | } 32 | 33 | cur = stack.pop(); 34 | if(cur.right == null || pre == cur.right){ 35 | res.add(cur.val); 36 | pre = cur; 37 | cur = null; 38 | } 39 | else{ 40 | stack.push(cur); 41 | cur = cur.right; 42 | } 43 | } 44 | return res; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0279-Perfect-Squares/cpp-0279/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/perfect-squares/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// BFS 13 | /// Time Complexity: O(n) 14 | /// Space Complexity: O(n) 15 | class Solution { 16 | public: 17 | int numSquares(int n) { 18 | 19 | if(n == 0) 20 | return 0; 21 | 22 | queue> q; 23 | q.push(make_pair(n, 0)); 24 | 25 | vector visited(n + 1, false); 26 | visited[n] = true; 27 | 28 | while(!q.empty()){ 29 | int num = q.front().first; 30 | int step = q.front().second; 31 | q.pop(); 32 | 33 | for(int i = 1; num - i * i >= 0; i ++){ 34 | int a = num - i * i; 35 | if(!visited[a]){ 36 | if(a == 0) return step + 1; 37 | q.push(make_pair(a, step + 1)); 38 | visited[a] = true; 39 | } 40 | } 41 | } 42 | 43 | throw invalid_argument("No Solution."); 44 | } 45 | }; 46 | 47 | int main() { 48 | 49 | cout << Solution().numSquares(12) << endl; 50 | cout << Solution().numSquares(13) << endl; 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /0283-Move-Zeroes/java-0283/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/move-zeroes/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-02-09 4 | 5 | import java.util.*; 6 | 7 | // Time Complexity: O(n) 8 | // Space Complexity: O(n) 9 | class Solution1 { 10 | 11 | public void moveZeroes(int[] nums) { 12 | 13 | ArrayList nonZeroElements = new ArrayList(); 14 | 15 | // put all the non zero elements into a new vector 16 | for (int i = 0; i < nums.length; i++) 17 | if (nums[i] != 0) 18 | nonZeroElements.add(nums[i]); 19 | 20 | // make nums[0...nonZeroElements.size()) all non zero elements 21 | for (int i = 0; i < nonZeroElements.size(); i++) 22 | nums[i] = nonZeroElements.get(i); 23 | 24 | // make nums[nonZeroElements.size()...nums.size()) all zero elements 25 | for (int i = nonZeroElements.size(); i < nums.length; i++) 26 | nums[i] = 0; 27 | } 28 | 29 | private static void printArr(int[] arr){ 30 | for(int i = 0 ; i < arr.length ; i ++) 31 | System.out.print(arr[i] + " "); 32 | System.out.println(); 33 | } 34 | 35 | public static void main(String args[]){ 36 | 37 | int[] arr = {0, 1, 0, 3, 12}; 38 | (new Solution1()).moveZeroes(arr); 39 | printArr(arr); 40 | } 41 | } -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/cpp-0094/main3.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-05-30 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | 21 | // Classic Non-Recursive algorithm for inorder traversal 22 | // Time Complexity: O(n), n is the node number in the tree 23 | // Space Complexity: O(h), h is the height of the tree 24 | class Solution { 25 | 26 | public: 27 | vector inorderTraversal(TreeNode* root) { 28 | 29 | vector res; 30 | if( root == NULL ) 31 | return res; 32 | 33 | stack stack; 34 | TreeNode* cur = root; 35 | while(cur != NULL || !stack.empty()){ 36 | 37 | while(cur != NULL){ 38 | stack.push(cur); 39 | cur = cur->left; 40 | } 41 | 42 | cur = stack.top(); 43 | stack.pop(); 44 | res.push_back(cur->val); 45 | cur = cur->right; 46 | 47 | } 48 | return res; 49 | } 50 | }; 51 | 52 | int main() { 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/cpp-0144/main3.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | // Classic Non-Recursive algorithm for preorder traversal 21 | // Time Complexity: O(n), n is the node number in the tree 22 | // Space Complexity: O(h), h is the height of the tree 23 | class Solution { 24 | 25 | public: 26 | vector preorderTraversal(TreeNode* root) { 27 | 28 | vector res; 29 | if(root == NULL) 30 | return res; 31 | 32 | stack stack; 33 | stack.push(root); 34 | while(!stack.empty()){ 35 | TreeNode* curNode = stack.top(); 36 | stack.pop(); 37 | res.push_back(curNode->val); 38 | 39 | if(curNode->right) 40 | stack.push(curNode->right); 41 | if(curNode->left) 42 | stack.push(curNode->left); 43 | } 44 | return res; 45 | } 46 | }; 47 | 48 | int main() { 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /0219-Contains-Duplicate-II/cpp-0219/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/contains-duplicate-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Using Hash Set 12 | /// Time Complexity: O(n) 13 | /// Space Complexity: O(k) 14 | class Solution { 15 | public: 16 | bool containsNearbyDuplicate(vector& nums, int k) { 17 | 18 | if(nums.size() <= 1) 19 | return false; 20 | 21 | if(k <= 0) 22 | return false; 23 | 24 | unordered_set record; 25 | for(int i = 0 ; i < nums.size() ; i ++){ 26 | 27 | if(record.find(nums[i]) != record.end()) 28 | return true; 29 | 30 | record.insert(nums[i]); 31 | 32 | // 保持record中最多有k个元素 33 | // 因为在下一次循环中会添加一个新元素,使得总共考虑k+1个元素 34 | if(record.size() == k + 1) 35 | record.erase(nums[i - k]); 36 | } 37 | 38 | return false; 39 | } 40 | }; 41 | 42 | 43 | void printBool(bool b){ 44 | cout << (b ? "True" : "False") << endl; 45 | } 46 | 47 | int main() { 48 | 49 | int nums[] = {1, 2, 1}; 50 | vector vec(nums, nums + sizeof(nums)/sizeof(int)); 51 | int k = 1; 52 | 53 | printBool(Solution().containsNearbyDuplicate(vec, k)); 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /0001-Two-Sum/cpp-0001/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// Two-Pass Hash Table 13 | /// Time Complexity: O(n) 14 | /// Space Complexity: O(n) 15 | class Solution { 16 | public: 17 | vector twoSum(vector& nums, int target) { 18 | 19 | unordered_map record; 20 | for(int i = 0 ; i < nums.size() ; i ++) 21 | record[nums[i]] = i; 22 | 23 | for(int i = 0 ; i < nums.size() ; i ++){ 24 | unordered_map::iterator iter = record.find(target - nums[i]); 25 | if(iter != record.end() && iter->second != i){ 26 | int res[] = {i, iter->second}; 27 | return vector(res, res + 2); 28 | } 29 | } 30 | 31 | throw invalid_argument("the input has no solution"); 32 | } 33 | }; 34 | 35 | 36 | void printVec(const vector& vec){ 37 | for(int e: vec) 38 | cout << e << " "; 39 | cout << endl; 40 | } 41 | 42 | int main() { 43 | 44 | const int nums[] = {0,4,3,0}; 45 | vector nums_vec( nums, nums + sizeof(nums)/sizeof(int) ); 46 | int target = 0; 47 | printVec(Solution().twoSum(nums_vec, target)); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution5.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Inorder Morris Traversal 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(1) 12 | public class Solution5 { 13 | 14 | public List inorderTraversal(TreeNode root) { 15 | 16 | ArrayList res = new ArrayList(); 17 | if(root == null) 18 | return res; 19 | 20 | TreeNode cur = root; 21 | while(cur != null){ 22 | 23 | if(cur.left == null){ 24 | res.add(cur.val); 25 | cur = cur.right; 26 | } 27 | else{ 28 | TreeNode prev = cur.left; 29 | while(prev.right != null && prev.right != cur) 30 | prev = prev.right; 31 | 32 | if(prev.right == null){ 33 | prev.right = cur; 34 | cur = cur.left; 35 | } 36 | else{ 37 | prev.right = null; 38 | res.add(cur.val); 39 | cur = cur.right; 40 | } 41 | } 42 | } 43 | return res; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /0328-Odd-Even-Linked-List/cpp-0328/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/odd-even-linked-list/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-10-01 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | 10 | /// Definition for singly-linked list. 11 | struct ListNode { 12 | int val; 13 | ListNode *next; 14 | ListNode(int x) : val(x), next(NULL) {} 15 | }; 16 | 17 | 18 | /// Split the Linked List into two and then merge 19 | /// Keep one in original Linked List 20 | /// 21 | /// Time Complexity: O(n) 22 | /// Space Complexity: O(1) 23 | class Solution { 24 | public: 25 | ListNode* oddEvenList(ListNode* head) { 26 | 27 | if(head == NULL || head->next == NULL || head->next->next == NULL) 28 | return head; 29 | 30 | ListNode* dummyHead2 = new ListNode(-1); 31 | ListNode* p2 = dummyHead2; 32 | ListNode* p = head; 33 | while(p->next){ 34 | p2->next = p->next; 35 | if(p->next->next == NULL){ 36 | p->next = NULL; 37 | break; 38 | } 39 | p->next = p->next->next; 40 | p = p->next; 41 | p2 = p2->next; 42 | p2->next = NULL; 43 | } 44 | 45 | p->next = dummyHead2->next; 46 | delete dummyHead2; 47 | return head; 48 | } 49 | }; 50 | 51 | 52 | int main() { 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /0350-Intersection-of-Two-Arrays-II/cpp-0350/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-14 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | /// Using Hash Map 11 | /// Time Complexity: O(len(nums1) + len(nums2)) 12 | /// Space Complexity: O(len(nums1)) 13 | class Solution { 14 | public: 15 | vector intersect(vector& nums1, vector& nums2) { 16 | 17 | unordered_map record; 18 | for(int i = 0 ; i < nums1.size() ; i ++) 19 | record[nums1[i]] += 1; 20 | 21 | vector resultVector; 22 | for(int i = 0 ; i < nums2.size() ; i ++) 23 | if(record[nums2[i]] > 0){ 24 | resultVector.push_back(nums2[i]); 25 | record[nums2[i]] --; 26 | } 27 | 28 | return resultVector; 29 | } 30 | }; 31 | 32 | 33 | void printVec(const vector& vec){ 34 | for(int e: vec) 35 | cout << e << " "; 36 | cout << endl; 37 | } 38 | 39 | int main() { 40 | 41 | int nums1[] = {1, 2, 2, 1}; 42 | vector vec1(nums1, nums1 + sizeof(nums1)/sizeof(int)); 43 | 44 | int nums2[] = {2, 2}; 45 | vector vec2(nums2, nums2 + sizeof(nums2)/sizeof(int)); 46 | 47 | printVec(Solution().intersect(vec1, vec2)); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution6.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-29 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // PreOrder Morris Traversal 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(1) 12 | public class Solution6 { 13 | 14 | public List preorderTraversal(TreeNode root) { 15 | 16 | ArrayList res = new ArrayList(); 17 | if(root == null) 18 | return res; 19 | 20 | TreeNode cur = root; 21 | while(cur != null){ 22 | if(cur.left == null){ 23 | res.add(cur.val); 24 | cur = cur.right; 25 | } 26 | else{ 27 | TreeNode prev = cur.left; 28 | while(prev.right != null && prev.right != cur) 29 | prev = prev.right; 30 | 31 | if(prev.right == null){ 32 | res.add(cur.val); 33 | prev.right = cur; 34 | cur = cur.left; 35 | } 36 | else{ 37 | prev.right = null; 38 | cur = cur.right; 39 | } 40 | } 41 | } 42 | 43 | return res; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/cpp-0094/main4.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-05-30 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | 21 | // Another Classic Non-Recursive algorithm for inorder traversal 22 | // Time Complexity: O(n), n is the node number in the tree 23 | // Space Complexity: O(h), h is the height of the tree 24 | class Solution { 25 | 26 | public: 27 | vector inorderTraversal(TreeNode* root) { 28 | 29 | vector res; 30 | if( root == NULL ) 31 | return res; 32 | 33 | stack stack; 34 | TreeNode* cur = root; 35 | while(cur != NULL || !stack.empty()){ 36 | 37 | if(cur != NULL){ 38 | stack.push(cur); 39 | cur = cur->left; 40 | } 41 | else { 42 | cur = stack.top(); 43 | stack.pop(); 44 | res.push_back(cur->val); 45 | cur = cur->right; 46 | } 47 | } 48 | return res; 49 | } 50 | }; 51 | 52 | int main() { 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution8.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-31 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Classic Non-Recursive 10 | // Using a pre pointer to record the last visted node 11 | // 12 | // Time Complexity: O(n) 13 | // Space Complexity: O(h) 14 | public class Solution8 { 15 | 16 | public List postorderTraversal(TreeNode root) { 17 | 18 | ArrayList res = new ArrayList(); 19 | if(root == null) 20 | return res; 21 | 22 | Stack stack = new Stack<>(); 23 | TreeNode pre = null; 24 | TreeNode cur = root; 25 | 26 | while(cur != null || !stack.empty()){ 27 | 28 | if(cur != null){ 29 | stack.push(cur); 30 | cur = cur.left; 31 | } 32 | else{ 33 | cur = stack.pop(); 34 | if(cur.right == null || pre == cur.right){ 35 | res.add(cur.val); 36 | pre = cur; 37 | cur = null; 38 | } 39 | else{ 40 | stack.push(cur); 41 | cur = cur.right; 42 | } 43 | } 44 | } 45 | return res; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.LinkedList; 8 | import javafx.util.Pair; 9 | 10 | /// BFS 11 | /// Time Complexity: O(n), where n is the number of nodes in the tree 12 | /// Space Complexity: O(n) 13 | class Solution { 14 | 15 | public List> levelOrder(TreeNode root) { 16 | 17 | ArrayList> res = new ArrayList<>(); 18 | if(root == null) 19 | return res; 20 | 21 | LinkedList> queue = new LinkedList<>(); 22 | queue.addLast(new Pair<>(root, 0)); 23 | 24 | while(!queue.isEmpty()){ 25 | 26 | Pair front = queue.removeFirst(); 27 | TreeNode node = front.getKey(); 28 | int level = front.getValue(); 29 | 30 | if(level == res.size()) 31 | res.add(new ArrayList<>()); 32 | assert level < res.size(); 33 | 34 | res.get(level).add(node.val); 35 | if(node.left != null) 36 | queue.addLast(new Pair<>(node.left, level + 1)); 37 | if(node.right != null) 38 | queue.addLast(new Pair<>(node.right, level + 1)); 39 | } 40 | 41 | return res; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution6.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-31 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Non-Recursive 10 | // Using a pre pointer to record the last visted node 11 | // 12 | // Time Complexity: O(n) 13 | // Space Complexity: O(h) 14 | public class Solution6 { 15 | 16 | public List postorderTraversal(TreeNode root) { 17 | 18 | ArrayList res = new ArrayList(); 19 | if(root == null) 20 | return res; 21 | 22 | Stack stack = new Stack<>(); 23 | TreeNode pre = null; 24 | 25 | stack.push(root); 26 | while(!stack.empty()){ 27 | 28 | TreeNode cur = stack.pop(); 29 | if((cur.left == null && cur.right == null) || 30 | (pre != null && pre == cur.left && cur.right == null) || 31 | (pre != null && pre == cur.right)){ 32 | res.add(cur.val); 33 | pre = cur; 34 | } 35 | else{ 36 | stack.push(cur); 37 | if(cur.right != null) 38 | stack.push(cur.right); 39 | if(cur.left != null) 40 | stack.push(cur.left); 41 | } 42 | } 43 | return res; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /0454-4Sum-II/java-0454/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// https://leetcode.com/problems/4sum-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | import java.util.HashMap; 6 | 7 | /// Using Hash Map 8 | /// Time Complexity: O(n^2) 9 | /// Space Complexity: O(n^2) 10 | public class Solution1 { 11 | 12 | public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { 13 | 14 | if(A == null || B == null || C == null || D == null) 15 | throw new IllegalArgumentException("Illegal argument"); 16 | 17 | HashMap map = new HashMap(); 18 | for(int i = 0 ; i < C.length ; i ++) 19 | for(int j = 0 ; j < D.length ; j ++){ 20 | int sum = C[i] + D[j]; 21 | if(map.containsKey(sum)) 22 | map.put(sum, map.get(sum) + 1); 23 | else 24 | map.put(sum, 1); 25 | } 26 | 27 | int res = 0; 28 | for(int i = 0 ; i < A.length ; i ++) 29 | for(int j = 0 ; j < B.length ; j ++) 30 | if(map.containsKey(-A[i]-B[j])) 31 | res += map.get(-A[i]-B[j]); 32 | 33 | return res; 34 | } 35 | 36 | public static void main(String[] args) { 37 | 38 | int[] a = {1, 2}; 39 | int[] b = {-2, -1}; 40 | int[] c = {-1, 2}; 41 | int[] d = {0, 2}; 42 | System.out.println((new Solution1()).fourSumCount(a, b, c, d)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0086-Partition-List/cpp-0086/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/partition-list/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-07-07 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | /// Definition for singly-linked list. 10 | struct ListNode { 11 | int val; 12 | ListNode *next; 13 | ListNode(int x) : val(x), next(NULL) {} 14 | }; 15 | 16 | /// Linear Scan 17 | /// Time Complexity: O(n) 18 | /// Space Complexity: O(1) 19 | class Solution { 20 | public: 21 | ListNode* partition(ListNode* head, int x) { 22 | 23 | ListNode* dummyHead1 = new ListNode(-1); 24 | ListNode* dummyHead2 = new ListNode(-1); 25 | ListNode* prev1 = dummyHead1; 26 | ListNode* prev2 = dummyHead2; 27 | 28 | for(ListNode* cur = head ; cur != NULL ;){ 29 | if(cur->val < x){ 30 | prev1->next = cur; 31 | cur = cur->next; 32 | prev1 = prev1->next; 33 | prev1->next = NULL; 34 | } 35 | else{ 36 | prev2->next = cur; 37 | cur = cur->next; 38 | prev2 = prev2->next; 39 | prev2->next = NULL; 40 | } 41 | } 42 | 43 | prev1->next = dummyHead2->next; 44 | ListNode* ret = dummyHead1->next; 45 | 46 | delete dummyHead1; 47 | delete dummyHead2; 48 | return ret; 49 | } 50 | }; 51 | 52 | 53 | int main() { 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | // Brute Force 6 | // Time Complexity: O(n^2) 7 | // Space Complexity: O(1) 8 | public class Solution1 { 9 | 10 | public int[] twoSum(int[] numbers, int target) { 11 | 12 | if(numbers.length < 2 /*|| !isSorted(numbers)*/) 13 | throw new IllegalArgumentException("Illegal argument numbers"); 14 | 15 | for(int i = 0 ; i < numbers.length ; i ++) 16 | for(int j = i+1 ; j < numbers.length ; j ++) 17 | if(numbers[i] + numbers[j] == target){ 18 | int[] res = {i+1, j+1}; 19 | return res; 20 | } 21 | 22 | throw new IllegalStateException("The input has no solution"); 23 | } 24 | 25 | private boolean isSorted(int[] numbers){ 26 | for(int i = 1 ; i < numbers.length ; i ++) 27 | if(numbers[i] < numbers[i-1]) 28 | return false; 29 | return true; 30 | } 31 | 32 | private static void printArr(int[] nums){ 33 | for(int num: nums) 34 | System.out.print(num + " "); 35 | System.out.println(); 36 | } 37 | 38 | public static void main(String[] args) { 39 | 40 | int[] nums = {2, 7, 11, 15}; 41 | int target = 9; 42 | printArr((new Solution1()).twoSum(nums, target)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/main4.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | 20 | // Non-Recursive 21 | // Using two stacks, Reverse the Preorder Traversal! 22 | // 23 | // Time Complexity: O(n) 24 | // Space Complexity: O(n) 25 | class Solution { 26 | 27 | public: 28 | vector postorderTraversal(TreeNode* root) { 29 | 30 | vector res; 31 | if(root == NULL) 32 | return res; 33 | 34 | stack stack, output; 35 | 36 | stack.push(root); 37 | while(!stack.empty()){ 38 | 39 | TreeNode* node = stack.top(); 40 | stack.pop(); 41 | output.push(node); 42 | 43 | if(node->left != NULL) 44 | stack.push(node->left); 45 | if(node->right != NULL) 46 | stack.push(node->right); 47 | } 48 | 49 | while(!output.empty()){ 50 | res.push_back((output.top())->val); 51 | output.pop(); 52 | } 53 | 54 | return res; 55 | } 56 | }; 57 | 58 | int main() { 59 | 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /0002-Add-Two-Numbers/cpp-0002/main3.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/add-two-numbers/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-08-09 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | /// Definition for singly-linked list. 10 | struct ListNode { 11 | int val; 12 | ListNode *next; 13 | ListNode(int x) : val(x), next(NULL) {} 14 | }; 15 | 16 | 17 | /// Using the longest list in l1 and l2 as the result list 18 | /// Time Complexity: O(n) 19 | /// Space Complexity: O(1) 20 | class Solution { 21 | 22 | public: 23 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 24 | 25 | int len1 = getLen(l1), len2 = getLen(l2); 26 | ListNode *p1 = len1 > len2 ? l1 : l2; 27 | ListNode *p2 = len1 > len2 ? l2 : l1; 28 | 29 | ListNode* pre = NULL; 30 | int carried = 0; 31 | while(p1){ 32 | int a = p1->val; 33 | int b = p2 ? p2->val : 0; 34 | p1->val = (a + b + carried) % 10; 35 | carried = (a + b + carried) / 10; 36 | 37 | pre = p1; 38 | p1 = p1->next; 39 | p2 = p2 ? p2->next : NULL; 40 | } 41 | 42 | pre->next = carried ? new ListNode(1) : NULL; 43 | return len1 > len2 ? l1 : l2; 44 | } 45 | 46 | private: 47 | int getLen(ListNode* l){ 48 | int res = 0; 49 | while(l) 50 | res ++, l = l -> next; 51 | return res; 52 | } 53 | }; 54 | 55 | 56 | int main() { 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /0019-Remove-Nth-Node-From-End-of-List/java-0019/src/ListNode.java: -------------------------------------------------------------------------------- 1 | // Definition for singly-linked list. 2 | // 在Java版本中,我们将LinkedList相关的测试辅助函数写在ListNode里 3 | public class ListNode { 4 | 5 | public int val; 6 | public ListNode next = null; 7 | 8 | public ListNode(int x) { 9 | val = x; 10 | } 11 | 12 | // 根据n个元素的数组arr创建一个链表 13 | // 使用arr为参数,创建另外一个ListNode的构造函数 14 | public ListNode (int[] arr){ 15 | 16 | if(arr == null || arr.length == 0) 17 | throw new IllegalArgumentException("arr can not be empty"); 18 | 19 | this.val = arr[0]; 20 | ListNode curNode = this; 21 | for(int i = 1 ; i < arr.length ; i ++){ 22 | curNode.next = new ListNode(arr[i]); 23 | curNode = curNode.next; 24 | } 25 | } 26 | 27 | ListNode findNode(int x){ 28 | 29 | ListNode curNode = this; 30 | while(curNode != null){ 31 | if(curNode.val == x) 32 | return curNode; 33 | curNode = curNode.next; 34 | } 35 | return null; 36 | } 37 | 38 | // 返回以当前ListNode为头结点的链表信息字符串 39 | @Override 40 | public String toString(){ 41 | 42 | StringBuilder s = new StringBuilder(""); 43 | ListNode curNode = this; 44 | while(curNode != null){ 45 | s.append(Integer.toString(curNode.val)); 46 | s.append(" -> "); 47 | curNode = curNode.next; 48 | } 49 | s.append("NULL"); 50 | return s.toString(); 51 | } 52 | } -------------------------------------------------------------------------------- /0454-4Sum-II/cpp-0454/main.cpp: -------------------------------------------------------------------------------- 1 | /// https://leetcode.com/problems/4sum-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// Using Hash Map 13 | /// Time Complexity: O(n^2) 14 | /// Space Complexity: O(n^2) 15 | class Solution { 16 | public: 17 | int fourSumCount(vector& A, vector& B, vector& C, vector& D) { 18 | 19 | unordered_map hashtable; 20 | for(int i = 0 ; i < C.size() ; i ++) 21 | for(int j = 0 ; j < D.size() ; j ++) 22 | hashtable[C[i]+D[j]] += 1; 23 | 24 | int res = 0; 25 | for(int i = 0 ; i < A.size() ; i ++) 26 | for(int j = 0 ; j < B.size() ; j ++) 27 | if(hashtable.find(-A[i]-B[j]) != hashtable.end()) 28 | res += hashtable[-A[i]-B[j]]; 29 | 30 | return res; 31 | } 32 | }; 33 | 34 | int main() { 35 | 36 | int a[] = {1, 2}; 37 | int b[] = {-2, -1}; 38 | int c[] = {-1, 2}; 39 | int d[] = {0, 2}; 40 | vector a_vec = vector(a, a + sizeof(a)/sizeof(int)); 41 | vector b_vec = vector(b, b + sizeof(b)/sizeof(int)); 42 | vector c_vec = vector(c, c + sizeof(c)/sizeof(int)); 43 | vector d_vec = vector(d, d + sizeof(d)/sizeof(int)); 44 | 45 | cout << Solution().fourSumCount(a_vec, b_vec, c_vec, d_vec) << endl; 46 | 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/main5.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | 20 | // Non-Recursive 21 | // Using two stacks, Reverse the Preorder Traversal! 22 | // 23 | // Time Complexity: O(n) 24 | // Space Complexity: O(n) 25 | class Solution { 26 | 27 | public: 28 | vector postorderTraversal(TreeNode* root) { 29 | 30 | vector res; 31 | if(root == NULL) 32 | return res; 33 | 34 | stack stack, output; 35 | 36 | TreeNode* p = root; 37 | while(p != NULL || !stack.empty()){ 38 | if(p != NULL){ 39 | stack.push(p); 40 | output.push(p); 41 | p = p->right; 42 | } 43 | else{ 44 | p = stack.top(); 45 | stack.pop(); 46 | p = p->left; 47 | } 48 | } 49 | 50 | while(!output.empty()){ 51 | res.push_back((output.top())->val); 52 | output.pop(); 53 | } 54 | 55 | return res; 56 | } 57 | }; 58 | 59 | int main() { 60 | 61 | return 0; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /0092-Reverse-Linked-List-II/cpp-0092/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/reverse-linked-list-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-10-02 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | 10 | /// Recursive 11 | /// Time Complexity: O(n) 12 | /// Space Complexity: O(n) 13 | 14 | /// Definition for singly-linked list. 15 | struct ListNode { 16 | int val; 17 | ListNode *next; 18 | ListNode(int x) : val(x), next(NULL) {} 19 | }; 20 | 21 | class Solution { 22 | public: 23 | ListNode* reverseBetween(ListNode* head, int m, int n) { 24 | 25 | ListNode* dummyHead = new ListNode(-1); 26 | dummyHead->next = head; 27 | 28 | ListNode* pre = dummyHead; 29 | for(int i = 0; i < m - 1; i ++){ 30 | pre = pre->next 31 | } 32 | 33 | ListNode* tail = pre->next; 34 | ListNode* left; 35 | pre->next = reverse(pre->next, n - m, left); 36 | tail->next = left; 37 | 38 | ListNode* ret = dummyHead->next; 39 | delete dummyHead; 40 | return ret; 41 | } 42 | 43 | private: 44 | ListNode* reverse(ListNode* head, int index, ListNode* &left){ 45 | 46 | if(index == 0){ 47 | left = head->next; 48 | return head; 49 | } 50 | 51 | ListNode* tail = head->next; 52 | ListNode* ret = reverse(head->next, index - 1, left); 53 | tail->next = head; 54 | return ret; 55 | } 56 | }; 57 | 58 | 59 | int main() { 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // Brute Force 12 | // Time Complexity: O(n^2) 13 | // Space Complexity: O(1) 14 | class Solution { 15 | public: 16 | vector twoSum(vector& numbers, int target) { 17 | 18 | assert(numbers.size() >= 2); 19 | // assert(isSorted(numbers)); 20 | 21 | for(int i = 0 ; i < numbers.size() ; i ++) 22 | for(int j = i+1 ; j < numbers.size() ; j ++) 23 | if(numbers[i] + numbers[j] == target){ 24 | int res[2] = {i+1, j+1}; 25 | return vector(res, res+2); 26 | } 27 | 28 | throw invalid_argument("the input has no solution"); 29 | } 30 | 31 | private: 32 | bool isSorted(const vector& numbers){ 33 | for(int i = 1 ; i < numbers.size() ; i ++) 34 | if(numbers[i] < numbers[i-1]) 35 | return false; 36 | return true; 37 | } 38 | }; 39 | 40 | 41 | void printVec(const vector& vec){ 42 | for(int e: vec) 43 | cout << e << " "; 44 | cout << endl; 45 | } 46 | 47 | int main() { 48 | 49 | int nums[] = {2, 7, 11, 15}; 50 | vector vec(nums, nums + sizeof(nums) / sizeof(int)); 51 | int target = 9; 52 | printVec(Solution().twoSum(vec, target)); 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /0447-Number-of-Boomerangs/java-0447/src/Solution.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/number-of-boomerangs/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | import java.util.HashMap; 6 | 7 | /// Using Hash Map 8 | /// Time Complexity: O(n^2) 9 | /// Space Complexity: O(n) 10 | public class Solution { 11 | 12 | public int numberOfBoomerangs(int[][] points) { 13 | 14 | int res = 0; 15 | for( int i = 0 ; i < points.length ; i ++ ){ 16 | 17 | // record中存储 点i 到所有其他点的距离出现的频次 18 | HashMap record = new HashMap(); 19 | for(int j = 0 ; j < points.length ; j ++) 20 | if(j != i){ 21 | // 计算距离时不进行开根运算, 以保证精度 22 | int dis = dis(points[i], points[j]); 23 | if(record.containsKey(dis)) 24 | record.put(dis, record.get(dis) + 1); 25 | else 26 | record.put(dis, 1); 27 | } 28 | 29 | for(Integer dis: record.keySet()) 30 | res += record.get(dis) * (record.get(dis) - 1); 31 | } 32 | 33 | return res; 34 | } 35 | 36 | private int dis(int[] pa, int pb[]){ 37 | return (pa[0] - pb[0]) * (pa[0] - pb[0]) + 38 | (pa[1] - pb[1]) * (pa[1] - pb[1]); 39 | } 40 | 41 | public static void main(String[] args) { 42 | 43 | int[][] points = {{0, 0}, {1, 0}, {2, 0}}; 44 | System.out.println((new Solution()).numberOfBoomerangs(points)); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | /// BFS 21 | /// Time Complexity: O(n), where n is the number of nodes in the tree 22 | /// Space Complexity: O(n) 23 | class Solution { 24 | public: 25 | vector> levelOrder(TreeNode* root) { 26 | 27 | vector> res; 28 | if(root == NULL) 29 | return res; 30 | 31 | queue> q; 32 | q.push(make_pair(root, 0)); 33 | 34 | while(!q.empty()){ 35 | 36 | TreeNode* node = q.front().first; 37 | int level = q.front().second; 38 | q.pop(); 39 | 40 | if(level == res.size()) 41 | res.push_back(vector()); 42 | assert( level < res.size() ); 43 | 44 | res[level].push_back(node->val); 45 | if(node->left) 46 | q.push(make_pair(node->left, level + 1 )); 47 | if(node->right) 48 | q.push(make_pair(node->right, level + 1 )); 49 | } 50 | 51 | return res; 52 | } 53 | }; 54 | 55 | 56 | int main() { 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /0328-Odd-Even-Linked-List/cpp-0328/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/odd-even-linked-list/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-10-01 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | 10 | /// Definition for singly-linked list. 11 | struct ListNode { 12 | int val; 13 | ListNode *next; 14 | ListNode(int x) : val(x), next(NULL) {} 15 | }; 16 | 17 | 18 | /// Split the Linked List into two and then merge 19 | /// Time Complexity: O(n) 20 | /// Space Complexity: O(1) 21 | class Solution { 22 | public: 23 | ListNode* oddEvenList(ListNode* head) { 24 | 25 | if(head == NULL || head->next == NULL || head->next->next == NULL) 26 | return head; 27 | 28 | ListNode* dummyHead1 = new ListNode(-1); 29 | ListNode* dummyHead2 = new ListNode(-1); 30 | ListNode* p1 = dummyHead1; 31 | ListNode* p2 = dummyHead2; 32 | ListNode* p = head; 33 | for(int i = 0; p; i ++) 34 | if(i % 2 == 0){ 35 | p1->next = p; 36 | p = p->next; 37 | p1 = p1->next; 38 | p1->next = NULL; 39 | } 40 | else{ 41 | p2->next = p; 42 | p = p->next; 43 | p2 = p2->next; 44 | p2->next = NULL; 45 | } 46 | 47 | p1->next = dummyHead2->next; 48 | ListNode* ret = dummyHead1->next; 49 | 50 | delete dummyHead1; 51 | delete dummyHead2; 52 | return ret; 53 | } 54 | }; 55 | 56 | 57 | int main() { 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-10-16 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.LinkedList; 8 | import java.util.Queue; 9 | 10 | /// BFS 11 | /// No need to store level information in the queue :-) 12 | /// 13 | /// Time Complexity: O(n), where n is the number of nodes in the tree 14 | /// Space Complexity: O(n) 15 | class Solution2 { 16 | 17 | public List> levelOrder(TreeNode root) { 18 | 19 | ArrayList> res = new ArrayList<>(); 20 | if(root == null) 21 | return res; 22 | 23 | Queue queue = new LinkedList<>(); 24 | queue.add(root); 25 | int levelNum = 1; 26 | 27 | while(!queue.isEmpty()){ 28 | 29 | int newLevelNum = 0; 30 | ArrayList level = new ArrayList<>(); 31 | for(int i = 0; i < levelNum; i ++){ 32 | TreeNode node = queue.remove(); 33 | level.add(node.val); 34 | 35 | if(node.left != null){ 36 | queue.add(node.left); 37 | newLevelNum ++; 38 | } 39 | if(node.right != null){ 40 | queue.add(node.right); 41 | newLevelNum ++; 42 | } 43 | } 44 | 45 | res.add(level); 46 | levelNum = newLevelNum; 47 | } 48 | 49 | return res; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /0279-Perfect-Squares/java-0279/src/Solution1.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/perfect-squares/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.LinkedList; 6 | import javafx.util.Pair; 7 | 8 | /// BFS 9 | /// Time Complexity: O(n) 10 | /// Space Complexity: O(n) 11 | public class Solution1 { 12 | 13 | public int numSquares(int n) { 14 | 15 | if(n == 0) 16 | return 0; 17 | 18 | LinkedList> queue = new LinkedList>(); 19 | queue.addLast(new Pair(n, 0)); 20 | 21 | boolean[] visited = new boolean[n+1]; 22 | visited[n] = true; 23 | 24 | while(!queue.isEmpty()){ 25 | Pair front = queue.removeFirst(); 26 | int num = front.getKey(); 27 | int step = front.getValue(); 28 | 29 | if(num == 0) 30 | return step; 31 | 32 | for(int i = 1 ; num - i*i >= 0 ; i ++){ 33 | int a = num - i*i; 34 | if(!visited[a]){ 35 | if(a == 0) return step + 1; 36 | queue.addLast(new Pair(num - i * i, step + 1)); 37 | visited[num - i * i] = true; 38 | } 39 | } 40 | } 41 | 42 | throw new IllegalStateException("No Solution."); 43 | } 44 | 45 | public static void main(String[] args) { 46 | 47 | System.out.println((new Solution1()).numSquares(12)); 48 | System.out.println((new Solution1()).numSquares(13)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution3.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | // Two Pointers 6 | // Time Complexity: O(n) 7 | // Space Complexity: O(1) 8 | public class Solution3 { 9 | 10 | public int[] twoSum(int[] numbers, int target) { 11 | 12 | if(numbers.length < 2 /*|| !isSorted(numbers)*/) 13 | throw new IllegalArgumentException("Illegal argument numbers"); 14 | 15 | int l = 0, r = numbers.length - 1; 16 | while(l < r){ 17 | 18 | if(numbers[l] + numbers[r] == target){ 19 | int[] res = {l+1, r+1}; 20 | return res; 21 | } 22 | else if(numbers[l] + numbers[r] < target) 23 | l ++; 24 | else // numbers[l] + numbers[r] > target 25 | r --; 26 | } 27 | 28 | throw new IllegalStateException("The input has no solution"); 29 | } 30 | 31 | private boolean isSorted(int[] numbers){ 32 | for(int i = 1 ; i < numbers.length ; i ++) 33 | if(numbers[i] < numbers[i-1]) 34 | return false; 35 | return true; 36 | } 37 | 38 | private static void printArr(int[] nums){ 39 | for(int num: nums) 40 | System.out.print(num + " "); 41 | System.out.println(); 42 | } 43 | 44 | public static void main(String[] args) { 45 | 46 | int[] nums = {2, 7, 11, 15}; 47 | int target = 9; 48 | printArr((new Solution3()).twoSum(nums, target)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // My Non-Recursive 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(h), h is the height of the tree 12 | public class Solution2 { 13 | 14 | private class Command{ 15 | String s; // go, print 16 | TreeNode node; 17 | Command(String s, TreeNode node){ 18 | this.s = s; 19 | this.node = node; 20 | } 21 | }; 22 | 23 | public List inorderTraversal(TreeNode root) { 24 | 25 | ArrayList res = new ArrayList(); 26 | if(root == null) 27 | return res; 28 | 29 | Stack stack = new Stack(); 30 | stack.push(new Command("go", root)); 31 | while(!stack.empty()){ 32 | Command command = stack.pop(); 33 | 34 | if(command.s.equals("print")) 35 | res.add(command.node.val); 36 | else{ 37 | assert command.s.equals("go"); 38 | if(command.node.right != null) 39 | stack.push(new Command("go",command.node.right)); 40 | stack.push(new Command("print", command.node)); 41 | if(command.node.left != null) 42 | stack.push(new Command("go",command.node.left)); 43 | } 44 | } 45 | return res; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /0447-Number-of-Boomerangs/cpp-0447/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/number-of-boomerangs/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | /// Using Hash Map 14 | /// Time Complexity: O(n^2) 15 | /// Space Complexity: O(n) 16 | class Solution { 17 | public: 18 | int numberOfBoomerangs(vector>& points) { 19 | 20 | int res = 0; 21 | for( int i = 0 ; i < points.size() ; i ++ ){ 22 | 23 | // record中存储 点i 到所有其他点的距离出现的频次 24 | unordered_map record; 25 | for(int j = 0 ; j < points.size() ; j ++) 26 | if(j != i) 27 | // 计算距离时不进行开根运算, 以保证精度 28 | record[dis(points[i], points[j])] += 1; 29 | 30 | for(unordered_map::iterator iter = record.begin() ; iter != record.end() ; iter ++) 31 | res += (iter->second) * (iter->second - 1); 32 | } 33 | return res; 34 | } 35 | 36 | private: 37 | int dis(const pair &pa, const pair &pb){ 38 | return (pa.first - pb.first) * (pa.first - pb.first) + 39 | (pa.second - pb.second) * (pa.second - pb.second); 40 | } 41 | }; 42 | 43 | int main() { 44 | 45 | vector> vec; 46 | vec.push_back(make_pair(0, 0)); 47 | vec.push_back(make_pair(1, 0)); 48 | vec.push_back(make_pair(2, 0)); 49 | 50 | cout << Solution().numberOfBoomerangs(vec) << endl; 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // My Non-Recursive 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(h), h is the height of the tree 12 | public class Solution2 { 13 | 14 | private class Command{ 15 | String s; // go, print 16 | TreeNode node; 17 | Command(String s, TreeNode node){ 18 | this.s = s; 19 | this.node = node; 20 | } 21 | }; 22 | 23 | public List preorderTraversal(TreeNode root) { 24 | 25 | ArrayList res = new ArrayList(); 26 | if(root == null) 27 | return res; 28 | 29 | Stack stack = new Stack(); 30 | stack.push(new Command("go", root)); 31 | while(!stack.empty()){ 32 | Command command = stack.pop(); 33 | 34 | if(command.s.equals("print")) 35 | res.add(command.node.val); 36 | else{ 37 | assert command.s.equals("go"); 38 | if(command.node.right != null) 39 | stack.push(new Command("go",command.node.right)); 40 | if(command.node.left != null) 41 | stack.push(new Command("go",command.node.left)); 42 | stack.push(new Command("print", command.node)); 43 | } 44 | } 45 | return res; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // My Non-Recursive 10 | // Time Complexity: O(n), n is the node number in the tree 11 | // Space Complexity: O(h), h is the height of the tree 12 | public class Solution2 { 13 | 14 | private class Command{ 15 | String s; // go, print 16 | TreeNode node; 17 | Command(String s, TreeNode node){ 18 | this.s = s; 19 | this.node = node; 20 | } 21 | }; 22 | 23 | public List postorderTraversal(TreeNode root) { 24 | 25 | ArrayList res = new ArrayList(); 26 | if(root == null) 27 | return res; 28 | 29 | Stack stack = new Stack(); 30 | stack.push(new Command("go", root)); 31 | while(!stack.empty()){ 32 | Command command = stack.pop(); 33 | 34 | if(command.s.equals("print")) 35 | res.add(command.node.val); 36 | else{ 37 | assert command.s.equals("go"); 38 | stack.push(new Command("print", command.node)); 39 | if(command.node.right != null) 40 | stack.push(new Command("go",command.node.right)); 41 | if(command.node.left != null) 42 | stack.push(new Command("go",command.node.left)); 43 | } 44 | } 45 | return res; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /0350-Intersection-of-Two-Arrays-II/java-0350/src/Solution.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-14 4 | 5 | import java.util.HashMap; 6 | import java.util.ArrayList; 7 | 8 | /// Using Hash Map 9 | /// Time Complexity: O(len(nums1) + len(nums2)*log(len(nums1))) 10 | /// Space Complexity: O(len(nums1)) 11 | public class Solution { 12 | 13 | public int[] intersect(int[] nums1, int[] nums2) { 14 | 15 | HashMap record = new HashMap(); 16 | for(int num: nums1) 17 | if(!record.containsKey(num)) 18 | record.put(num, 1); 19 | else 20 | record.put(num, record.get(num) + 1); 21 | 22 | ArrayList result = new ArrayList(); 23 | for(int num: nums2) 24 | if(record.containsKey(num) && record.get(num) > 0){ 25 | result.add(num); 26 | record.put(num, record.get(num) - 1); 27 | } 28 | 29 | int[] ret = new int[result.size()]; 30 | int index = 0; 31 | for(Integer num: result) 32 | ret[index++] = num; 33 | 34 | return ret; 35 | } 36 | 37 | private static void printArr(int[] arr){ 38 | for(int e: arr) 39 | System.out.print(e + " "); 40 | System.out.println(); 41 | } 42 | 43 | public static void main(String[] args) { 44 | 45 | int[] nums1 = {1, 2, 2, 1}; 46 | int[] nums2 = {2, 2}; 47 | int[] res = (new Solution()).intersect(nums1, nums2); 48 | printArr(res); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /0020-Valid-Parentheses/cpp-0020/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/valid-parentheses/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // Using Stack 12 | // Time Complexity: O(n) 13 | // Space Complexity: O(n) 14 | class Solution { 15 | public: 16 | bool isValid(string s) { 17 | 18 | stack stack; 19 | for( int i = 0 ; i < s.size() ; i ++ ) 20 | if( s[i] == '(' || s[i] == '{' || s[i] == '[') 21 | stack.push(s[i]); 22 | else{ 23 | 24 | if( stack.size() == 0 ) 25 | return false; 26 | 27 | char c = stack.top(); 28 | stack.pop(); 29 | 30 | char match; 31 | if( s[i] == ')' ) 32 | match = '('; 33 | else if( s[i] == ']' ) 34 | match = '['; 35 | else{ 36 | assert( s[i] == '}' ); 37 | match = '{'; 38 | } 39 | 40 | if(c != match) 41 | return false; 42 | } 43 | 44 | if( stack.size() != 0 ) 45 | return false; 46 | 47 | return true; 48 | } 49 | }; 50 | 51 | 52 | void printBool(bool res){ 53 | cout << (res ? "True" : "False") << endl; 54 | } 55 | 56 | int main() { 57 | 58 | printBool(Solution().isValid("()")); 59 | printBool(Solution().isValid("()[]{}")); 60 | printBool(Solution().isValid("(]")); 61 | printBool(Solution().isValid("([)]")); 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/cpp-0094/main5.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | 21 | // InOrder Morris Traversal 22 | // Time Complexity: O(n), n is the node number in the tree 23 | // Space Complexity: O(1) 24 | class Solution { 25 | 26 | public: 27 | vector inorderTraversal(TreeNode* root) { 28 | 29 | vector res; 30 | if( root == NULL ) 31 | return res; 32 | 33 | TreeNode* cur = root; 34 | while(cur != NULL){ 35 | 36 | if(cur->left == NULL){ 37 | res.push_back(cur->val); 38 | cur = cur->right; 39 | } 40 | else{ 41 | TreeNode* prev = cur->left; 42 | while(prev->right != NULL && prev->right != cur) 43 | prev = prev->right; 44 | 45 | if(prev->right == NULL){ 46 | prev->right = cur; 47 | cur = cur->left; 48 | } 49 | else{ 50 | prev->right = NULL; 51 | res.push_back(cur->val); 52 | cur = cur->right; 53 | } 54 | } 55 | } 56 | 57 | return res; 58 | } 59 | }; 60 | 61 | int main() { 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution3.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | // Non-Recursive 10 | // Using a tag to record whether the node has been visited 11 | // 12 | // Time Complexity: O(n), n is the node number in the tree 13 | // Space Complexity: O(h), h is the height of the tree 14 | public class Solution3 { 15 | 16 | private class TagNode{ 17 | TreeNode node; 18 | boolean isFirst; 19 | TagNode(TreeNode node){ 20 | this.node = node; 21 | this.isFirst = false; 22 | } 23 | }; 24 | 25 | public List postorderTraversal(TreeNode root) { 26 | 27 | ArrayList res = new ArrayList(); 28 | if(root == null) 29 | return res; 30 | 31 | Stack stack = new Stack<>(); 32 | TreeNode cur = root; 33 | while(cur != null || !stack.empty()){ 34 | 35 | while(cur != null){ 36 | stack.push(new TagNode(cur)); 37 | cur = cur.left; 38 | } 39 | 40 | TagNode tagNode = stack.pop(); 41 | cur = tagNode.node; 42 | if(tagNode.isFirst == false){ 43 | tagNode.isFirst = true; 44 | stack.push(tagNode); 45 | cur = cur.right; 46 | } 47 | else{ 48 | res.add(cur.val); 49 | cur = null; 50 | } 51 | } 52 | return res; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main3.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // Two Pointers 12 | // Time Complexity: O(n) 13 | // Space Complexity: O(1) 14 | class Solution { 15 | 16 | public: 17 | vector twoSum(vector& numbers, int target) { 18 | 19 | assert(numbers.size() >= 2); 20 | // assert(isSorted(numbers)); 21 | 22 | int l = 0, r = numbers.size() - 1; 23 | while(l < r){ 24 | 25 | if(numbers[l] + numbers[r] == target){ 26 | int res[2] = {l+1, r+1}; 27 | return vector(res, res+2); 28 | } 29 | else if(numbers[l] + numbers[r] < target) 30 | l ++; 31 | else // numbers[l] + numbers[r] > target 32 | r --; 33 | } 34 | 35 | throw invalid_argument("the input has no solution"); 36 | } 37 | 38 | private: 39 | bool isSorted(const vector& numbers){ 40 | for(int i = 1 ; i < numbers.size() ; i ++) 41 | if(numbers[i] < numbers[i-1]) 42 | return false; 43 | return true; 44 | } 45 | 46 | }; 47 | 48 | void printVec(const vector& vec){ 49 | for(int e: vec) 50 | cout << e << " "; 51 | cout << endl; 52 | } 53 | 54 | int main() { 55 | 56 | int nums[] = {2, 7, 11, 15}; 57 | vector vec(nums, nums + sizeof(nums) / sizeof(int)); 58 | int target = 9; 59 | printVec(Solution().twoSum(vec, target)); 60 | 61 | return 0; 62 | } -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/cpp-0144/main6.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-29 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | // PreOrder Morris Traversal 21 | // Time Complexity: O(n), n is the node number in the tree 22 | // Space Complexity: O(1) 23 | class Solution { 24 | 25 | public: 26 | vector preorderTraversal(TreeNode* root) { 27 | 28 | vector res; 29 | if(root == NULL) 30 | return res; 31 | 32 | TreeNode* cur = root; 33 | while(cur != NULL){ 34 | if(cur->left == NULL){ 35 | res.push_back(cur->val); 36 | cur = cur->right; 37 | } 38 | else{ 39 | TreeNode* prev = cur->left; 40 | while(prev->right != NULL && prev->right != cur) 41 | prev = prev->right; 42 | 43 | if(prev->right == NULL){ 44 | res.push_back(cur->val); 45 | prev->right = cur; 46 | cur = cur->left; 47 | } 48 | else{ 49 | prev->right = NULL; 50 | cur = cur->right; 51 | } 52 | } 53 | } 54 | 55 | return res; 56 | } 57 | }; 58 | 59 | int main() { 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /0020-Valid-Parentheses/java-0020/src/Solution.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/valid-parentheses/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | import java.util.Stack; 6 | 7 | // Using Stack 8 | // Time Complexity: O(n) 9 | // Space Complexity: O(n) 10 | public class Solution { 11 | 12 | public boolean isValid(String s) { 13 | 14 | Stack stack = new Stack(); 15 | for( int i = 0 ; i < s.length() ; i ++ ) 16 | if( s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') 17 | stack.push(s.charAt(i)); 18 | else{ 19 | 20 | if( stack.size() == 0 ) 21 | return false; 22 | 23 | Character c = stack.pop(); 24 | 25 | Character match; 26 | if( s.charAt(i) == ')' ) 27 | match = '('; 28 | else if( s.charAt(i) == ']' ) 29 | match = '['; 30 | else{ 31 | assert s.charAt(i) == '}'; 32 | match = '{'; 33 | } 34 | 35 | if(c != match) 36 | return false; 37 | } 38 | 39 | if( stack.size() != 0 ) 40 | return false; 41 | 42 | return true; 43 | } 44 | 45 | private static void printBool(boolean b){ 46 | System.out.println(b ? "True" : "False"); 47 | } 48 | 49 | public static void main(String[] args) { 50 | 51 | printBool((new Solution()).isValid("()")); 52 | printBool((new Solution()).isValid("()[]{}")); 53 | printBool((new Solution()).isValid("(]")); 54 | printBool((new Solution()).isValid("([)]")); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/cpp-0144/main4.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | // Another Classic Non-Recursive algorithm for preorder traversal 21 | // Time Complexity: O(n), n is the node number in the tree 22 | // Space Complexity: O(h), h is the height of the tree 23 | class Solution { 24 | 25 | public: 26 | vector preorderTraversal(TreeNode* root) { 27 | 28 | vector res; 29 | if(root == NULL) 30 | return res; 31 | 32 | stack stack; 33 | TreeNode* cur = root; 34 | while(cur != NULL || !stack.empty()){ 35 | while(cur != NULL){ 36 | res.push_back(cur->val); 37 | stack.push(cur); 38 | cur = cur->left; 39 | } 40 | 41 | cur = stack.top(); 42 | stack.pop(); 43 | cur = cur->right; 44 | } 45 | return res; 46 | } 47 | }; 48 | 49 | 50 | void print_vec(const vector& vec){ 51 | for(int e: vec) 52 | cout << e << " "; 53 | cout << endl; 54 | } 55 | 56 | int main() { 57 | 58 | TreeNode* root = new TreeNode(1); 59 | root->right = new TreeNode(2); 60 | root->right->left = new TreeNode(3); 61 | vector res = Solution().preorderTraversal(root); 62 | print_vec(res); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | /// Definition for a binary tree node. 11 | struct TreeNode { 12 | int val; 13 | TreeNode *left; 14 | TreeNode *right; 15 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 16 | }; 17 | 18 | 19 | // My Non-Recursive 20 | // Time Complexity: O(n), n is the node number in the tree 21 | // Space Complexity: O(h), h is the height of the tree 22 | class Solution { 23 | 24 | private: 25 | struct Command{ 26 | string s; // go, print 27 | TreeNode* node; 28 | Command(string s, TreeNode* node): s(s), node(node){} 29 | }; 30 | 31 | public: 32 | vector postorderTraversal(TreeNode* root) { 33 | 34 | vector res; 35 | if(root == NULL) 36 | return res; 37 | 38 | stack stack; 39 | stack.push(Command("go", root) ); 40 | while(!stack.empty()){ 41 | Command command = stack.top(); 42 | stack.pop(); 43 | 44 | if(command.s == "print") 45 | res.push_back(command.node->val); 46 | else{ 47 | assert(command.s == "go"); 48 | stack.push(Command("print", command.node)); 49 | if(command.node->right) 50 | stack.push(Command("go",command.node->right)); 51 | if(command.node->left) 52 | stack.push(Command("go",command.node->left)); 53 | } 54 | } 55 | return res; 56 | } 57 | }; 58 | 59 | int main() { 60 | 61 | return 0; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /0454-4Sum-II/cpp-0454/main2.cpp: -------------------------------------------------------------------------------- 1 | /// https://leetcode.com/problems/4sum-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | /// Another Way to use Hash Map 14 | /// Time Complexity: O(n^2) 15 | /// Space Complexity: O(n^2) 16 | class Solution { 17 | public: 18 | int fourSumCount(vector& A, vector& B, vector& C, vector& D) { 19 | 20 | unordered_map hashtable1; 21 | unordered_map hashtable2; 22 | 23 | for(int i = 0 ; i < A.size() ; i ++) 24 | for(int j = 0 ; j < B.size() ; j ++) 25 | hashtable1[A[i]+B[j]] += 1; 26 | 27 | for(int i = 0 ; i < C.size() ; i ++) 28 | for(int j = 0 ; j < D.size() ; j ++) 29 | hashtable2[C[i]+D[j]] += 1; 30 | 31 | int res = 0; 32 | for(unordered_map::iterator iter = hashtable1.begin() ; iter != hashtable1.end() ; iter ++) 33 | if(hashtable2.find(-(iter->first)) != hashtable2.end()) 34 | res += iter->second * hashtable2[-(iter->first)]; 35 | 36 | return res; 37 | } 38 | }; 39 | 40 | int main() { 41 | 42 | int a[] = {1, 2}; 43 | int b[] = {-2, -1}; 44 | int c[] = {-1, 2}; 45 | int d[] = {0, 2}; 46 | vector a_vec = vector(a, a + sizeof(a)/sizeof(int)); 47 | vector b_vec = vector(b, b + sizeof(b)/sizeof(int)); 48 | vector c_vec = vector(c, c + sizeof(c)/sizeof(int)); 49 | vector d_vec = vector(d, d + sizeof(d)/sizeof(int)); 50 | 51 | cout << Solution().fourSumCount(a_vec, b_vec, c_vec, d_vec) << endl; 52 | 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /0094-Binary-Tree-Inorder-Traversal/cpp-0094/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | 21 | // My Non-Recursive 22 | // Time Complexity: O(n), n is the node number in the tree 23 | // Space Complexity: O(h), h is the height of the tree 24 | class Solution { 25 | 26 | private: 27 | struct Command{ 28 | string s; // go, print 29 | TreeNode* node; 30 | Command(string s, TreeNode* node): s(s), node(node){} 31 | }; 32 | 33 | public: 34 | vector inorderTraversal(TreeNode* root) { 35 | 36 | vector res; 37 | if( root == NULL ) 38 | return res; 39 | 40 | stack stack; 41 | stack.push(Command("go", root)); 42 | while( !stack.empty() ){ 43 | Command command = stack.top(); 44 | stack.pop(); 45 | 46 | if(command.s == "print") 47 | res.push_back(command.node->val); 48 | else{ 49 | assert(command.s == "go"); 50 | if(command.node->right) 51 | stack.push(Command("go",command.node->right)); 52 | stack.push(Command("print", command.node)); 53 | if(command.node->left) 54 | stack.push(Command("go",command.node->left)); 55 | } 56 | } 57 | return res; 58 | } 59 | }; 60 | 61 | int main() { 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-10-16 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | /// BFS 21 | /// No need to store level information in the queue :-) 22 | /// 23 | /// Time Complexity: O(n), where n is the number of nodes in the tree 24 | /// Space Complexity: O(n) 25 | class Solution { 26 | public: 27 | vector> levelOrder(TreeNode* root) { 28 | 29 | vector> res; 30 | if(root == NULL) 31 | return res; 32 | 33 | queue q; 34 | q.push(root); 35 | int level_num = 1; 36 | 37 | while(!q.empty()){ 38 | 39 | int new_level_num = 0; 40 | vector level; 41 | for(int i = 0; i < level_num; i ++){ 42 | TreeNode* node = q.front(); 43 | q.pop(); 44 | level.push_back(node->val); 45 | 46 | if(node->left){ 47 | q.push(node->left); 48 | new_level_num ++; 49 | } 50 | if(node->right){ 51 | q.push(node->right); 52 | new_level_num ++; 53 | } 54 | } 55 | 56 | res.push_back(level); 57 | level_num = new_level_num; 58 | } 59 | 60 | return res; 61 | } 62 | }; 63 | 64 | 65 | int main() { 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/cpp-0144/main5.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | // Another Classic Non-Recursive algorithm for preorder traversal 21 | // Time Complexity: O(n), n is the node number in the tree 22 | // Space Complexity: O(h), h is the height of the tree 23 | class Solution { 24 | 25 | public: 26 | vector preorderTraversal(TreeNode* root) { 27 | 28 | vector res; 29 | if(root == NULL) 30 | return res; 31 | 32 | stack stack; 33 | TreeNode* cur = root; 34 | while(cur != NULL || !stack.empty()){ 35 | if(cur != NULL){ 36 | res.push_back(cur->val); 37 | stack.push(cur); 38 | cur = cur->left; 39 | } 40 | else{ 41 | cur = stack.top(); 42 | stack.pop(); 43 | cur = cur->right; 44 | } 45 | } 46 | return res; 47 | } 48 | }; 49 | 50 | 51 | void print_vec(const vector& vec){ 52 | for(int e: vec) 53 | cout << e << " "; 54 | cout << endl; 55 | } 56 | 57 | int main() { 58 | 59 | TreeNode* root = new TreeNode(1); 60 | root->right = new TreeNode(2); 61 | root->right->left = new TreeNode(3); 62 | vector res = Solution().preorderTraversal(root); 63 | print_vec(res); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /0144-Binary-Tree-Preorder-Traversal/cpp-0144/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-17 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | /// Definition for a binary tree node. 13 | struct TreeNode { 14 | int val; 15 | TreeNode *left; 16 | TreeNode *right; 17 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 18 | }; 19 | 20 | // My Non-Recursive 21 | // Time Complexity: O(n), n is the node number in the tree 22 | // Space Complexity: O(h), h is the height of the tree 23 | class Solution { 24 | 25 | private: 26 | struct Command{ 27 | string s; // go, print 28 | TreeNode* node; 29 | Command(string s, TreeNode* node): s(s), node(node){} 30 | }; 31 | 32 | public: 33 | vector preorderTraversal(TreeNode* root) { 34 | 35 | vector res; 36 | if(root == NULL) 37 | return res; 38 | 39 | stack stack; 40 | stack.push(Command("go", root)); 41 | while(!stack.empty()){ 42 | Command command = stack.top(); 43 | stack.pop(); 44 | 45 | if(command.s == "print") 46 | res.push_back(command.node->val); 47 | else{ 48 | assert(command.s == "go"); 49 | if(command.node->right) 50 | stack.push(Command("go",command.node->right)); 51 | if(command.node->left) 52 | stack.push(Command("go",command.node->left)); 53 | stack.push(Command("print", command.node)); 54 | } 55 | } 56 | return res; 57 | } 58 | }; 59 | 60 | int main() { 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/main3.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-30 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | 20 | // Non-Recursive 21 | // Using a tag to record whether the node has been visited 22 | // 23 | // Time Complexity: O(n), n is the node number in the tree 24 | // Space Complexity: O(h), h is the height of the tree 25 | class Solution { 26 | 27 | private: 28 | struct TagNode{ 29 | TreeNode* node; 30 | bool isFirst; 31 | TagNode(TreeNode* node): node(node), isFirst(false){} 32 | }; 33 | 34 | public: 35 | vector postorderTraversal(TreeNode* root) { 36 | 37 | vector res; 38 | if(root == NULL) 39 | return res; 40 | 41 | stack stack; 42 | TreeNode* cur = root; 43 | while(cur != NULL || !stack.empty()){ 44 | 45 | while(cur != NULL){ 46 | stack.push(TagNode(cur)); 47 | cur = cur->left; 48 | } 49 | 50 | TagNode tagNode = stack.top(); 51 | stack.pop(); 52 | cur = tagNode.node; 53 | if(tagNode.isFirst == false){ 54 | tagNode.isFirst = true; 55 | stack.push(tagNode); 56 | cur = cur->right; 57 | } 58 | else{ 59 | res.push_back(cur->val); 60 | cur = NULL; 61 | }; 62 | } 63 | return res; 64 | } 65 | }; 66 | 67 | int main() { 68 | 69 | return 0; 70 | } 71 | 72 | -------------------------------------------------------------------------------- /0454-4Sum-II/java-0454/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// https://leetcode.com/problems/4sum-ii/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | import java.util.HashMap; 6 | 7 | /// Another Way to use Hash Map 8 | /// Time Complexity: O(n^2) 9 | /// Space Complexity: O(n^2) 10 | public class Solution2 { 11 | 12 | public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { 13 | 14 | if(A == null || B == null || C == null || D == null) 15 | throw new IllegalArgumentException("Illegal argument"); 16 | 17 | HashMap mapAB = new HashMap(); 18 | for(int i = 0 ; i < A.length ; i ++) 19 | for(int j = 0 ; j < B.length ; j ++){ 20 | int sum = A[i] + B[j]; 21 | if(mapAB.containsKey(sum)) 22 | mapAB.put(sum, mapAB.get(sum) + 1); 23 | else 24 | mapAB.put(sum, 1); 25 | } 26 | 27 | HashMap mapCD = new HashMap(); 28 | for(int i = 0 ; i < C.length ; i ++) 29 | for(int j = 0 ; j < D.length ; j ++){ 30 | int sum = C[i] + D[j]; 31 | if(mapCD.containsKey(sum)) 32 | mapCD.put(sum, mapCD.get(sum) + 1); 33 | else 34 | mapCD.put(sum, 1); 35 | } 36 | 37 | int res = 0; 38 | for(Integer sumab: mapAB.keySet()){ 39 | if(mapCD.containsKey(-sumab)) 40 | res += mapAB.get(sumab) * mapCD.get(-sumab); 41 | } 42 | 43 | return res; 44 | } 45 | 46 | public static void main(String[] args) { 47 | 48 | int[] a = {1, 2}; 49 | int[] b = {-2, -1}; 50 | int[] c = {-1, 2}; 51 | int[] d = {0, 2}; 52 | System.out.println((new Solution2()).fourSumCount(a, b, c, d)); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/main7.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-31 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | 20 | // Classic Non-Recursive 21 | // Using a pre pointer to record the last visted node 22 | // 23 | // Time Complexity: O(n) 24 | // Space Complexity: O(h) 25 | class Solution { 26 | 27 | public: 28 | vector postorderTraversal(TreeNode* root) { 29 | 30 | vector res; 31 | if(root == NULL) 32 | return res; 33 | 34 | stack stack; 35 | TreeNode* pre = NULL; 36 | TreeNode* cur = root; 37 | 38 | while(cur != NULL || !stack.empty()){ 39 | 40 | while(cur != NULL){ 41 | stack.push(cur); 42 | cur = cur->left; 43 | } 44 | 45 | cur = stack.top(); 46 | stack.pop(); 47 | 48 | if(cur->right == NULL || pre == cur->right){ 49 | res.push_back(cur->val); 50 | pre = cur; 51 | cur = NULL; 52 | } 53 | else{ 54 | stack.push(cur); 55 | cur = cur->right; 56 | } 57 | } 58 | 59 | return res; 60 | } 61 | }; 62 | 63 | 64 | void print_vec(const vector& vec){ 65 | for(int e: vec) 66 | cout << e << " "; 67 | cout << endl; 68 | } 69 | 70 | int main() { 71 | 72 | TreeNode* root = new TreeNode(1); 73 | root->right = new TreeNode(2); 74 | root->right->left = new TreeNode(3); 75 | print_vec(Solution().postorderTraversal(root)); 76 | 77 | return 0; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution9.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-31 4 | 5 | import java.util.ArrayList; 6 | import java.util.Collections; 7 | import java.util.List; 8 | import java.util.Stack; 9 | 10 | // Morris PostOrder Traversal 11 | // 12 | // Time Complexity: O(n) 13 | // Space Complexity: O(1) 14 | public class Solution9 { 15 | 16 | public List postorderTraversal(TreeNode root) { 17 | 18 | ArrayList res = new ArrayList(); 19 | if(root == null) 20 | return res; 21 | 22 | TreeNode dummyRoot = new TreeNode(-1); 23 | dummyRoot.left = root; 24 | 25 | TreeNode cur = dummyRoot; 26 | while(cur != null){ 27 | if(cur.left == null) 28 | cur = cur.right; 29 | else{ 30 | TreeNode pre = cur.left; 31 | while(pre.right != null && pre.right != cur) 32 | pre = pre.right; 33 | 34 | if(pre.right == null){ 35 | pre.right = cur; 36 | cur = cur.left; 37 | } 38 | else{ 39 | pre.right = null; 40 | reverseTraversal(cur.left, res); 41 | cur = cur.right; 42 | } 43 | } 44 | } 45 | return res; 46 | } 47 | 48 | private void reverseTraversal(TreeNode node, ArrayList res){ 49 | int start = res.size(); 50 | while(node != null){ 51 | res.add(node.val); 52 | node = node.right; 53 | } 54 | 55 | int i = start, j = res.size() - 1; 56 | while(i < j){ 57 | Integer t = res.get(i); 58 | res.set(i, res.get(j)); 59 | res.set(j, t); 60 | 61 | i ++; 62 | j --; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/main8.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-31 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | 20 | // Classic Non-Recursive 21 | // Using a pre pointer to record the last visted node 22 | // 23 | // Time Complexity: O(n) 24 | // Space Complexity: O(h) 25 | class Solution { 26 | 27 | public: 28 | vector postorderTraversal(TreeNode* root) { 29 | 30 | vector res; 31 | if(root == NULL) 32 | return res; 33 | 34 | stack stack; 35 | TreeNode* pre = NULL; 36 | TreeNode* cur = root; 37 | 38 | while(cur != NULL || !stack.empty()){ 39 | if(cur != NULL){ 40 | stack.push(cur); 41 | cur = cur->left; 42 | } 43 | else{ 44 | cur = stack.top(); 45 | stack.pop(); 46 | 47 | if(cur->right == NULL || pre == cur->right){ 48 | res.push_back(cur->val); 49 | pre = cur; 50 | cur = NULL; 51 | } 52 | else{ 53 | stack.push(cur); 54 | cur = cur->right; 55 | } 56 | } 57 | } 58 | 59 | return res; 60 | } 61 | }; 62 | 63 | 64 | void print_vec(const vector& vec){ 65 | for(int e: vec) 66 | cout << e << " "; 67 | cout << endl; 68 | } 69 | 70 | int main() { 71 | 72 | TreeNode* root = new TreeNode(1); 73 | root->right = new TreeNode(2); 74 | root->right->left = new TreeNode(3); 75 | print_vec(Solution().postorderTraversal(root)); 76 | 77 | return 0; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/main6.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-31 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | 20 | // Non-Recursive 21 | // Using a pre pointer to record the last visted node 22 | // 23 | // Time Complexity: O(n) 24 | // Space Complexity: O(h) 25 | class Solution { 26 | 27 | public: 28 | vector postorderTraversal(TreeNode* root) { 29 | 30 | vector res; 31 | if(root == NULL) 32 | return res; 33 | 34 | stack stack; 35 | TreeNode* pre = NULL; 36 | 37 | stack.push(root); 38 | while(!stack.empty()){ 39 | 40 | TreeNode* node = stack.top(); 41 | stack.pop(); 42 | if((node->left == NULL && node->right == NULL) || 43 | (pre != NULL && pre == node->left && node->right == NULL) || 44 | (pre != NULL && pre == node->right)){ 45 | res.push_back(node->val); 46 | pre = node; 47 | } 48 | else{ 49 | stack.push(node); 50 | 51 | if(node->right != NULL) 52 | stack.push(node->right); 53 | if(node->left != NULL) 54 | stack.push(node->left); 55 | } 56 | } 57 | 58 | return res; 59 | } 60 | }; 61 | 62 | 63 | void print_vec(const vector& vec){ 64 | for(int e: vec) 65 | cout << e << " "; 66 | cout << endl; 67 | } 68 | 69 | int main() { 70 | 71 | TreeNode* root = new TreeNode(1); 72 | root->right = new TreeNode(2); 73 | root->right->left = new TreeNode(3); 74 | print_vec(Solution().postorderTraversal(root)); 75 | 76 | return 0; 77 | } 78 | 79 | -------------------------------------------------------------------------------- /0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // Binary Search 12 | // Time Complexity: O(nlogn) 13 | // Space Complexity: O(1) 14 | class Solution { 15 | public: 16 | vector twoSum(vector& numbers, int target) { 17 | 18 | assert(numbers.size() >= 2); 19 | // assert(isSorted(numbers)); 20 | 21 | for(int i = 0 ; i < numbers.size() - 1 ; i ++){ 22 | int j = binarySearch(numbers, i+1, numbers.size()-1, target - numbers[i]); 23 | if(j != -1){ 24 | int res[2] = {i+1, j+1}; 25 | return vector(res, res+2); 26 | } 27 | } 28 | 29 | throw invalid_argument("the input has no solution"); 30 | } 31 | 32 | private: 33 | int binarySearch(const vector &nums, int l, int r, int target){ 34 | 35 | assert(l >= 0 && l < nums.size()); 36 | assert(r >= 0 && r < nums.size()); 37 | 38 | while(l <= r){ 39 | 40 | int mid = l + (r - l)/2; 41 | if(nums[mid] == target) 42 | return mid; 43 | if(target > nums[mid]) 44 | l = mid + 1; 45 | else 46 | r = mid - 1; 47 | } 48 | 49 | return -1; 50 | } 51 | 52 | bool isSorted(const vector& numbers){ 53 | for(int i = 1 ; i < numbers.size() ; i ++) 54 | if(numbers[i] < numbers[i-1]) 55 | return false; 56 | return true; 57 | } 58 | }; 59 | 60 | void printVec(const vector& vec){ 61 | for(int e: vec) 62 | cout << e << " "; 63 | cout << endl; 64 | } 65 | 66 | int main() { 67 | 68 | int nums[] = {2, 7, 11, 15}; 69 | vector vec(nums, nums + sizeof(nums) / sizeof(int)); 70 | int target = 9; 71 | printVec(Solution().twoSum(vec, target)); 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution2.java: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-13 4 | 5 | // Binary Search 6 | // Time Complexity: O(nlogn) 7 | // Space Complexity: O(1) 8 | public class Solution2 { 9 | 10 | public int[] twoSum(int[] numbers, int target) { 11 | 12 | if(numbers.length < 2 /*|| !isSorted(numbers)*/) 13 | throw new IllegalArgumentException("Illegal argument numbers"); 14 | 15 | for(int i = 0 ; i < numbers.length - 1 ; i ++){ 16 | int j = binarySearch(numbers, i+1, numbers.length-1, target - numbers[i]); 17 | if(j != -1){ 18 | int[] res = {i+1, j+1}; 19 | return res; 20 | } 21 | } 22 | 23 | throw new IllegalStateException("The input has no solution"); 24 | } 25 | 26 | private int binarySearch(int[] nums, int l, int r, int target){ 27 | 28 | if(l < 0 || l > nums.length) 29 | throw new IllegalArgumentException("l is out of bound"); 30 | 31 | if(r < 0 || r > nums.length) 32 | throw new IllegalArgumentException("r is out of bound"); 33 | 34 | while(l <= r){ 35 | int mid = l + (r - l)/2; 36 | if(nums[mid] == target) 37 | return mid; 38 | if(target > nums[mid]) 39 | l = mid + 1; 40 | else 41 | r = mid - 1; 42 | } 43 | 44 | return -1; 45 | } 46 | 47 | private boolean isSorted(int[] numbers){ 48 | for(int i = 1 ; i < numbers.length ; i ++) 49 | if(numbers[i] < numbers[i-1]) 50 | return false; 51 | return true; 52 | } 53 | 54 | private static void printArr(int[] nums){ 55 | for(int num: nums) 56 | System.out.print(num + " "); 57 | System.out.println(); 58 | } 59 | 60 | public static void main(String[] args) { 61 | 62 | int[] nums = {2, 7, 11, 15}; 63 | int target = 9; 64 | printArr((new Solution2()).twoSum(nums, target)); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /0145-Binary-Tree-Postorder-Traversal/cpp-0145/main9.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2018-05-31 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /// Definition for a binary tree node. 12 | struct TreeNode { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | 20 | // Morris PostOrder Traversal 21 | // 22 | // Time Complexity: O(n) 23 | // Space Complexity: O(1) 24 | class Solution { 25 | 26 | public: 27 | vector postorderTraversal(TreeNode* root) { 28 | 29 | vector res; 30 | if(root == NULL) 31 | return res; 32 | 33 | TreeNode* dummyRoot = new TreeNode(-1); 34 | dummyRoot->left = root; 35 | 36 | TreeNode* cur = dummyRoot; 37 | while(cur != NULL){ 38 | if(cur->left == NULL) 39 | cur = cur->right; 40 | else{ 41 | TreeNode* prev = cur->left; 42 | while(prev->right != NULL && prev->right != cur) 43 | prev = prev->right; 44 | 45 | if(prev->right == NULL){ 46 | prev->right = cur; 47 | cur = cur->left; 48 | } 49 | else{ 50 | prev->right = NULL; 51 | reverseTraversal(cur->left, res); 52 | cur = cur->right; 53 | } 54 | } 55 | } 56 | delete dummyRoot; 57 | 58 | return res; 59 | } 60 | 61 | private: 62 | void reverseTraversal(TreeNode* node, vector& res){ 63 | 64 | int start = res.size(); 65 | while(node != NULL){ 66 | res.push_back(node->val); 67 | node = node->right; 68 | } 69 | reverse(res.begin() + start, res.end()); 70 | } 71 | }; 72 | 73 | 74 | void print_vec(const vector& vec){ 75 | for(int e: vec) 76 | cout << e << " "; 77 | cout << endl; 78 | } 79 | 80 | int main() { 81 | 82 | TreeNode* root = new TreeNode(1); 83 | root->right = new TreeNode(2); 84 | root->right->left = new TreeNode(3); 85 | print_vec(Solution().postorderTraversal(root)); 86 | 87 | return 0; 88 | } 89 | 90 | -------------------------------------------------------------------------------- /0237-Delete-Node-in-a-Linked-List/cpp-0237/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/delete-node-in-a-linked-list/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | 11 | /// Definition for singly-linked list. 12 | struct ListNode { 13 | int val; 14 | ListNode *next; 15 | ListNode(int x) : val(x), next(NULL) {} 16 | }; 17 | 18 | 19 | /// LinkedList Test Helper Functions 20 | ListNode* createLinkedList(int arr[], int n){ 21 | 22 | if(n == 0) 23 | return NULL; 24 | 25 | ListNode* head = new ListNode(arr[0]); 26 | ListNode* curNode = head; 27 | for(int i = 1 ; i < n ; i ++){ 28 | curNode->next = new ListNode(arr[i]); 29 | curNode = curNode->next; 30 | } 31 | 32 | return head; 33 | } 34 | 35 | void printLinkedList(ListNode* head){ 36 | 37 | ListNode* curNode = head; 38 | while(curNode != NULL){ 39 | cout << curNode->val << " -> "; 40 | curNode = curNode->next; 41 | } 42 | 43 | cout << "NULL" << endl; 44 | 45 | return; 46 | } 47 | 48 | void deleteLinkedList(ListNode* head){ 49 | 50 | ListNode* curNode = head; 51 | while(curNode != NULL){ 52 | ListNode* delNode = curNode; 53 | curNode = curNode->next; 54 | delete delNode; 55 | } 56 | 57 | return; 58 | } 59 | 60 | ListNode* findNode(ListNode* head, int x){ 61 | 62 | ListNode* curNode = head; 63 | while(curNode != NULL){ 64 | if(curNode->val == x) 65 | return curNode; 66 | curNode = curNode->next; 67 | } 68 | return NULL; 69 | } 70 | 71 | // 时间复杂度: O(1) 72 | // 空间复杂度: O(1) 73 | class Solution { 74 | public: 75 | void deleteNode(ListNode* node) { 76 | 77 | assert(node != NULL && node->next != NULL); 78 | 79 | node->val = node->next->val; 80 | ListNode* delNode = node->next; 81 | node->next = delNode->next; 82 | 83 | delete delNode; 84 | 85 | return; 86 | } 87 | }; 88 | 89 | int main() { 90 | 91 | int arr[] = {1, 2, 3, 4}; 92 | int n = sizeof(arr)/sizeof(int); 93 | 94 | ListNode* head = createLinkedList(arr, n); 95 | printLinkedList(head); 96 | 97 | ListNode* node2 = findNode(head, 2); 98 | Solution().deleteNode(node2); 99 | printLinkedList(head); 100 | 101 | deleteLinkedList(head); 102 | 103 | return 0; 104 | } -------------------------------------------------------------------------------- /0024-Swap-Nodes-in-Pairs/cpp-0024/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | /// Definition for singly-linked list. 10 | struct ListNode { 11 | int val; 12 | ListNode *next; 13 | ListNode(int x) : val(x), next(NULL) {} 14 | }; 15 | 16 | /// LinkedList Test Helper Functions 17 | ListNode* createLinkedList(int arr[], int n){ 18 | 19 | if(n == 0) 20 | return NULL; 21 | 22 | ListNode* head = new ListNode(arr[0]); 23 | ListNode* curNode = head; 24 | for(int i = 1 ; i < n ; i ++){ 25 | curNode->next = new ListNode(arr[i]); 26 | curNode = curNode->next; 27 | } 28 | 29 | return head; 30 | } 31 | 32 | void printLinkedList(ListNode* head){ 33 | 34 | if(head == NULL){ 35 | cout<<"NULL"<val; 42 | if(curNode->next != NULL) 43 | cout << " -> "; 44 | curNode = curNode->next; 45 | } 46 | 47 | cout << endl; 48 | 49 | return; 50 | } 51 | 52 | void deleteLinkedList(ListNode* head){ 53 | 54 | ListNode* curNode = head; 55 | while(curNode != NULL){ 56 | ListNode* delNode = curNode; 57 | curNode = curNode->next; 58 | delete delNode; 59 | } 60 | 61 | return; 62 | } 63 | 64 | // Time Complexity: O(n) 65 | // Space Complexity: O(1) 66 | class Solution { 67 | public: 68 | ListNode* swapPairs(ListNode* head) { 69 | 70 | ListNode* dummyHead = new ListNode(0); 71 | dummyHead->next = head; 72 | 73 | ListNode* p = dummyHead; 74 | while(p->next && p->next->next){ 75 | ListNode* node1 = p->next; 76 | ListNode* node2 = node1->next; 77 | ListNode* next = node2->next; 78 | node2->next = node1; 79 | node1->next = next; 80 | p->next = node2; 81 | p = node1; 82 | } 83 | 84 | ListNode* retHead = dummyHead->next; 85 | delete dummyHead; 86 | 87 | return retHead; 88 | } 89 | }; 90 | 91 | int main() { 92 | 93 | int arr[] = {1, 2, 3, 4}; 94 | int n = sizeof(arr) / sizeof(int); 95 | 96 | ListNode* head = createLinkedList(arr, n); 97 | printLinkedList(head); 98 | 99 | head = Solution().swapPairs(head); 100 | printLinkedList(head); 101 | 102 | deleteLinkedList(head); 103 | 104 | return 0; 105 | } -------------------------------------------------------------------------------- /0203-Remove-Linked-List-Elements/cpp-0203/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/remove-linked-list-elements/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | ///Definition for singly-linked list. 10 | struct ListNode { 11 | int val; 12 | ListNode *next; 13 | ListNode(int x) : val(x), next(NULL) {} 14 | }; 15 | 16 | /// LinkedList Test Helper Functions 17 | ListNode* createLinkedList(int arr[], int n){ 18 | 19 | if(n == 0) 20 | return NULL; 21 | 22 | ListNode* head = new ListNode(arr[0]); 23 | ListNode* curNode = head; 24 | for(int i = 1 ; i < n ; i ++){ 25 | curNode->next = new ListNode(arr[i]); 26 | curNode = curNode->next; 27 | } 28 | 29 | return head; 30 | } 31 | 32 | void printLinkedList(ListNode* head){ 33 | 34 | if(head == NULL){ 35 | cout << "NULL" << endl; 36 | return; 37 | } 38 | 39 | ListNode* curNode = head; 40 | while(curNode != NULL){ 41 | cout << curNode->val; 42 | if(curNode->next != NULL) 43 | cout << " -> "; 44 | curNode = curNode->next; 45 | } 46 | 47 | cout << endl; 48 | 49 | return; 50 | } 51 | 52 | void deleteLinkedList(ListNode* head){ 53 | 54 | ListNode* curNode = head; 55 | while(curNode != NULL){ 56 | ListNode* delNode = curNode; 57 | curNode = curNode->next; 58 | delete delNode; 59 | } 60 | 61 | return; 62 | } 63 | 64 | 65 | /// Linear Scan with dummy head 66 | /// Time Complexity: O(n) 67 | /// Space Complexity: O(1) 68 | class Solution { 69 | public: 70 | ListNode* removeElements(ListNode* head, int val) { 71 | 72 | ListNode* dummyHead = new ListNode(0); 73 | dummyHead->next = head; 74 | 75 | ListNode* cur = dummyHead; 76 | while(cur->next != NULL){ 77 | if(cur->next->val == val){ 78 | ListNode* delNode = cur->next; 79 | cur->next = delNode->next; 80 | delete delNode; 81 | } 82 | else 83 | cur = cur->next; 84 | } 85 | 86 | ListNode* retNode = dummyHead->next; 87 | delete dummyHead; 88 | 89 | return retNode; 90 | } 91 | }; 92 | 93 | 94 | int main() { 95 | 96 | int arr[] = {1, 2, 6, 3, 4, 5, 6}; 97 | int n = sizeof(arr) / sizeof(int); 98 | 99 | ListNode* head = createLinkedList(arr, n); 100 | printLinkedList(head); 101 | 102 | Solution().removeElements(head, 6); 103 | printLinkedList(head); 104 | 105 | deleteLinkedList(head); 106 | 107 | return 0; 108 | } -------------------------------------------------------------------------------- /0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main2.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | ///Definition for singly-linked list. 11 | struct ListNode { 12 | int val; 13 | ListNode *next; 14 | ListNode(int x) : val(x), next(NULL) {} 15 | }; 16 | 17 | /// LinkedList Test Helper Functions 18 | ListNode* createLinkedList(int arr[], int n){ 19 | 20 | if(n == 0) 21 | return NULL; 22 | 23 | ListNode* head = new ListNode(arr[0]); 24 | ListNode* curNode = head; 25 | for(int i = 1 ; i < n ; i ++){ 26 | curNode->next = new ListNode(arr[i]); 27 | curNode = curNode->next; 28 | } 29 | 30 | return head; 31 | } 32 | 33 | void printLinkedList(ListNode* head){ 34 | 35 | if( head == NULL ){ 36 | cout << "NULL" << endl; 37 | return; 38 | } 39 | 40 | ListNode* curNode = head; 41 | while(curNode != NULL){ 42 | cout << curNode->val; 43 | if( curNode->next != NULL ) 44 | cout << " -> "; 45 | curNode = curNode->next; 46 | } 47 | 48 | cout << endl; 49 | 50 | return; 51 | } 52 | 53 | void deleteLinkedList(ListNode* head){ 54 | 55 | ListNode* curNode = head; 56 | while(curNode != NULL){ 57 | ListNode* delNode = curNode; 58 | curNode = curNode->next; 59 | delete delNode; 60 | } 61 | 62 | return; 63 | } 64 | 65 | /// Two Pointers - One Pass Algorithm 66 | /// Time Complexity: O(n) 67 | /// Space Complexity: O(1) 68 | class Solution { 69 | public: 70 | ListNode* removeNthFromEnd(ListNode* head, int n) { 71 | 72 | ListNode* dummyHead = new ListNode(0); 73 | dummyHead->next = head; 74 | 75 | ListNode* p = dummyHead; 76 | ListNode* q = dummyHead; 77 | for(int i = 0 ; i < n + 1 ; i ++){ 78 | assert(q); 79 | q = q->next; 80 | } 81 | 82 | while(q){ 83 | p = p->next; 84 | q = q->next; 85 | } 86 | 87 | ListNode* delNode = p->next; 88 | p->next = delNode->next; 89 | delete delNode; 90 | 91 | ListNode* retNode = dummyHead->next; 92 | delete dummyHead; 93 | 94 | return retNode; 95 | } 96 | }; 97 | 98 | int main() { 99 | 100 | int arr[] = {1, 2, 3, 4, 5}; 101 | int n = sizeof(arr)/sizeof(int); 102 | 103 | ListNode* head = createLinkedList(arr, n); 104 | printLinkedList(head); 105 | 106 | head = Solution().removeNthFromEnd(head, 2); 107 | printLinkedList(head); 108 | 109 | deleteLinkedList(head); 110 | 111 | return 0; 112 | } -------------------------------------------------------------------------------- /0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main.cpp: -------------------------------------------------------------------------------- 1 | /// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2 | /// Author : liuyubobobo 3 | /// Time : 2017-11-15 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | ///Definition for singly-linked list. 11 | struct ListNode { 12 | int val; 13 | ListNode *next; 14 | ListNode(int x) : val(x), next(NULL) {} 15 | }; 16 | 17 | /// LinkedList Test Helper Functions 18 | ListNode* createLinkedList(int arr[], int n){ 19 | 20 | if(n == 0) 21 | return NULL; 22 | 23 | ListNode* head = new ListNode(arr[0]); 24 | ListNode* curNode = head; 25 | for(int i = 1 ; i < n ; i ++){ 26 | curNode->next = new ListNode(arr[i]); 27 | curNode = curNode->next; 28 | } 29 | 30 | return head; 31 | } 32 | 33 | void printLinkedList(ListNode* head){ 34 | 35 | if(head == NULL){ 36 | cout<<"NULL"<val; 43 | if(curNode->next != NULL) 44 | cout << " -> "; 45 | curNode = curNode->next; 46 | } 47 | 48 | cout << endl; 49 | 50 | return; 51 | } 52 | 53 | void deleteLinkedList(ListNode* head){ 54 | 55 | ListNode* curNode = head; 56 | while(curNode != NULL){ 57 | ListNode* delNode = curNode; 58 | curNode = curNode->next; 59 | delete delNode; 60 | } 61 | 62 | return; 63 | } 64 | 65 | /// Get the total length and remove the nth node 66 | /// Two Pass Algorithm 67 | /// 68 | /// Time Complexity: O(n) 69 | /// Space Complexity: O(1) 70 | class Solution { 71 | public: 72 | ListNode* removeNthFromEnd(ListNode* head, int n) { 73 | 74 | ListNode* dummyHead = new ListNode(0); 75 | dummyHead->next = head; 76 | 77 | int length = 0; 78 | for(ListNode* cur = dummyHead->next ; cur != NULL ; cur = cur->next) 79 | length ++; 80 | 81 | int k = length - n; 82 | assert(k >= 0); 83 | ListNode* cur = dummyHead; 84 | for(int i = 0 ; i < k ; i ++) 85 | cur = cur->next; 86 | 87 | ListNode* delNode = cur->next; 88 | cur->next = delNode->next; 89 | delete delNode; 90 | 91 | ListNode* retNode = dummyHead->next; 92 | delete dummyHead; 93 | return retNode; 94 | } 95 | }; 96 | 97 | int main() { 98 | 99 | int arr[] = {1, 2, 3, 4, 5}; 100 | int n = sizeof(arr)/sizeof(int); 101 | 102 | ListNode* head = createLinkedList(arr, n); 103 | printLinkedList(head); 104 | 105 | head = Solution().removeNthFromEnd(head, 2); 106 | printLinkedList(head); 107 | 108 | deleteLinkedList(head); 109 | 110 | return 0; 111 | } -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | ![LeetCode Animation All in One](https://upload-images.jianshu.io/upload_images/1940317-e837182a805cecce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 2 | 3 | [![Travis](https://img.shields.io/badge/language-C++-red.svg)](https://developer.apple.com/) 4 | [![Travis](https://img.shields.io/badge/language-Java-yellow.svg)](https://developer.apple.com/) 5 | 6 | 7 | [There is an English version of README here. just click it!](https://github.com/MisterBooo/LeetCodeAnimation/blob/master/README-En.md) 8 | 9 | 我会尽力将LeetCode上所有的题目都用动画的形式演示出来,计划用3到4年时间去完成它,期待与你见证这一天! 10 | 11 | 文章最新首发于微信公众号 **五分钟学算法** ,您可以关注获取最新的文章。 12 | 13 | 14 | 15 | ## Problems 16 | 17 | 18 | | ID | Problem | Article | Animation |Date| 19 | | --- | --- | :--- |:--- |:--- | 20 | | 000 |十大经典排序算法 | [十大经典排序算法动画与解析,看我就够了!(配代码完全版)](https://mp.weixin.qq.com/s/vn3KiV-ez79FmbZ36SX9lg) | ![归并排序.gif](https://upload-images.jianshu.io/upload_images/1940317-92f62b62af03e233.gif?imageMogr2/auto-orient/strip)|| 21 | | 001 |两数之和 | [每天一算:Two Sum](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483740&idx=1&sn=1950545589ea9b86ee65fbb6be1f4290&chksm=fa0e6eddcd79e7cb542b7d4dc1304eead516994315fa4f52b575230f0f022c9e0a88ede3714e&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161033.gif) || 22 | | 002 |两个数字相加 | [图解LeetCode第 2 号问题:两个数字相加](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484231&idx=2&sn=6a9eb4fd0619c822e4dede69b8d841c8&chksm=fa0e6cc6cd79e5d0c03fffcd65b665fed62db9dca9c97771898f388ea292ce806bfd6eb908b5&token=934487935&lang=zh_CN#rd) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092831.gif)|| 23 | | 003 |无重复字符的最长子串| [图解LeetCode第 3 号问题:无重复字符的最长子串](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484265&idx=2&sn=7f72afb341865923315bd51e1f50beff&chksm=fa0e6ce8cd79e5fe4be925fd5f01f59f59010c6c965fb3daefac79992593a6e58990c212e0bb&token=1412967663&lang=zh_CN#rd)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092855.gif)|| 24 | | 019| 删除链表的倒数第N个节点 |[每天一算:Remove Nth Node From End of List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483821&idx=1&sn=11ecccab76cd53163e9dedb75effeb93&chksm=fa0e6e2ccd79e73ae9137c0d91b3533df4ea4ead4ad081834b8d91ff364c0d55c350ddcfa6c4&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161058.gif)|| 25 | | 020|有效的括号 | [每天一算:Valid Parentheses](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483824&idx=1&sn=ab9362e125dc5e2b3ef1611cad9448c2&chksm=fa0e6e31cd79e727c6e1e0e3c467e193edb6ae841a41e5dc8eef39d0bf3141cc53f63b019cba&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161106.gif)|| 26 | | 024| 两两交换链表中的节点 | [每天一算:Swap Nodes in Pairs](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483773&idx=1&sn=db6cf272df968cd6571eb0bb50ecc721&chksm=fa0e6efccd79e7ea26810d335e6ece9ac23b8e3ac31be00dbd534018737ccb3ef9a00f22aff3&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161115.gif)|| 27 | | 026| 删除排序数组中的重复项| [图解LeetCode第 26 号问题:删除排序数组中的重复项](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484284&idx=2&sn=c8af62a82a62a21217d0f0b2b5891e4f&chksm=fa0e6cfdcd79e5ebe8726a61f93b834467d29b7d9e60a44feb990388f9e98605ac1e3f7e723d&token=762342620&lang=zh_CN#rd) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161124.gif)|| 28 | | 075| 颜色分类| [每天一算:Sort Colors](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483706&idx=1&sn=905f43c882a91b55fd169d812620f277&chksm=fa0e6ebbcd79e7ad8857b0dad9ad14dbaf17fe557ef56ba600cec26b2bb668df2e171431d74c&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161136.gif)|| 29 | | 086| 分隔链表| [每天一算:Partition List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483781&idx=1&sn=f31548ebbb2cf9ba56d979d3e51ddde2&chksm=fa0e6e04cd79e712d6cc7ff8e8b7631b7300ac0fa1a3e4c4e3b836de7a01fb5d0d6428a18bc4&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161147.gif)|| 30 | |092 |反转链表 II | [每天一算:Reverse Linked List II](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483820&idx=1&sn=113e87b55c8ac8e22e9db00673798118&chksm=fa0e6e2dcd79e73b5835a262599b935783de3317a453bc0ed8df9fa5d1532785a085ea663e59&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161156.gif)|| 31 | | 094|二叉树的中序遍历 | [每天一算:Binary Tree Inorder Traversal ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483853&idx=1&sn=94cd4b4ee8dc2268290a72334c6af57b&chksm=fa0e6e4ccd79e75a41a6b78397b80cdfccda332823874475b516f997f89e786488599fc5cc1e&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161208.gif)|| 32 | |102 |二叉树的层序遍历| [每天一算:Binary Tree Level Order Traveral](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483868&idx=1&sn=d50041789fcd13a75a2296f620b69d71&chksm=fa0e6e5dcd79e74b0030ac5129f10ec4ba87c98da63c5904affe9f06e06ecf28695c410d3ec7&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161232.gif)|| 33 | |103 | 二叉树的锯齿形层次遍历| [图解LeetCode第 103 号问题:二叉树的锯齿形层次遍历](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484290&idx=2&sn=c29c4eefcbe8954cca6b3c8491ebccf1&chksm=fa0e6c03cd79e515581905322a3a22a3f3d10d24ca668a9d5aaef00932f0237eeaeaf3199668&token=1840661183&lang=zh_CN#rd)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092922.gif)|| 34 | |107 | 二叉树的层次遍历 II | 每天一算: Binary Tree Level Order Traversal II|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092949.gif)|| 35 | |136 | 只出现一次的数字 | [一道让你拍案叫绝的算法题 ](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484505&idx=1&sn=4c1c056dd4852c3b4b1ead51c90a9b2d&chksm=fa0e6bd8cd79e2ce8188dcdd8843a5d071248906bfb8971c62d513dbd69b816acc191a78e4b2&token=487128715&lang=zh_CN#rd)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190116110804.gif)| 2019-01-16 | 36 | |144 | 二叉树的前序遍历| [每天一算:Binary Tree Preorder Traversal](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483843&idx=1&sn=994bf0d42dd9941a879a3a3ed500a4d6&chksm=fa0e6e42cd79e75472404eb5da7ee98f20d303efe230eb4f41efec57164630f555e7111e62ff&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181207112441.gif)|| 37 | | 145| 二叉树的后序遍历 | [每天一算:Binary Tree Postorder Traversal](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483861&idx=1&sn=91a32b22ab9bc7fd282b489c4d3006e0&chksm=fa0e6e54cd79e742e22a0b15d4eb55b933de19bb12d2de41fac19fc59da6c8e3cc65dea2120c&token=647916903&lang=zh_CN#rd) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181219084940.gif)|| 38 | | 146| LRU缓存机制 | LRU缓存机制 |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190125143756.gif)| 2019-01-25 Made by Jun chen| 39 | | 150| 逆波兰表达式求值 | [每天一算:Evaluate Reverse Polish Notation](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483834&idx=1&sn=27cbff99f10dfcdb56cb37c237d7f2bb&chksm=fa0e6e3bcd79e72dc430bf81aed9dde9bd01634239dcf7820d6befa881efd323d9d58d76d90d&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161304.gif)|| 40 | | 167| 两数之和 II - 输入有序数组 | [每天一算:Two Sum II ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483711&idx=1&sn=3afec74e9e9effa71dc0b22659e14b44&chksm=fa0e6ebecd79e7a84db7861c9b5dbccdc98aa9d9a6994dda49a37edeb729e8242ea6af8f20ad&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161314.gif)|| 41 | |199 | 二叉树的右视图 | 每天一算:Binary Tree Right Side View |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161328.gif)|| 42 | | 203| 移除链表元素 | [每天一算:Remove Linked List Elements](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483766&idx=1&sn=6721376a65680bf7cf9064cf7b1ae4ae&chksm=fa0e6ef7cd79e7e1665e60fe6ea3f2087bca518c1573bc4c4b9425573f98401bafc59542dca0&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161338.gif)|| 43 | |206 | 反转链表 | [每天一算: Reverse Linked List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483799&idx=1&sn=c2212c8963809e8d3392abeeb851dbfc&chksm=fa0e6e16cd79e7003c2d30b1a2bb4f23dc56df38e3efedd0ab2cfae291609280a832eabe67de&scene=21#wechat_redirect)|![](https://diycode.b0.upaiyun.com/photo/2018/0e0b3a4b1031c3bcf16bd1240ebc571a.gif)|| 44 | |209 | 长度最小的子数组 | 每天一算: Minimum Size Subarray Sum |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210093031.gif)|| 45 | | 219|存在重复元素 II |[每天一算:Contains Duplicate II ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483755&idx=1&sn=2501b6ca09c43eaa9fba71a9bd1f5253&chksm=fa0e6eeacd79e7fc192c0a23cf90d98fe6f2c35f9e4f2d0f937ccba45a58cf23a0a9c49d35d5&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161352.gif)|| 46 | |237| 删除链表中的节点 |每天一算:Delete Node in a Linked List|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161400.gif) || 47 | |279| 完全平方数 |[图解LeetCode第 279 号问题: 完全平方数](https://mp.weixin.qq.com/s/53AlHe29fJF1hEwj0cj7ZA)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190119213317.gif) | Made by 王琛 2019-01-19日| 48 | |283 |移动零 |[每天一算:Move Zeros](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483700&idx=1&sn=465f778d60e8560742feab5844d7cac5&chksm=fa0e6eb5cd79e7a357899d378edb532b498cd63e3ce9113f8ac74d397ce4b214ca5aa8198b7d&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161421.gif)|| 49 | |328 |奇偶链表 | [每天一算:Odd Even Linked List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483786&idx=1&sn=f7810950b34675e1c4420361faf5e361&chksm=fa0e6e0bcd79e71d2c6fc6a4a68b6ef7a17abc3dc9897548f8e44b51e9494f52c4cebbc4176e&scene=21#wechat_redirect) |![](https://diycode.b0.upaiyun.com/photo/2018/94e5c38540029690c93314b3d697caaf.gif)|| 50 | |344 | 反转字符串 |每天一算:Reverse String |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181211110918.gif)|| 51 | |349 | 两个数组的交集| [每天一算:Intersection of Two Arrays ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483726&idx=1&sn=a887f6b983058d97c183dd300832ecbb&chksm=fa0e6ecfcd79e7d985587b543622c85aadc83a4d7a074135e1356fb4a0ebfd07e7af13467906&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161441.gif)|| 52 | | 350| 两个数组的交集 II| [每天一算:Intersection of Two Arrays II ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483733&idx=1&sn=946bd6de3251437dd77b43ecab056c82&chksm=fa0e6ed4cd79e7c2a439b5f1853bf5154a3438ed282c7ba5e94948780c426a1f1492c0b201c4&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161729.gif)|| 53 | |445| 两数相加 II |[图解LeetCode第 445 号问题: 两数相加 II](https://mp.weixin.qq.com/s/z8_1dK7mw9gxfhhSZUBVgg)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190119213714.gif) | Made by 王琛 2019-01-19日| 54 | |447 | 回旋镖的数量 | [每日一算:Number of Boomerangs](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483747&idx=1&sn=7774eee0b252b311257134f6a52c4e2d&chksm=fa0e6ee2cd79e7f44858c46c3d04859ced9073dbb9de95ce7ee0bcc131e613862ddfd9a6f158&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161738.gif)|| 55 | |454 |四数相加 II | 每日一算:4Sum II|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161751.gif)|| 56 | |642 |设计搜索自动补全系统 | [图解 LeetCode 第 642 号问题:搜索自动完成系统](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484491&idx=1&sn=b329d90370d183b5a58bbf03f6a436ae&chksm=fa0e6bcacd79e2dc05bb5eaabd888561b82c37700b511e4971aa76ec42a630c0a35ef3e4721b&token=397665543&lang=zh_CN#rd)|动画是采用AE制作,内存太大,无法使用GIF播放,请前往[动画](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484491&idx=1&sn=b329d90370d183b5a58bbf03f6a436ae&chksm=fa0e6bcacd79e2dc05bb5eaabd888561b82c37700b511e4971aa76ec42a630c0a35ef3e4721b&token=397665543&lang=zh_CN#rd)观看,感谢 Jun Chen 大佬提供动画技术支持,笔芯。|| 57 | 58 | 59 | 如果文章链接无法点击,说明文章还未发布,敬请期待:) 60 | 61 | 为了让文章加载动画更快,录制动画时进行了压缩,有可能会模糊,如果你想获取上述文章的所有超清动画,请在公众号 **五分钟学算法** 内回复 **MP4** 进行获取。 62 | 63 | ## 代码来源声明 64 | 本仓库代码如无特殊说明,全部来源于此仓库 65 | 66 | [Play-Leetcode](https://github.com/liuyubobobo/Play-Leetcode) 67 | 68 | ## 补充 69 | 该仓库保持随时更新。 70 | 71 | 2018-12-29 说明: 72 | 73 | [《2019年LeetCodeAnimationd的更新计划》](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484375&idx=1&sn=5a5482d9863342650d8b43bb59171f7c&chksm=fa0e6c56cd79e540115e52500b80c8e72001c87ddceb7c0ae1de166fd283d632b960cde41aca&token=578760218&lang=zh_CN#rd) 74 | 75 | 2018-12-07 说明: 76 | 77 | 为了更好的做好LeetCode动画,笔者正在努力的学习更多的数据结构与算法。 78 | 79 | 笔者目前正在写数据结构的文章与动画,将《动画图解数据结构》系列文章写完后将继续更新此仓库。 80 | 81 | 邮箱:misterbigbooo@gmail.com 82 | 83 | 喜欢就star❤️一下吧! 84 | 85 | 86 | ## 和我交流 87 | | 二维码 | 说明 | 88 | | --- | --- | 89 | |![](https://diycode.b0.upaiyun.com/photo/2018/6ac660d9a4a36db44c404246ba858862.jpg) | 关注公众号 **五分钟学算法** 获取更多数据结构与动画相关的内容| 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /README-En.md: -------------------------------------------------------------------------------- 1 | ![LeetCode Animation All in One](https://upload-images.jianshu.io/upload_images/1940317-e837182a805cecce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 2 | 3 | [![Travis](https://img.shields.io/badge/language-C++-red.svg)](https://developer.apple.com/) 4 | [![Travis](https://img.shields.io/badge/language-Java-yellow.svg)](https://developer.apple.com/) 5 | 6 | [这里有中文版本的README,点击它!](https://github.com/MisterBooo/LeetCodeAnimation/blob/master/Readme.md) 7 | 8 | I will do my best to demonstrate all the questions on LeetCode in the form of animation. 9 | 10 | I plan to take three to four years to complete it! 11 | 12 | I look forward to witnessing this day with you! 13 | 14 | The latest article published in WeChat **五分钟学算法** , you can pay attention to get the latest article. 15 | 16 | 17 | 18 | ## Problems 19 | 20 | 21 | | ID | Problem | Article | Animation | Date| 22 | | --- | --- | :--- |:--- | :--- | 23 | | 000 |Ten Classic Sorting Algorithms | [十大经典排序算法动画与解析,看我就够了!(配代码完全版)](https://mp.weixin.qq.com/s/vn3KiV-ez79FmbZ36SX9lg) | ![归并排序.gif](https://upload-images.jianshu.io/upload_images/1940317-92f62b62af03e233.gif?imageMogr2/auto-orient/strip)| 24 | | 001 |Two-Sum | [每天一算:Two Sum](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483740&idx=1&sn=1950545589ea9b86ee65fbb6be1f4290&chksm=fa0e6eddcd79e7cb542b7d4dc1304eead516994315fa4f52b575230f0f022c9e0a88ede3714e&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161033.gif) | 25 | | 002 |Add Two Numbers| [图解LeetCode第 2 号问题:两个数字相加](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484231&idx=2&sn=6a9eb4fd0619c822e4dede69b8d841c8&chksm=fa0e6cc6cd79e5d0c03fffcd65b665fed62db9dca9c97771898f388ea292ce806bfd6eb908b5&token=934487935&lang=zh_CN#rd) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092831.gif)| 26 | | 003 |Longest Substring Without Repeating Characters| [图解LeetCode第 3 号问题:无重复字符的最长子串](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484265&idx=2&sn=7f72afb341865923315bd51e1f50beff&chksm=fa0e6ce8cd79e5fe4be925fd5f01f59f59010c6c965fb3daefac79992593a6e58990c212e0bb&token=1412967663&lang=zh_CN#rd)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092855.gif)| 27 | | 019| Remove-Nth-Node-From-End-of-List |[每天一算:Remove Nth Node From End of List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483821&idx=1&sn=11ecccab76cd53163e9dedb75effeb93&chksm=fa0e6e2ccd79e73ae9137c0d91b3533df4ea4ead4ad081834b8d91ff364c0d55c350ddcfa6c4&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161058.gif)| 28 | | 020|Valid-Parentheses | [每天一算:Valid Parentheses](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483824&idx=1&sn=ab9362e125dc5e2b3ef1611cad9448c2&chksm=fa0e6e31cd79e727c6e1e0e3c467e193edb6ae841a41e5dc8eef39d0bf3141cc53f63b019cba&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161106.gif)| 29 | | 024| Swap-Nodes-in-Pairs | [每天一算:Swap Nodes in Pairs](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483773&idx=1&sn=db6cf272df968cd6571eb0bb50ecc721&chksm=fa0e6efccd79e7ea26810d335e6ece9ac23b8e3ac31be00dbd534018737ccb3ef9a00f22aff3&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161115.gif)| 30 | | 026| Remove-Duplicates-from-Sorted-Array| [图解LeetCode第 26 号问题:删除排序数组中的重复项](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484284&idx=2&sn=c8af62a82a62a21217d0f0b2b5891e4f&chksm=fa0e6cfdcd79e5ebe8726a61f93b834467d29b7d9e60a44feb990388f9e98605ac1e3f7e723d&token=762342620&lang=zh_CN#rd) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161124.gif)| 31 | | 075| Sort-Colors| [每天一算:Sort Colors](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483706&idx=1&sn=905f43c882a91b55fd169d812620f277&chksm=fa0e6ebbcd79e7ad8857b0dad9ad14dbaf17fe557ef56ba600cec26b2bb668df2e171431d74c&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161136.gif)| 32 | | 086| Partition-List| [每天一算:Partition List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483781&idx=1&sn=f31548ebbb2cf9ba56d979d3e51ddde2&chksm=fa0e6e04cd79e712d6cc7ff8e8b7631b7300ac0fa1a3e4c4e3b836de7a01fb5d0d6428a18bc4&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161147.gif)| 33 | |092 |Reverse-Linked-List-II | [每天一算:Reverse Linked List II](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483820&idx=1&sn=113e87b55c8ac8e22e9db00673798118&chksm=fa0e6e2dcd79e73b5835a262599b935783de3317a453bc0ed8df9fa5d1532785a085ea663e59&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161156.gif)| 34 | | 094|Binary-Tree-Inorder-Traversal | [每天一算:Binary Tree Inorder Traversal ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483853&idx=1&sn=94cd4b4ee8dc2268290a72334c6af57b&chksm=fa0e6e4ccd79e75a41a6b78397b80cdfccda332823874475b516f997f89e786488599fc5cc1e&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161208.gif)| 35 | |102 |Binary-Tree-Level-Order-Traversal| [每天一算:Binary Tree Level Order Traveral](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483868&idx=1&sn=d50041789fcd13a75a2296f620b69d71&chksm=fa0e6e5dcd79e74b0030ac5129f10ec4ba87c98da63c5904affe9f06e06ecf28695c410d3ec7&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161232.gif)| 36 | |103 |Binary Tree Zigzag Level Order Traversal| [图解LeetCode第 103 号问题:二叉树的锯齿形层次遍历](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484290&idx=2&sn=c29c4eefcbe8954cca6b3c8491ebccf1&chksm=fa0e6c03cd79e515581905322a3a22a3f3d10d24ca668a9d5aaef00932f0237eeaeaf3199668&token=1840661183&lang=zh_CN#rd)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092922.gif)| 37 | |107 | Binary Tree Level Order Traversal II | 每天一算: Binary Tree Level Order Traversal II|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210092949.gif)| 38 | |136 | Single Number | [一道让你拍案叫绝的算法题 ](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484505&idx=1&sn=4c1c056dd4852c3b4b1ead51c90a9b2d&chksm=fa0e6bd8cd79e2ce8188dcdd8843a5d071248906bfb8971c62d513dbd69b816acc191a78e4b2&token=487128715&lang=zh_CN#rd)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190116110804.gif)| 2019-01-16 | 39 | |144 | Binary-Tree-Preorder-Traversal| [每天一算:Binary Tree Preorder Traversal](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483843&idx=1&sn=994bf0d42dd9941a879a3a3ed500a4d6&chksm=fa0e6e42cd79e75472404eb5da7ee98f20d303efe230eb4f41efec57164630f555e7111e62ff&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181207112441.gif)| 40 | | 145| Binary-Tree-Postorder-Traversal | [每天一算:Binary Tree Postorder Traversal](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483853&idx=1&sn=94cd4b4ee8dc2268290a72334c6af57b&chksm=fa0e6e4ccd79e75a41a6b78397b80cdfccda332823874475b516f997f89e786488599fc5cc1e&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181219084940.gif)| 41 | | 146| LRU Cache | LRU缓存机制 |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190125143756.gif)| 2019-01-25 Made by Jun chen| 42 | | 150| Evaluate-Reverse-Polish-Notation | [每天一算:Evaluate Reverse Polish Notation](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483834&idx=1&sn=27cbff99f10dfcdb56cb37c237d7f2bb&chksm=fa0e6e3bcd79e72dc430bf81aed9dde9bd01634239dcf7820d6befa881efd323d9d58d76d90d&scene=21#wechat_redirect) |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161304.gif)| 43 | | 167| Two-Sum-II-Input-array-is-sorted | [每天一算:Two Sum II ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483711&idx=1&sn=3afec74e9e9effa71dc0b22659e14b44&chksm=fa0e6ebecd79e7a84db7861c9b5dbccdc98aa9d9a6994dda49a37edeb729e8242ea6af8f20ad&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161314.gif)| 44 | |199 | Binary Tree Right Side View | 每天一算:Binary Tree Right Side View |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161328.gif)| 45 | | 203| Remove-Linked-List-Elements | [每天一算:Remove Linked List Elements](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483766&idx=1&sn=6721376a65680bf7cf9064cf7b1ae4ae&chksm=fa0e6ef7cd79e7e1665e60fe6ea3f2087bca518c1573bc4c4b9425573f98401bafc59542dca0&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161338.gif)| 46 | |206 | Reverse Linked List | [每天一算: Reverse Linked List ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483799&idx=1&sn=c2212c8963809e8d3392abeeb851dbfc&chksm=fa0e6e16cd79e7003c2d30b1a2bb4f23dc56df38e3efedd0ab2cfae291609280a832eabe67de&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210093009.gif)| 47 | |209 | Minimum Size Subarray Sum | 每天一算: Minimum Size Subarray Sum |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181210093031.gif)| 48 | | 219|Contains-Duplicate-II |[每天一算:Contains Duplicate II ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483755&idx=1&sn=2501b6ca09c43eaa9fba71a9bd1f5253&chksm=fa0e6eeacd79e7fc192c0a23cf90d98fe6f2c35f9e4f2d0f937ccba45a58cf23a0a9c49d35d5&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161352.gif)| 49 | |237| Delete-Node-in-a-Linked-List |每天一算:Delete Node in a Linked List|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161400.gif) | 50 | |279| Perfect Squares |[图解LeetCode第 279 号问题: 完全平方数](https://mp.weixin.qq.com/s/53AlHe29fJF1hEwj0cj7ZA)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190119213317.gif) | Made by 王琛 2019-01-19日| 51 | |283 |Move-Zeroes |[每天一算:Move Zeros](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483700&idx=1&sn=465f778d60e8560742feab5844d7cac5&chksm=fa0e6eb5cd79e7a357899d378edb532b498cd63e3ce9113f8ac74d397ce4b214ca5aa8198b7d&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161421.gif)| 52 | |328 |Odd-Even-Linked-List | [每天一算:Odd Even Linked List](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483786&idx=1&sn=f7810950b34675e1c4420361faf5e361&chksm=fa0e6e0bcd79e71d2c6fc6a4a68b6ef7a17abc3dc9897548f8e44b51e9494f52c4cebbc4176e&scene=21#wechat_redirect) |![](https://diycode.b0.upaiyun.com/photo/2018/94e5c38540029690c93314b3d697caaf.gif)| 53 | |344 | Reverse-String |每天一算:Reverse String |![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181211110918.gif)| 54 | |349 | Intersection-of-Two-Arrays| [每天一算:Intersection of Two Arrays ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483726&idx=1&sn=a887f6b983058d97c183dd300832ecbb&chksm=fa0e6ecfcd79e7d985587b543622c85aadc83a4d7a074135e1356fb4a0ebfd07e7af13467906&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161441.gif)| 55 | | 350| Intersection-of-Two-Arrays-II| [每天一算:Intersection of Two Arrays II ](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483733&idx=1&sn=946bd6de3251437dd77b43ecab056c82&chksm=fa0e6ed4cd79e7c2a439b5f1853bf5154a3438ed282c7ba5e94948780c426a1f1492c0b201c4&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161729.gif)| 56 | |445| Add Two Numbers II |[图解LeetCode第 445 号问题: 两数相加 II](https://mp.weixin.qq.com/s/z8_1dK7mw9gxfhhSZUBVgg)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20190119213714.gif) | Made by 王琛 2019-01-19日| 57 | |447 | Number-of-Boomerangs | [每日一算:Number of Boomerangs](http://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247483747&idx=1&sn=7774eee0b252b311257134f6a52c4e2d&chksm=fa0e6ee2cd79e7f44858c46c3d04859ced9073dbb9de95ce7ee0bcc131e613862ddfd9a6f158&scene=21#wechat_redirect)|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161738.gif)| 58 | |454 |4Sum-II | 每日一算:4Sum II|![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/20181206161751.gif)| 59 | |642 |Design Search Autocomplete System | [图解 LeetCode 第 642 号问题:搜索自动完成系统](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484491&idx=1&sn=b329d90370d183b5a58bbf03f6a436ae&chksm=fa0e6bcacd79e2dc05bb5eaabd888561b82c37700b511e4971aa76ec42a630c0a35ef3e4721b&token=397665543&lang=zh_CN#rd)|Made by Jun [Click here](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484491&idx=1&sn=b329d90370d183b5a58bbf03f6a436ae&chksm=fa0e6bcacd79e2dc05bb5eaabd888561b82c37700b511e4971aa76ec42a630c0a35ef3e4721b&token=397665543&lang=zh_CN#rd)| 60 | 61 | If the link of the article cannot be clicked, it means that the article has not been published. Please look forward to it :) 62 | 63 | ## Code source 64 | This warehouse code unless otherwise specified, all from this warehouse 65 | 66 | [Play-Leetcode](https://github.com/liuyubobobo/Play-Leetcode) 67 | 68 | 69 | ## Supplement 70 | 71 | The warehouse is kept up to date. 72 | 73 | 74 | 2018-12-29 explain: 75 | 76 | [《2019年LeetCodeAnimationd的更新计划》](https://mp.weixin.qq.com/s?__biz=MzUyNjQxNjYyMg==&mid=2247484375&idx=1&sn=5a5482d9863342650d8b43bb59171f7c&chksm=fa0e6c56cd79e540115e52500b80c8e72001c87ddceb7c0ae1de166fd283d632b960cde41aca&token=578760218&lang=zh_CN#rd) 77 | 78 | 2018-12-07 explain: 79 | 80 | In order to better perform LeetCode animation, i am working hard to learn more data structures and algorithms. 81 | 82 | I am currently writing an article 《动画图解数据结构》, and will continue to update this warehouse after finishing the series of articles on animation graphic data structure. 83 | 84 | E-mail:misterbigbooo@gmail.com 85 | 86 | like it! star❤️ it! 87 | --------------------------------------------------------------------------------