From c1aef7e0fe2b8029010522514cff8ccc603863f8 Mon Sep 17 00:00:00 2001 From: misterbooo <278166530@qq.com> Date: Thu, 2 May 2019 08:34:39 +0800 Subject: [PATCH] Remove Code --- 0001-Two-Sum/cpp-0001/CMakeLists.txt | 7 -- 0001-Two-Sum/cpp-0001/main.cpp | 43 ------- 0001-Two-Sum/cpp-0001/main2.cpp | 50 -------- 0001-Two-Sum/cpp-0001/main3.cpp | 50 -------- 0001-Two-Sum/java-0001/src/Solution1.java | 36 ------ 0001-Two-Sum/java-0001/src/Solution2.java | 44 ------- 0001-Two-Sum/java-0001/src/Solution3.java | 41 ------- 0002-Add-Two-Numbers/cpp-0002/CMakeLists.txt | 7 -- 0002-Add-Two-Numbers/cpp-0002/main.cpp | 49 -------- 0002-Add-Two-Numbers/cpp-0002/main2.cpp | 54 --------- 0002-Add-Two-Numbers/cpp-0002/main3.cpp | 59 --------- .../cpp-0019/CMakeLists.txt | 7 -- .../cpp-0019/main.cpp | 111 ----------------- .../cpp-0019/main2.cpp | 112 ------------------ .../java-0019/src/ListNode.java | 52 -------- .../java-0019/src/Solution1.java | 38 ------ .../java-0019/src/Solution2.java | 41 ------- .../cpp-0020/CMakeLists.txt | 7 -- 0020-Valid-Parentheses/cpp-0020/main.cpp | 64 ---------- .../java-0020/src/Main.java | 6 - .../java-0020/src/Solution.java | 56 --------- .../cpp-0024/CMakeLists.txt | 7 -- 0024-Swap-Nodes-in-Pairs/cpp-0024/main.cpp | 105 ---------------- .../java-0024/src/ListNode.java | 36 ------ .../java-0024/src/Solution.java | 38 ------ .../cpp-0026/CMakeLists.txt | 7 -- .../cpp-0026/main.cpp | 51 -------- 0075-Sort-Colors/cpp-0075/CMakeLists.txt | 7 -- 0075-Sort-Colors/cpp-0075/main.cpp | 52 -------- 0075-Sort-Colors/cpp-0075/main2.cpp | 51 -------- 0075-Sort-Colors/java-0075/src/Solution1.java | 39 ------ 0075-Sort-Colors/java-0075/src/Solution2.java | 44 ------- 0086-Partition-List/cpp-0086/CMakeLists.txt | 7 -- 0086-Partition-List/cpp-0086/main.cpp | 56 --------- .../cpp-0092/CMakeLists.txt | 7 -- 0092-Reverse-Linked-List-II/cpp-0092/main.cpp | 62 ---------- .../cpp-0094/CMakeLists.txt | 7 -- .../cpp-0094/main.cpp | 45 ------- .../cpp-0094/main2.cpp | 64 ---------- .../cpp-0094/main3.cpp | 55 --------- .../cpp-0094/main4.cpp | 55 --------- .../cpp-0094/main5.cpp | 64 ---------- .../java-0094/src/Solution1.java | 27 ----- .../java-0094/src/Solution2.java | 48 -------- .../java-0094/src/Solution3.java | 35 ------ .../java-0094/src/Solution4.java | 36 ------ .../java-0094/src/Solution5.java | 45 ------- .../java-0094/src/TreeNode.java | 7 -- .../cpp-0102/CMakeLists.txt | 7 -- .../cpp-0102/main.cpp | 59 --------- .../cpp-0102/main2.cpp | 68 ----------- .../java-0102/src/Solution.java | 43 ------- .../java-0102/src/Solution2.java | 51 -------- .../java-0102/src/TreeNode.java | 7 -- .../cpp-0144/CMakeLists.txt | 7 -- .../cpp-0144/main.cpp | 45 ------- .../cpp-0144/main2.cpp | 63 ---------- .../cpp-0144/main3.cpp | 51 -------- .../cpp-0144/main4.cpp | 65 ---------- .../cpp-0144/main5.cpp | 66 ----------- .../cpp-0144/main6.cpp | 62 ---------- .../java-0144/src/Solution1.java | 26 ---- .../java-0144/src/Solution2.java | 48 -------- .../java-0144/src/Solution3.java | 34 ------ .../java-0144/src/Solution4.java | 34 ------ .../java-0144/src/Solution5.java | 35 ------ .../java-0144/src/Solution6.java | 45 ------- .../java-0144/src/TreeNode.java | 7 -- .../cpp-0145/CMakeLists.txt | 7 -- .../cpp-0145/main.cpp | 45 ------- .../cpp-0145/main2.cpp | 63 ---------- .../cpp-0145/main3.cpp | 71 ----------- .../cpp-0145/main4.cpp | 62 ---------- .../cpp-0145/main5.cpp | 63 ---------- .../cpp-0145/main6.cpp | 78 ------------ .../cpp-0145/main7.cpp | 79 ------------ .../cpp-0145/main8.cpp | 79 ------------ .../cpp-0145/main9.cpp | 89 -------------- .../java-0145/src/Solution1.java | 27 ----- .../java-0145/src/Solution2.java | 48 -------- .../java-0145/src/Solution3.java | 54 --------- .../java-0145/src/Solution4.java | 41 ------- .../java-0145/src/Solution5.java | 40 ------- .../java-0145/src/Solution6.java | 45 ------- .../java-0145/src/Solution7.java | 46 ------- .../java-0145/src/Solution8.java | 47 -------- .../java-0145/src/Solution9.java | 65 ---------- .../java-0145/src/TreeNode.java | 7 -- .../cpp-0150/CMakeLists.txt | 7 -- .../cpp-0150/main.cpp | 50 -------- .../cpp-0167/CMakeLists.txt | 7 -- .../cpp-0167/main.cpp | 55 --------- .../cpp-0167/main2.cpp | 74 ------------ .../cpp-0167/main3.cpp | 62 ---------- .../java-0167/src/Solution1.java | 44 ------- .../java-0167/src/Solution2.java | 66 ----------- .../java-0167/src/Solution3.java | 50 -------- .../cpp-0203/CMakeLists.txt | 7 -- .../cpp-0203/main.cpp | 108 ----------------- .../cpp-0203/main2.cpp | 42 ------- .../java-0203/src/ListNode.java | 36 ------ .../java-0203/src/Solution.java | 38 ------ .../java-0203/src/Solution2.java | 24 ---- .../cpp-0219/CMakeLists.txt | 7 -- 0219-Contains-Duplicate-II/cpp-0219/main.cpp | 56 --------- .../java-0219/src/Solution.java | 43 ------- .../cpp-0237/CMakeLists.txt | 7 -- .../cpp-0237/main.cpp | 104 ---------------- .../java-0237/src/ListNode.java | 47 -------- .../java-0237/src/Solution.java | 29 ----- 0279-Perfect-Squares/cpp-0279/CMakeLists.txt | 7 -- 0279-Perfect-Squares/cpp-0279/main.cpp | 53 --------- 0279-Perfect-Squares/cpp-0279/main2.cpp | 45 ------- 0279-Perfect-Squares/cpp-0279/main3.cpp | 34 ------ .../java-0279/src/Solution1.java | 50 -------- .../java-0279/src/Solution2.java | 39 ------ .../java-0279/src/Solution3.java | 29 ----- 0283-Move-Zeroes/cpp-0283/CMakeLists.txt | 7 -- 0283-Move-Zeroes/cpp-0283/main.cpp | 48 -------- 0283-Move-Zeroes/cpp-0283/main2.cpp | 42 ------- 0283-Move-Zeroes/cpp-0283/main3.cpp | 38 ------ 0283-Move-Zeroes/cpp-0283/main4.cpp | 42 ------- 0283-Move-Zeroes/java-0283/src/Solution1.java | 41 ------- 0283-Move-Zeroes/java-0283/src/Solution2.java | 35 ------ 0283-Move-Zeroes/java-0283/src/Solution3.java | 37 ------ 0283-Move-Zeroes/java-0283/src/Solution4.java | 40 ------- .../cpp-0328/CMakeLists.txt | 7 -- 0328-Odd-Even-Linked-List/cpp-0328/main.cpp | 60 ---------- 0328-Odd-Even-Linked-List/cpp-0328/main2.cpp | 55 --------- 0344-Reverse-String/cpp-0344/CMakeLists.txt | 7 -- 0344-Reverse-String/cpp-0344/main.cpp | 35 ------ .../cpp-0349/CMakeLists.txt | 7 -- .../cpp-0349/main.cpp | 47 -------- .../java-0349/src/Solution.java | 44 ------- .../cpp-0350/CMakeLists.txt | 7 -- .../cpp-0350/main.cpp | 50 -------- .../cpp-0350/main2.cpp | 46 ------- .../java-0350/src/Solution.java | 50 -------- .../cpp-0447/CMakeLists.txt | 7 -- 0447-Number-of-Boomerangs/cpp-0447/main.cpp | 53 --------- .../java-0447/src/Solution.java | 46 ------- 0454-4Sum-II/cpp-0454/CMakeLists.txt | 7 -- 0454-4Sum-II/cpp-0454/main.cpp | 49 -------- 0454-4Sum-II/cpp-0454/main2.cpp | 55 --------- 0454-4Sum-II/java-0454/src/Solution1.java | 44 ------- 0454-4Sum-II/java-0454/src/Solution2.java | 54 --------- 146 files changed, 6150 deletions(-) delete mode 100755 0001-Two-Sum/cpp-0001/CMakeLists.txt delete mode 100755 0001-Two-Sum/cpp-0001/main.cpp delete mode 100755 0001-Two-Sum/cpp-0001/main2.cpp delete mode 100755 0001-Two-Sum/cpp-0001/main3.cpp delete mode 100755 0001-Two-Sum/java-0001/src/Solution1.java delete mode 100755 0001-Two-Sum/java-0001/src/Solution2.java delete mode 100755 0001-Two-Sum/java-0001/src/Solution3.java delete mode 100755 0002-Add-Two-Numbers/cpp-0002/CMakeLists.txt delete mode 100755 0002-Add-Two-Numbers/cpp-0002/main.cpp delete mode 100755 0002-Add-Two-Numbers/cpp-0002/main2.cpp delete mode 100755 0002-Add-Two-Numbers/cpp-0002/main3.cpp delete mode 100755 0019-Remove-Nth-Node-From-End-of-List/cpp-0019/CMakeLists.txt delete mode 100755 0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main.cpp delete mode 100755 0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main2.cpp delete mode 100755 0019-Remove-Nth-Node-From-End-of-List/java-0019/src/ListNode.java delete mode 100755 0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution1.java delete mode 100755 0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution2.java delete mode 100755 0020-Valid-Parentheses/cpp-0020/CMakeLists.txt delete mode 100755 0020-Valid-Parentheses/cpp-0020/main.cpp delete mode 100755 0020-Valid-Parentheses/java-0020/src/Main.java delete mode 100755 0020-Valid-Parentheses/java-0020/src/Solution.java delete mode 100755 0024-Swap-Nodes-in-Pairs/cpp-0024/CMakeLists.txt delete mode 100755 0024-Swap-Nodes-in-Pairs/cpp-0024/main.cpp delete mode 100755 0024-Swap-Nodes-in-Pairs/java-0024/src/ListNode.java delete mode 100755 0024-Swap-Nodes-in-Pairs/java-0024/src/Solution.java delete mode 100755 0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/CMakeLists.txt delete mode 100755 0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/main.cpp delete mode 100755 0075-Sort-Colors/cpp-0075/CMakeLists.txt delete mode 100755 0075-Sort-Colors/cpp-0075/main.cpp delete mode 100755 0075-Sort-Colors/cpp-0075/main2.cpp delete mode 100755 0075-Sort-Colors/java-0075/src/Solution1.java delete mode 100755 0075-Sort-Colors/java-0075/src/Solution2.java delete mode 100755 0086-Partition-List/cpp-0086/CMakeLists.txt delete mode 100755 0086-Partition-List/cpp-0086/main.cpp delete mode 100755 0092-Reverse-Linked-List-II/cpp-0092/CMakeLists.txt delete mode 100755 0092-Reverse-Linked-List-II/cpp-0092/main.cpp delete mode 100755 0094-Binary-Tree-Inorder-Traversal/cpp-0094/CMakeLists.txt delete mode 100755 0094-Binary-Tree-Inorder-Traversal/cpp-0094/main.cpp delete mode 100755 0094-Binary-Tree-Inorder-Traversal/cpp-0094/main2.cpp delete mode 100755 0094-Binary-Tree-Inorder-Traversal/cpp-0094/main3.cpp delete mode 100755 0094-Binary-Tree-Inorder-Traversal/cpp-0094/main4.cpp delete mode 100755 0094-Binary-Tree-Inorder-Traversal/cpp-0094/main5.cpp delete mode 100755 0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution1.java delete mode 100755 0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution2.java delete mode 100755 0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution3.java delete mode 100755 0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution4.java delete mode 100755 0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution5.java delete mode 100755 0094-Binary-Tree-Inorder-Traversal/java-0094/src/TreeNode.java delete mode 100755 0102-Binary-Tree-Level-Order-Traversal/cpp-0102/CMakeLists.txt delete mode 100755 0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main.cpp delete mode 100755 0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main2.cpp delete mode 100755 0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution.java delete mode 100755 0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution2.java delete mode 100755 0102-Binary-Tree-Level-Order-Traversal/java-0102/src/TreeNode.java delete mode 100755 0144-Binary-Tree-Preorder-Traversal/cpp-0144/CMakeLists.txt delete mode 100755 0144-Binary-Tree-Preorder-Traversal/cpp-0144/main.cpp delete mode 100755 0144-Binary-Tree-Preorder-Traversal/cpp-0144/main2.cpp delete mode 100755 0144-Binary-Tree-Preorder-Traversal/cpp-0144/main3.cpp delete mode 100755 0144-Binary-Tree-Preorder-Traversal/cpp-0144/main4.cpp delete mode 100755 0144-Binary-Tree-Preorder-Traversal/cpp-0144/main5.cpp delete mode 100755 0144-Binary-Tree-Preorder-Traversal/cpp-0144/main6.cpp delete mode 100755 0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution1.java delete mode 100755 0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution2.java delete mode 100755 0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution3.java delete mode 100755 0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution4.java delete mode 100755 0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution5.java delete mode 100755 0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution6.java delete mode 100755 0144-Binary-Tree-Preorder-Traversal/java-0144/src/TreeNode.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/CMakeLists.txt delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/main.cpp delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/main2.cpp delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/main3.cpp delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/main4.cpp delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/main5.cpp delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/main6.cpp delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/main7.cpp delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/main8.cpp delete mode 100755 0145-Binary-Tree-Postorder-Traversal/cpp-0145/main9.cpp delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution1.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution2.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution3.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution4.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution5.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution6.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution7.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution8.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution9.java delete mode 100755 0145-Binary-Tree-Postorder-Traversal/java-0145/src/TreeNode.java delete mode 100755 0150-Evaluate-Reverse-Polish-Notation/cpp-0150/CMakeLists.txt delete mode 100755 0150-Evaluate-Reverse-Polish-Notation/cpp-0150/main.cpp delete mode 100755 0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/CMakeLists.txt delete mode 100755 0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main.cpp delete mode 100755 0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main2.cpp delete mode 100755 0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main3.cpp delete mode 100755 0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution1.java delete mode 100755 0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution2.java delete mode 100755 0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution3.java delete mode 100755 0203-Remove-Linked-List-Elements/cpp-0203/CMakeLists.txt delete mode 100755 0203-Remove-Linked-List-Elements/cpp-0203/main.cpp delete mode 100755 0203-Remove-Linked-List-Elements/cpp-0203/main2.cpp delete mode 100755 0203-Remove-Linked-List-Elements/java-0203/src/ListNode.java delete mode 100755 0203-Remove-Linked-List-Elements/java-0203/src/Solution.java delete mode 100755 0203-Remove-Linked-List-Elements/java-0203/src/Solution2.java delete mode 100755 0219-Contains-Duplicate-II/cpp-0219/CMakeLists.txt delete mode 100755 0219-Contains-Duplicate-II/cpp-0219/main.cpp delete mode 100755 0219-Contains-Duplicate-II/java-0219/src/Solution.java delete mode 100755 0237-Delete-Node-in-a-Linked-List/cpp-0237/CMakeLists.txt delete mode 100755 0237-Delete-Node-in-a-Linked-List/cpp-0237/main.cpp delete mode 100755 0237-Delete-Node-in-a-Linked-List/java-0237/src/ListNode.java delete mode 100755 0237-Delete-Node-in-a-Linked-List/java-0237/src/Solution.java delete mode 100755 0279-Perfect-Squares/cpp-0279/CMakeLists.txt delete mode 100755 0279-Perfect-Squares/cpp-0279/main.cpp delete mode 100755 0279-Perfect-Squares/cpp-0279/main2.cpp delete mode 100755 0279-Perfect-Squares/cpp-0279/main3.cpp delete mode 100755 0279-Perfect-Squares/java-0279/src/Solution1.java delete mode 100755 0279-Perfect-Squares/java-0279/src/Solution2.java delete mode 100755 0279-Perfect-Squares/java-0279/src/Solution3.java delete mode 100755 0283-Move-Zeroes/cpp-0283/CMakeLists.txt delete mode 100755 0283-Move-Zeroes/cpp-0283/main.cpp delete mode 100755 0283-Move-Zeroes/cpp-0283/main2.cpp delete mode 100755 0283-Move-Zeroes/cpp-0283/main3.cpp delete mode 100755 0283-Move-Zeroes/cpp-0283/main4.cpp delete mode 100755 0283-Move-Zeroes/java-0283/src/Solution1.java delete mode 100755 0283-Move-Zeroes/java-0283/src/Solution2.java delete mode 100755 0283-Move-Zeroes/java-0283/src/Solution3.java delete mode 100755 0283-Move-Zeroes/java-0283/src/Solution4.java delete mode 100755 0328-Odd-Even-Linked-List/cpp-0328/CMakeLists.txt delete mode 100755 0328-Odd-Even-Linked-List/cpp-0328/main.cpp delete mode 100755 0328-Odd-Even-Linked-List/cpp-0328/main2.cpp delete mode 100755 0344-Reverse-String/cpp-0344/CMakeLists.txt delete mode 100755 0344-Reverse-String/cpp-0344/main.cpp delete mode 100755 0349-Intersection-of-Two-Arrays/cpp-0349/CMakeLists.txt delete mode 100755 0349-Intersection-of-Two-Arrays/cpp-0349/main.cpp delete mode 100755 0349-Intersection-of-Two-Arrays/java-0349/src/Solution.java delete mode 100755 0350-Intersection-of-Two-Arrays-II/cpp-0350/CMakeLists.txt delete mode 100755 0350-Intersection-of-Two-Arrays-II/cpp-0350/main.cpp delete mode 100755 0350-Intersection-of-Two-Arrays-II/cpp-0350/main2.cpp delete mode 100755 0350-Intersection-of-Two-Arrays-II/java-0350/src/Solution.java delete mode 100755 0447-Number-of-Boomerangs/cpp-0447/CMakeLists.txt delete mode 100755 0447-Number-of-Boomerangs/cpp-0447/main.cpp delete mode 100755 0447-Number-of-Boomerangs/java-0447/src/Solution.java delete mode 100755 0454-4Sum-II/cpp-0454/CMakeLists.txt delete mode 100755 0454-4Sum-II/cpp-0454/main.cpp delete mode 100755 0454-4Sum-II/cpp-0454/main2.cpp delete mode 100755 0454-4Sum-II/java-0454/src/Solution1.java delete mode 100755 0454-4Sum-II/java-0454/src/Solution2.java diff --git a/0001-Two-Sum/cpp-0001/CMakeLists.txt b/0001-Two-Sum/cpp-0001/CMakeLists.txt deleted file mode 100755 index 7af49d30..00000000 --- a/0001-Two-Sum/cpp-0001/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0001) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main2.cpp) -add_executable(cpp_0001 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0001-Two-Sum/cpp-0001/main.cpp b/0001-Two-Sum/cpp-0001/main.cpp deleted file mode 100755 index d6a79140..00000000 --- a/0001-Two-Sum/cpp-0001/main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include - -using namespace std; - -/// Brute Force -/// Time Complexity: O(n^2) -/// Space Complexity: O(1) -class Solution { -public: - vector twoSum(vector& nums, int target) { - - for(int i = 0 ; i < nums.size() ; i ++) - for(int j = i + 1 ; j < nums.size() ; j ++) - if(nums[i] + nums[j] == target){ - int res[] = {i, j}; - return vector(res, res + 2); - } - - throw invalid_argument("the input has no solution"); - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - const int nums[] = {0,4,3,0}; - vector nums_vec( nums, nums + sizeof(nums)/sizeof(int) ); - int target = 0; - printVec(Solution().twoSum(nums_vec, target)); - - return 0; -} \ No newline at end of file diff --git a/0001-Two-Sum/cpp-0001/main2.cpp b/0001-Two-Sum/cpp-0001/main2.cpp deleted file mode 100755 index 8d1aedab..00000000 --- a/0001-Two-Sum/cpp-0001/main2.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include -#include -#include - -using namespace std; - -/// Two-Pass Hash Table -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { -public: - vector twoSum(vector& nums, int target) { - - unordered_map record; - for(int i = 0 ; i < nums.size() ; i ++) - record[nums[i]] = i; - - for(int i = 0 ; i < nums.size() ; i ++){ - unordered_map::iterator iter = record.find(target - nums[i]); - if(iter != record.end() && iter->second != i){ - int res[] = {i, iter->second}; - return vector(res, res + 2); - } - } - - throw invalid_argument("the input has no solution"); - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - const int nums[] = {0,4,3,0}; - vector nums_vec( nums, nums + sizeof(nums)/sizeof(int) ); - int target = 0; - printVec(Solution().twoSum(nums_vec, target)); - - return 0; -} \ No newline at end of file diff --git a/0001-Two-Sum/cpp-0001/main3.cpp b/0001-Two-Sum/cpp-0001/main3.cpp deleted file mode 100755 index e4fd14b3..00000000 --- a/0001-Two-Sum/cpp-0001/main3.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include -#include -#include - -using namespace std; - -/// One-Pass Hash Table -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { -public: - vector twoSum(vector& nums, int target) { - - unordered_map record; - for(int i = 0 ; i < nums.size() ; i ++){ - - int complement = target - nums[i]; - if(record.find(complement) != record.end()){ - int res[] = {i, record[complement]}; - return vector(res, res + 2); - } - - record[nums[i]] = i; - } - - throw invalid_argument("the input has no solution"); - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - const int nums[] = {0,4,3,0}; - vector nums_vec( nums, nums + sizeof(nums)/sizeof(int) ); - int target = 0; - printVec(Solution().twoSum(nums_vec, target)); - - return 0; -} \ No newline at end of file diff --git a/0001-Two-Sum/java-0001/src/Solution1.java b/0001-Two-Sum/java-0001/src/Solution1.java deleted file mode 100755 index f6ea5064..00000000 --- a/0001-Two-Sum/java-0001/src/Solution1.java +++ /dev/null @@ -1,36 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -import java.util.HashMap; - -/// Brute Force -/// Time Complexity: O(n^2) -/// Space Complexity: O(1) -public class Solution1 { - - public int[] twoSum(int[] nums, int target) { - - for(int i = 0 ; i < nums.length; i ++) - for(int j = 0 ; j < nums.length ; j ++) - if(nums[i] + nums[j] == target){ - int[] res = {i, j}; - return res; - } - - throw new IllegalStateException("the input has no solution"); - } - - private static void printArr(int[] nums){ - for(int num: nums) - System.out.print(num + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums = {0, 4, 3, 0}; - int target = 0; - printArr((new Solution1()).twoSum(nums, target)); - } -} diff --git a/0001-Two-Sum/java-0001/src/Solution2.java b/0001-Two-Sum/java-0001/src/Solution2.java deleted file mode 100755 index 4e7162ec..00000000 --- a/0001-Two-Sum/java-0001/src/Solution2.java +++ /dev/null @@ -1,44 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -import java.util.HashMap; - -/// Two-Pass Hash Table -/// Time Complexity: O(n) -/// Space Complexity: O(n) -public class Solution2 { - - public int[] twoSum(int[] nums, int target) { - - HashMap record = new HashMap(); - for(int i = 0 ; i < nums.length ; i ++) - record.put(nums[i], i); - - for(int i = 0 ; i < nums.length; i ++){ - - Integer index = record.get(target - nums[i]); - if(index != null && index != i){ - int[] res = {i, index}; - return res; - } - - record.put(nums[i], i); - } - - throw new IllegalStateException("the input has no solution"); - } - - private static void printArr(int[] nums){ - for(int num: nums) - System.out.print(num + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums = {0, 4, 3, 0}; - int target = 0; - printArr((new Solution2()).twoSum(nums, target)); - } -} diff --git a/0001-Two-Sum/java-0001/src/Solution3.java b/0001-Two-Sum/java-0001/src/Solution3.java deleted file mode 100755 index 6916133e..00000000 --- a/0001-Two-Sum/java-0001/src/Solution3.java +++ /dev/null @@ -1,41 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -import java.util.HashMap; - -/// One-Pass Hash Table -/// Time Complexity: O(n) -/// Space Complexity: O(n) -public class Solution3 { - - public int[] twoSum(int[] nums, int target) { - - HashMap record = new HashMap(); - for(int i = 0 ; i < nums.length; i ++){ - - int complement = target - nums[i]; - if(record.containsKey(complement)){ - int[] res = {i, record.get(complement)}; - return res; - } - - record.put(nums[i], i); - } - - throw new IllegalStateException("the input has no solution"); - } - - private static void printArr(int[] nums){ - for(int num: nums) - System.out.print(num + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums = {0, 4, 3, 0}; - int target = 0; - printArr((new Solution3()).twoSum(nums, target)); - } -} diff --git a/0002-Add-Two-Numbers/cpp-0002/CMakeLists.txt b/0002-Add-Two-Numbers/cpp-0002/CMakeLists.txt deleted file mode 100755 index 3a14c42c..00000000 --- a/0002-Add-Two-Numbers/cpp-0002/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0002) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main2.cpp) -add_executable(cpp_0002 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0002-Add-Two-Numbers/cpp-0002/main.cpp b/0002-Add-Two-Numbers/cpp-0002/main.cpp deleted file mode 100755 index d988cc1f..00000000 --- a/0002-Add-Two-Numbers/cpp-0002/main.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/// Source : https://leetcode.com/problems/add-two-numbers/description/ -/// Author : liuyubobobo -/// Time : 2018-08-09 - -#include - -using namespace std; - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - - -/// 时间复杂度: O(n) -/// 空间复杂度: O(n) -class Solution { -public: - ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { - - ListNode *p1 = l1, *p2 = l2; - ListNode *dummyHead = new ListNode(-1); - ListNode* cur = dummyHead; - int carried = 0; - while(p1 || p2 ){ - int a = p1 ? p1->val : 0; - int b = p2 ? p2->val : 0; - cur->next = new ListNode((a + b + carried) % 10); - carried = (a + b + carried) / 10; - - cur = cur->next; - p1 = p1 ? p1->next : NULL; - p2 = p2 ? p2->next : NULL; - } - - cur->next = carried ? new ListNode(1) : NULL; - ListNode* ret = dummyHead->next; - delete dummyHead; - return ret; - } -}; - - -int main() { - - return 0; -} diff --git a/0002-Add-Two-Numbers/cpp-0002/main2.cpp b/0002-Add-Two-Numbers/cpp-0002/main2.cpp deleted file mode 100755 index cdd669bd..00000000 --- a/0002-Add-Two-Numbers/cpp-0002/main2.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/// Source : https://leetcode.com/problems/add-two-numbers/description/ -/// Author : liuyubobobo -/// Time : 2018-08-09 - -#include - -using namespace std; - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - - -/// Using l1 as the result list -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { - -public: - ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { - - ListNode *p1 = l1, *p2 = l2; - ListNode* pre = NULL; - int carried = 0; - - while(p1 || p2){ - int a = p1 ? p1->val : 0; - int b = p2 ? p2->val : 0; - if(p1) - p1->val = (a + b + carried) % 10; - else{ - pre->next = new ListNode((a + b + carried) % 10); - p1 = pre->next; - } - carried = (a + b + carried) / 10; - - pre = p1; - p1 = p1->next; - if(p2) p2 = p2->next; - } - - pre->next = carried ? new ListNode(1) : NULL; - return l1; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/0002-Add-Two-Numbers/cpp-0002/main3.cpp b/0002-Add-Two-Numbers/cpp-0002/main3.cpp deleted file mode 100755 index 116d4185..00000000 --- a/0002-Add-Two-Numbers/cpp-0002/main3.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/// Source : https://leetcode.com/problems/add-two-numbers/description/ -/// Author : liuyubobobo -/// Time : 2018-08-09 - -#include - -using namespace std; - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - - -/// Using the longest list in l1 and l2 as the result list -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { - -public: - ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { - - int len1 = getLen(l1), len2 = getLen(l2); - ListNode *p1 = len1 > len2 ? l1 : l2; - ListNode *p2 = len1 > len2 ? l2 : l1; - - ListNode* pre = NULL; - int carried = 0; - while(p1){ - int a = p1->val; - int b = p2 ? p2->val : 0; - p1->val = (a + b + carried) % 10; - carried = (a + b + carried) / 10; - - pre = p1; - p1 = p1->next; - p2 = p2 ? p2->next : NULL; - } - - pre->next = carried ? new ListNode(1) : NULL; - return len1 > len2 ? l1 : l2; - } - -private: - int getLen(ListNode* l){ - int res = 0; - while(l) - res ++, l = l -> next; - return res; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/0019-Remove-Nth-Node-From-End-of-List/cpp-0019/CMakeLists.txt b/0019-Remove-Nth-Node-From-End-of-List/cpp-0019/CMakeLists.txt deleted file mode 100755 index d7fe1800..00000000 --- a/0019-Remove-Nth-Node-From-End-of-List/cpp-0019/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0019) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0019 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main.cpp b/0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main.cpp deleted file mode 100755 index 743df3fd..00000000 --- a/0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include - -using namespace std; - -///Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -/// LinkedList Test Helper Functions -ListNode* createLinkedList(int arr[], int n){ - - if(n == 0) - return NULL; - - ListNode* head = new ListNode(arr[0]); - ListNode* curNode = head; - for(int i = 1 ; i < n ; i ++){ - curNode->next = new ListNode(arr[i]); - curNode = curNode->next; - } - - return head; -} - -void printLinkedList(ListNode* head){ - - if(head == NULL){ - cout<<"NULL"<val; - if(curNode->next != NULL) - cout << " -> "; - curNode = curNode->next; - } - - cout << endl; - - return; -} - -void deleteLinkedList(ListNode* head){ - - ListNode* curNode = head; - while(curNode != NULL){ - ListNode* delNode = curNode; - curNode = curNode->next; - delete delNode; - } - - return; -} - -/// Get the total length and remove the nth node -/// Two Pass Algorithm -/// -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - ListNode* removeNthFromEnd(ListNode* head, int n) { - - ListNode* dummyHead = new ListNode(0); - dummyHead->next = head; - - int length = 0; - for(ListNode* cur = dummyHead->next ; cur != NULL ; cur = cur->next) - length ++; - - int k = length - n; - assert(k >= 0); - ListNode* cur = dummyHead; - for(int i = 0 ; i < k ; i ++) - cur = cur->next; - - ListNode* delNode = cur->next; - cur->next = delNode->next; - delete delNode; - - ListNode* retNode = dummyHead->next; - delete dummyHead; - return retNode; - } -}; - -int main() { - - int arr[] = {1, 2, 3, 4, 5}; - int n = sizeof(arr)/sizeof(int); - - ListNode* head = createLinkedList(arr, n); - printLinkedList(head); - - head = Solution().removeNthFromEnd(head, 2); - printLinkedList(head); - - deleteLinkedList(head); - - return 0; -} \ No newline at end of file diff --git a/0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main2.cpp b/0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main2.cpp deleted file mode 100755 index 3be9d091..00000000 --- a/0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main2.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include - -using namespace std; - -///Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -/// LinkedList Test Helper Functions -ListNode* createLinkedList(int arr[], int n){ - - if(n == 0) - return NULL; - - ListNode* head = new ListNode(arr[0]); - ListNode* curNode = head; - for(int i = 1 ; i < n ; i ++){ - curNode->next = new ListNode(arr[i]); - curNode = curNode->next; - } - - return head; -} - -void printLinkedList(ListNode* head){ - - if( head == NULL ){ - cout << "NULL" << endl; - return; - } - - ListNode* curNode = head; - while(curNode != NULL){ - cout << curNode->val; - if( curNode->next != NULL ) - cout << " -> "; - curNode = curNode->next; - } - - cout << endl; - - return; -} - -void deleteLinkedList(ListNode* head){ - - ListNode* curNode = head; - while(curNode != NULL){ - ListNode* delNode = curNode; - curNode = curNode->next; - delete delNode; - } - - return; -} - -/// Two Pointers - One Pass Algorithm -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - ListNode* removeNthFromEnd(ListNode* head, int n) { - - ListNode* dummyHead = new ListNode(0); - dummyHead->next = head; - - ListNode* p = dummyHead; - ListNode* q = dummyHead; - for(int i = 0 ; i < n + 1 ; i ++){ - assert(q); - q = q->next; - } - - while(q){ - p = p->next; - q = q->next; - } - - ListNode* delNode = p->next; - p->next = delNode->next; - delete delNode; - - ListNode* retNode = dummyHead->next; - delete dummyHead; - - return retNode; - } -}; - -int main() { - - int arr[] = {1, 2, 3, 4, 5}; - int n = sizeof(arr)/sizeof(int); - - ListNode* head = createLinkedList(arr, n); - printLinkedList(head); - - head = Solution().removeNthFromEnd(head, 2); - printLinkedList(head); - - deleteLinkedList(head); - - return 0; -} \ No newline at end of file diff --git a/0019-Remove-Nth-Node-From-End-of-List/java-0019/src/ListNode.java b/0019-Remove-Nth-Node-From-End-of-List/java-0019/src/ListNode.java deleted file mode 100755 index 65198546..00000000 --- a/0019-Remove-Nth-Node-From-End-of-List/java-0019/src/ListNode.java +++ /dev/null @@ -1,52 +0,0 @@ -// Definition for singly-linked list. -// 在Java版本中,我们将LinkedList相关的测试辅助函数写在ListNode里 -public class ListNode { - - public int val; - public ListNode next = null; - - public ListNode(int x) { - val = x; - } - - // 根据n个元素的数组arr创建一个链表 - // 使用arr为参数,创建另外一个ListNode的构造函数 - public ListNode (int[] arr){ - - if(arr == null || arr.length == 0) - throw new IllegalArgumentException("arr can not be empty"); - - this.val = arr[0]; - ListNode curNode = this; - for(int i = 1 ; i < arr.length ; i ++){ - curNode.next = new ListNode(arr[i]); - curNode = curNode.next; - } - } - - ListNode findNode(int x){ - - ListNode curNode = this; - while(curNode != null){ - if(curNode.val == x) - return curNode; - curNode = curNode.next; - } - return null; - } - - // 返回以当前ListNode为头结点的链表信息字符串 - @Override - public String toString(){ - - StringBuilder s = new StringBuilder(""); - ListNode curNode = this; - while(curNode != null){ - s.append(Integer.toString(curNode.val)); - s.append(" -> "); - curNode = curNode.next; - } - s.append("NULL"); - return s.toString(); - } -} \ No newline at end of file diff --git a/0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution1.java b/0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution1.java deleted file mode 100755 index f3392f2f..00000000 --- a/0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution1.java +++ /dev/null @@ -1,38 +0,0 @@ -/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 -// -// Time Complexity: O(n) -// Space Complexity: O(1) -public class Solution1 { - - public ListNode removeNthFromEnd(ListNode head, int n) { - - ListNode dummyHead = new ListNode(0); - dummyHead.next = head; - - int length = 0; - for(ListNode cur = dummyHead.next ; cur != null ; cur = cur.next) - length ++; - - int k = length - n; - assert k >= 0; - ListNode cur = dummyHead; - for(int i = 0 ; i < k ; i ++) - cur = cur.next; - - cur.next = cur.next.next; - - return dummyHead.next; - } - - public static void main(String[] args) { - - int arr[] = {1, 2, 3, 4, 5}; - ListNode head = new ListNode(arr); - System.out.println(head); - - head = (new Solution1()).removeNthFromEnd(head, 2); - System.out.println(head); - } -} diff --git a/0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution2.java b/0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution2.java deleted file mode 100755 index 8c4896d2..00000000 --- a/0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution2.java +++ /dev/null @@ -1,41 +0,0 @@ -/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 -// -// Two Pointers - One Pass Algorithm -// Time Complexity: O(n) -// Space Complexity: O(1) -public class Solution2 { - - public ListNode removeNthFromEnd(ListNode head, int n) { - - ListNode dummyHead = new ListNode(0); - dummyHead.next = head; - - ListNode p = dummyHead; - ListNode q = dummyHead; - for( int i = 0 ; i < n + 1 ; i ++ ){ - assert q != null; - q = q.next; - } - - while(q != null){ - p = p.next; - q = q.next; - } - - p.next = p.next.next; - - return dummyHead.next; - } - - public static void main(String[] args) { - - int arr[] = {1, 2, 3, 4, 5}; - ListNode head = new ListNode(arr); - System.out.println(head); - - head = (new Solution2()).removeNthFromEnd(head, 2); - System.out.println(head); - } -} diff --git a/0020-Valid-Parentheses/cpp-0020/CMakeLists.txt b/0020-Valid-Parentheses/cpp-0020/CMakeLists.txt deleted file mode 100755 index 71931a6d..00000000 --- a/0020-Valid-Parentheses/cpp-0020/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0020) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0020 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0020-Valid-Parentheses/cpp-0020/main.cpp b/0020-Valid-Parentheses/cpp-0020/main.cpp deleted file mode 100755 index 4890cfc9..00000000 --- a/0020-Valid-Parentheses/cpp-0020/main.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/// Source : https://leetcode.com/problems/valid-parentheses/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include -#include - -using namespace std; - -// Using Stack -// Time Complexity: O(n) -// Space Complexity: O(n) -class Solution { -public: - bool isValid(string s) { - - stack stack; - for( int i = 0 ; i < s.size() ; i ++ ) - if( s[i] == '(' || s[i] == '{' || s[i] == '[') - stack.push(s[i]); - else{ - - if( stack.size() == 0 ) - return false; - - char c = stack.top(); - stack.pop(); - - char match; - if( s[i] == ')' ) - match = '('; - else if( s[i] == ']' ) - match = '['; - else{ - assert( s[i] == '}' ); - match = '{'; - } - - if(c != match) - return false; - } - - if( stack.size() != 0 ) - return false; - - return true; - } -}; - - -void printBool(bool res){ - cout << (res ? "True" : "False") << endl; -} - -int main() { - - printBool(Solution().isValid("()")); - printBool(Solution().isValid("()[]{}")); - printBool(Solution().isValid("(]")); - printBool(Solution().isValid("([)]")); - - return 0; -} \ No newline at end of file diff --git a/0020-Valid-Parentheses/java-0020/src/Main.java b/0020-Valid-Parentheses/java-0020/src/Main.java deleted file mode 100755 index 5c6d9e38..00000000 --- a/0020-Valid-Parentheses/java-0020/src/Main.java +++ /dev/null @@ -1,6 +0,0 @@ -public class Main { - - public static void main(String[] args) { - // write your code here - } -} diff --git a/0020-Valid-Parentheses/java-0020/src/Solution.java b/0020-Valid-Parentheses/java-0020/src/Solution.java deleted file mode 100755 index 1bebf06d..00000000 --- a/0020-Valid-Parentheses/java-0020/src/Solution.java +++ /dev/null @@ -1,56 +0,0 @@ -/// Source : https://leetcode.com/problems/valid-parentheses/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.Stack; - -// Using Stack -// Time Complexity: O(n) -// Space Complexity: O(n) -public class Solution { - - public boolean isValid(String s) { - - Stack stack = new Stack(); - for( int i = 0 ; i < s.length() ; i ++ ) - if( s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') - stack.push(s.charAt(i)); - else{ - - if( stack.size() == 0 ) - return false; - - Character c = stack.pop(); - - Character match; - if( s.charAt(i) == ')' ) - match = '('; - else if( s.charAt(i) == ']' ) - match = '['; - else{ - assert s.charAt(i) == '}'; - match = '{'; - } - - if(c != match) - return false; - } - - if( stack.size() != 0 ) - return false; - - return true; - } - - private static void printBool(boolean b){ - System.out.println(b ? "True" : "False"); - } - - public static void main(String[] args) { - - printBool((new Solution()).isValid("()")); - printBool((new Solution()).isValid("()[]{}")); - printBool((new Solution()).isValid("(]")); - printBool((new Solution()).isValid("([)]")); - } -} diff --git a/0024-Swap-Nodes-in-Pairs/cpp-0024/CMakeLists.txt b/0024-Swap-Nodes-in-Pairs/cpp-0024/CMakeLists.txt deleted file mode 100755 index 3ff009a7..00000000 --- a/0024-Swap-Nodes-in-Pairs/cpp-0024/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0024) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0024 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0024-Swap-Nodes-in-Pairs/cpp-0024/main.cpp b/0024-Swap-Nodes-in-Pairs/cpp-0024/main.cpp deleted file mode 100755 index d62c3c0b..00000000 --- a/0024-Swap-Nodes-in-Pairs/cpp-0024/main.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/// Source : https://leetcode.com/problems/swap-nodes-in-pairs/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include - -using namespace std; - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -/// LinkedList Test Helper Functions -ListNode* createLinkedList(int arr[], int n){ - - if(n == 0) - return NULL; - - ListNode* head = new ListNode(arr[0]); - ListNode* curNode = head; - for(int i = 1 ; i < n ; i ++){ - curNode->next = new ListNode(arr[i]); - curNode = curNode->next; - } - - return head; -} - -void printLinkedList(ListNode* head){ - - if(head == NULL){ - cout<<"NULL"<val; - if(curNode->next != NULL) - cout << " -> "; - curNode = curNode->next; - } - - cout << endl; - - return; -} - -void deleteLinkedList(ListNode* head){ - - ListNode* curNode = head; - while(curNode != NULL){ - ListNode* delNode = curNode; - curNode = curNode->next; - delete delNode; - } - - return; -} - -// Time Complexity: O(n) -// Space Complexity: O(1) -class Solution { -public: - ListNode* swapPairs(ListNode* head) { - - ListNode* dummyHead = new ListNode(0); - dummyHead->next = head; - - ListNode* p = dummyHead; - while(p->next && p->next->next){ - ListNode* node1 = p->next; - ListNode* node2 = node1->next; - ListNode* next = node2->next; - node2->next = node1; - node1->next = next; - p->next = node2; - p = node1; - } - - ListNode* retHead = dummyHead->next; - delete dummyHead; - - return retHead; - } -}; - -int main() { - - int arr[] = {1, 2, 3, 4}; - int n = sizeof(arr) / sizeof(int); - - ListNode* head = createLinkedList(arr, n); - printLinkedList(head); - - head = Solution().swapPairs(head); - printLinkedList(head); - - deleteLinkedList(head); - - return 0; -} \ No newline at end of file diff --git a/0024-Swap-Nodes-in-Pairs/java-0024/src/ListNode.java b/0024-Swap-Nodes-in-Pairs/java-0024/src/ListNode.java deleted file mode 100755 index 8844dbea..00000000 --- a/0024-Swap-Nodes-in-Pairs/java-0024/src/ListNode.java +++ /dev/null @@ -1,36 +0,0 @@ -public class ListNode { - - public int val; - public ListNode next = null; - - public ListNode(int x) { - val = x; - } - - public ListNode (int[] arr){ - - if(arr == null || arr.length == 0) - throw new IllegalArgumentException("arr can not be empty"); - - this.val = arr[0]; - ListNode curNode = this; - for(int i = 1 ; i < arr.length ; i ++){ - curNode.next = new ListNode(arr[i]); - curNode = curNode.next; - } - } - - @Override - public String toString(){ - - StringBuilder s = new StringBuilder(""); - ListNode curNode = this; - while(curNode != null){ - s.append(Integer.toString(curNode.val)); - s.append(" -> "); - curNode = curNode.next; - } - s.append("NULL"); - return s.toString(); - } -} \ No newline at end of file diff --git a/0024-Swap-Nodes-in-Pairs/java-0024/src/Solution.java b/0024-Swap-Nodes-in-Pairs/java-0024/src/Solution.java deleted file mode 100755 index cbf69136..00000000 --- a/0024-Swap-Nodes-in-Pairs/java-0024/src/Solution.java +++ /dev/null @@ -1,38 +0,0 @@ -/// Source : https://leetcode.com/problems/swap-nodes-in-pairs/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -/// Time Complexity: O(n) -/// Space Complexity: O(1) -public class Solution { - - public ListNode swapPairs(ListNode head) { - - ListNode dummyHead = new ListNode(0); - dummyHead.next = head; - - ListNode p = dummyHead; - while(p.next != null && p.next.next != null ){ - ListNode node1 = p.next; - ListNode node2 = node1.next; - ListNode next = node2.next; - node2.next = node1; - node1.next = next; - p.next = node2; - p = node1; - } - - return dummyHead.next; - } - - public static void main(String[] args) { - - int[] arr = {1, 2, 3, 4}; - - ListNode head = new ListNode(arr); - System.out.println(head); - - head = (new Solution()).swapPairs(head); - System.out.println(head); - } -} diff --git a/0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/CMakeLists.txt b/0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/CMakeLists.txt deleted file mode 100755 index 16014cc2..00000000 --- a/0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_Remove_Duplicates_from_Sorted_Array) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_Remove_Duplicates_from_Sorted_Array ${SOURCE_FILES}) \ No newline at end of file diff --git a/0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/main.cpp b/0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/main.cpp deleted file mode 100755 index 358ee43a..00000000 --- a/0026-Remove-Duplicates-from-Sorted-Array/cpp-0026/main.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/// Source : https://leetcode.com/problems/remove-duplicates-from-sorted-array/ -/// Author : liuyubobobo -/// Time : 2016-12-06 - - -#include -#include -#include -#include - -using namespace std; - -/// Two pointers -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - int removeDuplicates(vector& nums) { - - if(nums.size() == 0) - return 0; - - int res = 1; - int index = nextDifferentCharacterIndex(nums, 1); - int i = 1; - while(index < nums.size()){ - res ++; - nums[i++] = nums[index]; - index = nextDifferentCharacterIndex(nums, index + 1); - } - - return res; - } - -private: - int nextDifferentCharacterIndex(const vector &nums, int p){ - for( ; p < nums.size() ; p ++ ) - if( nums[p] != nums[p - 1] ) - break; - return p; - } -}; - - -int main() { - - vector nums1 = {1, 1, 2}; - cout << Solution().removeDuplicates(nums1) << endl; - - return 0; -} \ No newline at end of file diff --git a/0075-Sort-Colors/cpp-0075/CMakeLists.txt b/0075-Sort-Colors/cpp-0075/CMakeLists.txt deleted file mode 100755 index 38f9ae75..00000000 --- a/0075-Sort-Colors/cpp-0075/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0075) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main2.cpp) -add_executable(cpp_0075 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0075-Sort-Colors/cpp-0075/main.cpp b/0075-Sort-Colors/cpp-0075/main.cpp deleted file mode 100755 index bc40f6ee..00000000 --- a/0075-Sort-Colors/cpp-0075/main.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/// Source : https://leetcode.com/problems/sort-colors/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -#include -#include -#include - -using namespace std; - -// Counting -// Time Complexity: O(n) -// Space Complexity: O(3) -class Solution { -public: - void sortColors(vector &nums) { - - int count[3] = {0}; - for(int i = 0 ; i < nums.size() ; i ++){ - assert(nums[i] >= 0 && nums[i] <= 2); - count[nums[i]] ++; - } - - int index = 0; - for(int i = 0 ; i < count[0] ; i ++) - nums[index++] = 0; - for(int i = 0 ; i < count[1] ; i ++) - nums[index++] = 1; - for(int i = 0 ; i < count[2] ; i ++) - nums[index++] = 2; - } -}; - - -void printArr(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - vector vec1 = {2, 2, 2, 1, 1, 0}; - Solution().sortColors(vec1); - printArr(vec1); - - vector vec2 = {2}; - Solution().sortColors(vec2); - printArr(vec2); - - return 0; -} diff --git a/0075-Sort-Colors/cpp-0075/main2.cpp b/0075-Sort-Colors/cpp-0075/main2.cpp deleted file mode 100755 index 257a83aa..00000000 --- a/0075-Sort-Colors/cpp-0075/main2.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/// Source : https://leetcode.com/problems/sort-colors/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -#include -#include -#include - -using namespace std; - -// Three Way Quick Sort -// Time Complexity: O(n) -// Space Complexity: O(1) -class Solution { -public: - void sortColors(vector &nums) { - - int zero = -1; // [0...zero] == 0 - int two = nums.size(); // [two...n-1] == 2 - for(int i = 0 ; i < two ; ){ - if(nums[i] == 1) - i ++; - else if (nums[i] == 2) - swap( nums[i] , nums[--two]); - else{ // nums[i] == 0 - assert(nums[i] == 0); - swap(nums[++zero] , nums[i++]); - } - } - } -}; - - -void printArr(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - vector vec1 = {2, 2, 2, 1, 1, 0}; - Solution().sortColors(vec1); - printArr(vec1); - - vector vec2 = {2}; - Solution().sortColors(vec2); - printArr(vec2); - - return 0; -} diff --git a/0075-Sort-Colors/java-0075/src/Solution1.java b/0075-Sort-Colors/java-0075/src/Solution1.java deleted file mode 100755 index bc09c317..00000000 --- a/0075-Sort-Colors/java-0075/src/Solution1.java +++ /dev/null @@ -1,39 +0,0 @@ -/// Source : https://leetcode.com/problems/sort-colors/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -// Counting -// Time Complexity: O(n) -// Space Complexity: O(3) -public class Solution1 { - - public void sortColors(int[] nums) { - - int[] count = {0, 0, 0}; - for(int i = 0 ; i < nums.length ; i ++){ - assert nums[i] >= 0 && nums[i] <= 2; - count[nums[i]] ++; - } - - int index = 0; - for(int i = 0 ; i < count[0] ; i ++) - nums[index++] = 0; - for(int i = 0 ; i < count[1] ; i ++) - nums[index++] = 1; - for(int i = 0 ; i < count[2] ; i ++) - nums[index++] = 2; - } - - public static void printArr(int[] nums){ - for(int num: nums) - System.out.print(num + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums = {2, 2, 2, 1, 1, 0}; - (new Solution1()).sortColors(nums); - printArr(nums); - } -} diff --git a/0075-Sort-Colors/java-0075/src/Solution2.java b/0075-Sort-Colors/java-0075/src/Solution2.java deleted file mode 100755 index 9f8ef01f..00000000 --- a/0075-Sort-Colors/java-0075/src/Solution2.java +++ /dev/null @@ -1,44 +0,0 @@ -/// Source : https://leetcode.com/problems/sort-colors/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -// Three Way Quick Sort -// Time Complexity: O(n) -// Space Complexity: O(1) -public class Solution2 { - - public void sortColors(int[] nums) { - - int zero = -1; // [0...zero] == 0 - int two = nums.length; // [two...n-1] == 2 - for(int i = 0 ; i < two ; ){ - if(nums[i] == 1) - i ++; - else if (nums[i] == 2) - swap(nums, i, --two); - else{ // nums[i] == 0 - assert nums[i] == 0; - swap(nums, ++zero, i++); - } - } - } - - private void swap(int[] nums, int i, int j){ - int t = nums[i]; - nums[i]= nums[j]; - nums[j] = t; - } - - public static void printArr(int[] nums){ - for(int num: nums) - System.out.print(num + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums = {2, 2, 2, 1, 1, 0}; - (new Solution2()).sortColors(nums); - printArr(nums); - } -} diff --git a/0086-Partition-List/cpp-0086/CMakeLists.txt b/0086-Partition-List/cpp-0086/CMakeLists.txt deleted file mode 100755 index b5ba07cd..00000000 --- a/0086-Partition-List/cpp-0086/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0086) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0086 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0086-Partition-List/cpp-0086/main.cpp b/0086-Partition-List/cpp-0086/main.cpp deleted file mode 100755 index ce8e3fad..00000000 --- a/0086-Partition-List/cpp-0086/main.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/// Source : https://leetcode.com/problems/partition-list/description/ -/// Author : liuyubobobo -/// Time : 2018-07-07 - -#include - -using namespace std; - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -/// Linear Scan -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - ListNode* partition(ListNode* head, int x) { - - ListNode* dummyHead1 = new ListNode(-1); - ListNode* dummyHead2 = new ListNode(-1); - ListNode* prev1 = dummyHead1; - ListNode* prev2 = dummyHead2; - - for(ListNode* cur = head ; cur != NULL ;){ - if(cur->val < x){ - prev1->next = cur; - cur = cur->next; - prev1 = prev1->next; - prev1->next = NULL; - } - else{ - prev2->next = cur; - cur = cur->next; - prev2 = prev2->next; - prev2->next = NULL; - } - } - - prev1->next = dummyHead2->next; - ListNode* ret = dummyHead1->next; - - delete dummyHead1; - delete dummyHead2; - return ret; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/0092-Reverse-Linked-List-II/cpp-0092/CMakeLists.txt b/0092-Reverse-Linked-List-II/cpp-0092/CMakeLists.txt deleted file mode 100755 index 3c0abe12..00000000 --- a/0092-Reverse-Linked-List-II/cpp-0092/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0092) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0092 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0092-Reverse-Linked-List-II/cpp-0092/main.cpp b/0092-Reverse-Linked-List-II/cpp-0092/main.cpp deleted file mode 100755 index 338ea4b8..00000000 --- a/0092-Reverse-Linked-List-II/cpp-0092/main.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/// Source : https://leetcode.com/problems/reverse-linked-list-ii/description/ -/// Author : liuyubobobo -/// Time : 2018-10-02 - -#include - -using namespace std; - - -/// Recursive -/// Time Complexity: O(n) -/// Space Complexity: O(n) - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -class Solution { -public: - ListNode* reverseBetween(ListNode* head, int m, int n) { - - ListNode* dummyHead = new ListNode(-1); - dummyHead->next = head; - - ListNode* pre = dummyHead; - for(int i = 0; i < m - 1; i ++){ - pre = pre->next - } - - ListNode* tail = pre->next; - ListNode* left; - pre->next = reverse(pre->next, n - m, left); - tail->next = left; - - ListNode* ret = dummyHead->next; - delete dummyHead; - return ret; - } - -private: - ListNode* reverse(ListNode* head, int index, ListNode* &left){ - - if(index == 0){ - left = head->next; - return head; - } - - ListNode* tail = head->next; - ListNode* ret = reverse(head->next, index - 1, left); - tail->next = head; - return ret; - } -}; - - -int main() { - - return 0; -} diff --git a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/CMakeLists.txt b/0094-Binary-Tree-Inorder-Traversal/cpp-0094/CMakeLists.txt deleted file mode 100755 index 783977e9..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0094) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main3.cpp) -add_executable(cpp_0094 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main.cpp b/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main.cpp deleted file mode 100755 index 0265badf..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include - -using namespace std; - - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { -public: - vector inorderTraversal(TreeNode* root) { - - vector res; - __inorderTraversal(root, res); - return res; - } - -private: - void __inorderTraversal(TreeNode* node, vector &res){ - - if( node ){ - __inorderTraversal(node->left, res); - res.push_back( node->val ); - __inorderTraversal(node->right, res); - } - } -}; - -int main() { - - return 0; -} diff --git a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main2.cpp b/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main2.cpp deleted file mode 100755 index 043483f2..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main2.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include -#include - -using namespace std; - - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// My Non-Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { - -private: - struct Command{ - string s; // go, print - TreeNode* node; - Command(string s, TreeNode* node): s(s), node(node){} - }; - -public: - vector inorderTraversal(TreeNode* root) { - - vector res; - if( root == NULL ) - return res; - - stack stack; - stack.push(Command("go", root)); - while( !stack.empty() ){ - Command command = stack.top(); - stack.pop(); - - if(command.s == "print") - res.push_back(command.node->val); - else{ - assert(command.s == "go"); - if(command.node->right) - stack.push(Command("go",command.node->right)); - stack.push(Command("print", command.node)); - if(command.node->left) - stack.push(Command("go",command.node->left)); - } - } - return res; - } -}; - -int main() { - - return 0; -} diff --git a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main3.cpp b/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main3.cpp deleted file mode 100755 index c10758e0..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main3.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2017-05-30 - -#include -#include -#include - -using namespace std; - - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// Classic Non-Recursive algorithm for inorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { - -public: - vector inorderTraversal(TreeNode* root) { - - vector res; - if( root == NULL ) - return res; - - stack stack; - TreeNode* cur = root; - while(cur != NULL || !stack.empty()){ - - while(cur != NULL){ - stack.push(cur); - cur = cur->left; - } - - cur = stack.top(); - stack.pop(); - res.push_back(cur->val); - cur = cur->right; - - } - return res; - } -}; - -int main() { - - return 0; -} diff --git a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main4.cpp b/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main4.cpp deleted file mode 100755 index 2c950952..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main4.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2017-05-30 - -#include -#include -#include - -using namespace std; - - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// Another Classic Non-Recursive algorithm for inorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { - -public: - vector inorderTraversal(TreeNode* root) { - - vector res; - if( root == NULL ) - return res; - - stack stack; - TreeNode* cur = root; - while(cur != NULL || !stack.empty()){ - - if(cur != NULL){ - stack.push(cur); - cur = cur->left; - } - else { - cur = stack.top(); - stack.pop(); - res.push_back(cur->val); - cur = cur->right; - } - } - return res; - } -}; - -int main() { - - return 0; -} diff --git a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main5.cpp b/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main5.cpp deleted file mode 100755 index d5de25a4..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/cpp-0094/main5.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -#include -#include -#include - -using namespace std; - - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// InOrder Morris Traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(1) -class Solution { - -public: - vector inorderTraversal(TreeNode* root) { - - vector res; - if( root == NULL ) - return res; - - TreeNode* cur = root; - while(cur != NULL){ - - if(cur->left == NULL){ - res.push_back(cur->val); - cur = cur->right; - } - else{ - TreeNode* prev = cur->left; - while(prev->right != NULL && prev->right != cur) - prev = prev->right; - - if(prev->right == NULL){ - prev->right = cur; - cur = cur->left; - } - else{ - prev->right = NULL; - res.push_back(cur->val); - cur = cur->right; - } - } - } - - return res; - } -}; - -int main() { - - return 0; -} diff --git a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution1.java b/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution1.java deleted file mode 100755 index 1cf54d2a..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution1.java +++ /dev/null @@ -1,27 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.ArrayList; -import java.util.List; - -// Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution1 { - - public List inorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - inorderTraversal(root, res); - return res; - } - - private void inorderTraversal(TreeNode node, List list){ - if(node != null){ - inorderTraversal(node.left, list); - list.add(node.val); - inorderTraversal(node.right, list); - } - } -} diff --git a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution2.java b/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution2.java deleted file mode 100755 index c2f9507f..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution2.java +++ /dev/null @@ -1,48 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// My Non-Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution2 { - - private class Command{ - String s; // go, print - TreeNode node; - Command(String s, TreeNode node){ - this.s = s; - this.node = node; - } - }; - - public List inorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack(); - stack.push(new Command("go", root)); - while(!stack.empty()){ - Command command = stack.pop(); - - if(command.s.equals("print")) - res.add(command.node.val); - else{ - assert command.s.equals("go"); - if(command.node.right != null) - stack.push(new Command("go",command.node.right)); - stack.push(new Command("print", command.node)); - if(command.node.left != null) - stack.push(new Command("go",command.node.left)); - } - } - return res; - } - -} diff --git a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution3.java b/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution3.java deleted file mode 100755 index bce6a201..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution3.java +++ /dev/null @@ -1,35 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Classic Non-Recursive algorithm for inorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution3 { - - public List inorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack<>(); - TreeNode cur = root; - while(cur != null || !stack.empty()){ - - while(cur != null){ - stack.push(cur); - cur = cur.left; - } - - cur = stack.pop(); - res.add(cur.val); - cur = cur.right; - } - return res; - } -} diff --git a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution4.java b/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution4.java deleted file mode 100755 index 1d76d12c..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution4.java +++ /dev/null @@ -1,36 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Another Classic Non-Recursive algorithm for inorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution4 { - - public List inorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack<>(); - TreeNode cur = root; - while(cur != null || !stack.empty()){ - - if(cur != null){ - stack.push(cur); - cur = cur.left; - } - else{ - cur = stack.pop(); - res.add(cur.val); - cur = cur.right; - } - } - return res; - } -} diff --git a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution5.java b/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution5.java deleted file mode 100755 index 41ec5fae..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/Solution5.java +++ /dev/null @@ -1,45 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Inorder Morris Traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(1) -public class Solution5 { - - public List inorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - TreeNode cur = root; - while(cur != null){ - - if(cur.left == null){ - res.add(cur.val); - cur = cur.right; - } - else{ - TreeNode prev = cur.left; - while(prev.right != null && prev.right != cur) - prev = prev.right; - - if(prev.right == null){ - prev.right = cur; - cur = cur.left; - } - else{ - prev.right = null; - res.add(cur.val); - cur = cur.right; - } - } - } - return res; - } -} diff --git a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/TreeNode.java b/0094-Binary-Tree-Inorder-Traversal/java-0094/src/TreeNode.java deleted file mode 100755 index f408f764..00000000 --- a/0094-Binary-Tree-Inorder-Traversal/java-0094/src/TreeNode.java +++ /dev/null @@ -1,7 +0,0 @@ -// Definition for a binary tree node. -public class TreeNode { - int val; - TreeNode left; - TreeNode right; - TreeNode(int x) { val = x; } -} \ No newline at end of file diff --git a/0102-Binary-Tree-Level-Order-Traversal/cpp-0102/CMakeLists.txt b/0102-Binary-Tree-Level-Order-Traversal/cpp-0102/CMakeLists.txt deleted file mode 100755 index f6643a5f..00000000 --- a/0102-Binary-Tree-Level-Order-Traversal/cpp-0102/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0102) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main2.cpp) -add_executable(cpp_0102 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main.cpp b/0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main.cpp deleted file mode 100755 index 12313dd9..00000000 --- a/0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -/// BFS -/// Time Complexity: O(n), where n is the number of nodes in the tree -/// Space Complexity: O(n) -class Solution { -public: - vector> levelOrder(TreeNode* root) { - - vector> res; - if(root == NULL) - return res; - - queue> q; - q.push(make_pair(root, 0)); - - while(!q.empty()){ - - TreeNode* node = q.front().first; - int level = q.front().second; - q.pop(); - - if(level == res.size()) - res.push_back(vector()); - assert( level < res.size() ); - - res[level].push_back(node->val); - if(node->left) - q.push(make_pair(node->left, level + 1 )); - if(node->right) - q.push(make_pair(node->right, level + 1 )); - } - - return res; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main2.cpp b/0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main2.cpp deleted file mode 100755 index b1494a4f..00000000 --- a/0102-Binary-Tree-Level-Order-Traversal/cpp-0102/main2.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-10-16 - -#include -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -/// BFS -/// No need to store level information in the queue :-) -/// -/// Time Complexity: O(n), where n is the number of nodes in the tree -/// Space Complexity: O(n) -class Solution { -public: - vector> levelOrder(TreeNode* root) { - - vector> res; - if(root == NULL) - return res; - - queue q; - q.push(root); - int level_num = 1; - - while(!q.empty()){ - - int new_level_num = 0; - vector level; - for(int i = 0; i < level_num; i ++){ - TreeNode* node = q.front(); - q.pop(); - level.push_back(node->val); - - if(node->left){ - q.push(node->left); - new_level_num ++; - } - if(node->right){ - q.push(node->right); - new_level_num ++; - } - } - - res.push_back(level); - level_num = new_level_num; - } - - return res; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution.java b/0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution.java deleted file mode 100755 index d654fd95..00000000 --- a/0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution.java +++ /dev/null @@ -1,43 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.ArrayList; -import java.util.List; -import java.util.LinkedList; -import javafx.util.Pair; - -/// BFS -/// Time Complexity: O(n), where n is the number of nodes in the tree -/// Space Complexity: O(n) -class Solution { - - public List> levelOrder(TreeNode root) { - - ArrayList> res = new ArrayList<>(); - if(root == null) - return res; - - LinkedList> queue = new LinkedList<>(); - queue.addLast(new Pair<>(root, 0)); - - while(!queue.isEmpty()){ - - Pair front = queue.removeFirst(); - TreeNode node = front.getKey(); - int level = front.getValue(); - - if(level == res.size()) - res.add(new ArrayList<>()); - assert level < res.size(); - - res.get(level).add(node.val); - if(node.left != null) - queue.addLast(new Pair<>(node.left, level + 1)); - if(node.right != null) - queue.addLast(new Pair<>(node.right, level + 1)); - } - - return res; - } -} diff --git a/0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution2.java b/0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution2.java deleted file mode 100755 index 269d9366..00000000 --- a/0102-Binary-Tree-Level-Order-Traversal/java-0102/src/Solution2.java +++ /dev/null @@ -1,51 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-10-16 - -import java.util.ArrayList; -import java.util.List; -import java.util.LinkedList; -import java.util.Queue; - -/// BFS -/// No need to store level information in the queue :-) -/// -/// Time Complexity: O(n), where n is the number of nodes in the tree -/// Space Complexity: O(n) -class Solution2 { - - public List> levelOrder(TreeNode root) { - - ArrayList> res = new ArrayList<>(); - if(root == null) - return res; - - Queue queue = new LinkedList<>(); - queue.add(root); - int levelNum = 1; - - while(!queue.isEmpty()){ - - int newLevelNum = 0; - ArrayList level = new ArrayList<>(); - for(int i = 0; i < levelNum; i ++){ - TreeNode node = queue.remove(); - level.add(node.val); - - if(node.left != null){ - queue.add(node.left); - newLevelNum ++; - } - if(node.right != null){ - queue.add(node.right); - newLevelNum ++; - } - } - - res.add(level); - levelNum = newLevelNum; - } - - return res; - } -} diff --git a/0102-Binary-Tree-Level-Order-Traversal/java-0102/src/TreeNode.java b/0102-Binary-Tree-Level-Order-Traversal/java-0102/src/TreeNode.java deleted file mode 100755 index f408f764..00000000 --- a/0102-Binary-Tree-Level-Order-Traversal/java-0102/src/TreeNode.java +++ /dev/null @@ -1,7 +0,0 @@ -// Definition for a binary tree node. -public class TreeNode { - int val; - TreeNode left; - TreeNode right; - TreeNode(int x) { val = x; } -} \ No newline at end of file diff --git a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/CMakeLists.txt b/0144-Binary-Tree-Preorder-Traversal/cpp-0144/CMakeLists.txt deleted file mode 100755 index 28d00385..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0144) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main5.cpp) -add_executable(cpp_0144 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main.cpp b/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main.cpp deleted file mode 100755 index 0ac90928..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include - -using namespace std; - - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { -public: - vector preorderTraversal(TreeNode* root) { - - vector res; - preorderTraversal(root, res); - return res; - } - -private: - void preorderTraversal(TreeNode* node, vector &res){ - - if(node){ - res.push_back(node->val); - preorderTraversal(node->left, res); - preorderTraversal(node->right, res); - } - } -}; - -int main() { - - return 0; -} diff --git a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main2.cpp b/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main2.cpp deleted file mode 100755 index 7ab68de6..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main2.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// My Non-Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { - -private: - struct Command{ - string s; // go, print - TreeNode* node; - Command(string s, TreeNode* node): s(s), node(node){} - }; - -public: - vector preorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack; - stack.push(Command("go", root)); - while(!stack.empty()){ - Command command = stack.top(); - stack.pop(); - - if(command.s == "print") - res.push_back(command.node->val); - else{ - assert(command.s == "go"); - if(command.node->right) - stack.push(Command("go",command.node->right)); - if(command.node->left) - stack.push(Command("go",command.node->left)); - stack.push(Command("print", command.node)); - } - } - return res; - } -}; - -int main() { - - return 0; -} diff --git a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main3.cpp b/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main3.cpp deleted file mode 100755 index 07280b2a..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main3.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// Classic Non-Recursive algorithm for preorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { - -public: - vector preorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack; - stack.push(root); - while(!stack.empty()){ - TreeNode* curNode = stack.top(); - stack.pop(); - res.push_back(curNode->val); - - if(curNode->right) - stack.push(curNode->right); - if(curNode->left) - stack.push(curNode->left); - } - return res; - } -}; - -int main() { - - return 0; -} diff --git a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main4.cpp b/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main4.cpp deleted file mode 100755 index 62985356..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main4.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -#include -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// Another Classic Non-Recursive algorithm for preorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { - -public: - vector preorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack; - TreeNode* cur = root; - while(cur != NULL || !stack.empty()){ - while(cur != NULL){ - res.push_back(cur->val); - stack.push(cur); - cur = cur->left; - } - - cur = stack.top(); - stack.pop(); - cur = cur->right; - } - return res; - } -}; - - -void print_vec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - TreeNode* root = new TreeNode(1); - root->right = new TreeNode(2); - root->right->left = new TreeNode(3); - vector res = Solution().preorderTraversal(root); - print_vec(res); - - return 0; -} diff --git a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main5.cpp b/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main5.cpp deleted file mode 100755 index 47f44151..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main5.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -#include -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// Another Classic Non-Recursive algorithm for preorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { - -public: - vector preorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack; - TreeNode* cur = root; - while(cur != NULL || !stack.empty()){ - if(cur != NULL){ - res.push_back(cur->val); - stack.push(cur); - cur = cur->left; - } - else{ - cur = stack.top(); - stack.pop(); - cur = cur->right; - } - } - return res; - } -}; - - -void print_vec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - TreeNode* root = new TreeNode(1); - root->right = new TreeNode(2); - root->right->left = new TreeNode(3); - vector res = Solution().preorderTraversal(root); - print_vec(res); - - return 0; -} diff --git a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main6.cpp b/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main6.cpp deleted file mode 100755 index 0aa0dcaa..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/cpp-0144/main6.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-29 - -#include -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// PreOrder Morris Traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(1) -class Solution { - -public: - vector preorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - TreeNode* cur = root; - while(cur != NULL){ - if(cur->left == NULL){ - res.push_back(cur->val); - cur = cur->right; - } - else{ - TreeNode* prev = cur->left; - while(prev->right != NULL && prev->right != cur) - prev = prev->right; - - if(prev->right == NULL){ - res.push_back(cur->val); - prev->right = cur; - cur = cur->left; - } - else{ - prev->right = NULL; - cur = cur->right; - } - } - } - - return res; - } -}; - -int main() { - - return 0; -} diff --git a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution1.java b/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution1.java deleted file mode 100755 index ba66bee9..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution1.java +++ /dev/null @@ -1,26 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 -import java.util.ArrayList; -import java.util.List; - -// Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution1 { - - public List preorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - preorderTraversal(root, res); - return res; - } - - private void preorderTraversal(TreeNode node, List list){ - if(node != null){ - list.add(node.val); - preorderTraversal(node.left, list); - preorderTraversal(node.right, list); - } - } -} diff --git a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution2.java b/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution2.java deleted file mode 100755 index 28bb5173..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution2.java +++ /dev/null @@ -1,48 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// My Non-Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution2 { - - private class Command{ - String s; // go, print - TreeNode node; - Command(String s, TreeNode node){ - this.s = s; - this.node = node; - } - }; - - public List preorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack(); - stack.push(new Command("go", root)); - while(!stack.empty()){ - Command command = stack.pop(); - - if(command.s.equals("print")) - res.add(command.node.val); - else{ - assert command.s.equals("go"); - if(command.node.right != null) - stack.push(new Command("go",command.node.right)); - if(command.node.left != null) - stack.push(new Command("go",command.node.left)); - stack.push(new Command("print", command.node)); - } - } - return res; - } - -} diff --git a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution3.java b/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution3.java deleted file mode 100755 index 09ebc934..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution3.java +++ /dev/null @@ -1,34 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Classic Non-Recursive algorithm for preorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution3 { - - public List preorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack(); - stack.push(root); - while(!stack.empty()){ - TreeNode curNode = stack.pop(); - res.add(curNode.val); - - if(curNode.right != null) - stack.push(curNode.right); - if(curNode.left != null) - stack.push(curNode.left); - } - return res; - } - -} diff --git a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution4.java b/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution4.java deleted file mode 100755 index 290717ab..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution4.java +++ /dev/null @@ -1,34 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Another Classic Non-Recursive algorithm for preorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution4 { - - public List preorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack(); - TreeNode cur = root; - while(cur != null || !stack.isEmpty()){ - while(cur != null){ - res.add(cur.val); - stack.push(cur); - cur = cur.left; - } - - cur = stack.pop(); - cur = cur.right; - } - return res; - } -} diff --git a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution5.java b/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution5.java deleted file mode 100755 index 68d12ec3..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution5.java +++ /dev/null @@ -1,35 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Another Classic Non-Recursive algorithm for preorder traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution5 { - - public List preorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack(); - TreeNode cur = root; - while(cur != null || !stack.isEmpty()){ - if(cur != null){ - res.add(cur.val); - stack.push(cur); - cur = cur.left; - } - else{ - cur = stack.pop(); - cur = cur.right; - } - } - return res; - } -} diff --git a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution6.java b/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution6.java deleted file mode 100755 index 9dbb2008..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/Solution6.java +++ /dev/null @@ -1,45 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-29 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// PreOrder Morris Traversal -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(1) -public class Solution6 { - - public List preorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - TreeNode cur = root; - while(cur != null){ - if(cur.left == null){ - res.add(cur.val); - cur = cur.right; - } - else{ - TreeNode prev = cur.left; - while(prev.right != null && prev.right != cur) - prev = prev.right; - - if(prev.right == null){ - res.add(cur.val); - prev.right = cur; - cur = cur.left; - } - else{ - prev.right = null; - cur = cur.right; - } - } - } - - return res; - } -} diff --git a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/TreeNode.java b/0144-Binary-Tree-Preorder-Traversal/java-0144/src/TreeNode.java deleted file mode 100755 index f408f764..00000000 --- a/0144-Binary-Tree-Preorder-Traversal/java-0144/src/TreeNode.java +++ /dev/null @@ -1,7 +0,0 @@ -// Definition for a binary tree node. -public class TreeNode { - int val; - TreeNode left; - TreeNode right; - TreeNode(int x) { val = x; } -} \ No newline at end of file diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/CMakeLists.txt b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/CMakeLists.txt deleted file mode 100755 index 9407f7ea..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0145) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main8.cpp) -add_executable(cpp_0145 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main.cpp b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main.cpp deleted file mode 100755 index d394d7b2..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include - -using namespace std; - - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { -public: - vector postorderTraversal(TreeNode* root) { - - vector res; - __postorderTraversal(root, res); - return res; - } - -private: - void __postorderTraversal(TreeNode* node, vector &res){ - - if( node ){ - __postorderTraversal(node->left, res); - __postorderTraversal(node->right, res); - res.push_back(node->val); - } - } -}; - -int main() { - - return 0; -} diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main2.cpp b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main2.cpp deleted file mode 100755 index 152e5ff0..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main2.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// My Non-Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { - -private: - struct Command{ - string s; // go, print - TreeNode* node; - Command(string s, TreeNode* node): s(s), node(node){} - }; - -public: - vector postorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack; - stack.push(Command("go", root) ); - while(!stack.empty()){ - Command command = stack.top(); - stack.pop(); - - if(command.s == "print") - res.push_back(command.node->val); - else{ - assert(command.s == "go"); - stack.push(Command("print", command.node)); - if(command.node->right) - stack.push(Command("go",command.node->right)); - if(command.node->left) - stack.push(Command("go",command.node->left)); - } - } - return res; - } -}; - -int main() { - - return 0; -} - diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main3.cpp b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main3.cpp deleted file mode 100755 index 08c7b0ab..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main3.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// Non-Recursive -// Using a tag to record whether the node has been visited -// -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -class Solution { - -private: - struct TagNode{ - TreeNode* node; - bool isFirst; - TagNode(TreeNode* node): node(node), isFirst(false){} - }; - -public: - vector postorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack; - TreeNode* cur = root; - while(cur != NULL || !stack.empty()){ - - while(cur != NULL){ - stack.push(TagNode(cur)); - cur = cur->left; - } - - TagNode tagNode = stack.top(); - stack.pop(); - cur = tagNode.node; - if(tagNode.isFirst == false){ - tagNode.isFirst = true; - stack.push(tagNode); - cur = cur->right; - } - else{ - res.push_back(cur->val); - cur = NULL; - }; - } - return res; - } -}; - -int main() { - - return 0; -} - diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main4.cpp b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main4.cpp deleted file mode 100755 index 4ca66408..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main4.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// Non-Recursive -// Using two stacks, Reverse the Preorder Traversal! -// -// Time Complexity: O(n) -// Space Complexity: O(n) -class Solution { - -public: - vector postorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack, output; - - stack.push(root); - while(!stack.empty()){ - - TreeNode* node = stack.top(); - stack.pop(); - output.push(node); - - if(node->left != NULL) - stack.push(node->left); - if(node->right != NULL) - stack.push(node->right); - } - - while(!output.empty()){ - res.push_back((output.top())->val); - output.pop(); - } - - return res; - } -}; - -int main() { - - return 0; -} - diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main5.cpp b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main5.cpp deleted file mode 100755 index fa5926c4..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main5.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// Non-Recursive -// Using two stacks, Reverse the Preorder Traversal! -// -// Time Complexity: O(n) -// Space Complexity: O(n) -class Solution { - -public: - vector postorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack, output; - - TreeNode* p = root; - while(p != NULL || !stack.empty()){ - if(p != NULL){ - stack.push(p); - output.push(p); - p = p->right; - } - else{ - p = stack.top(); - stack.pop(); - p = p->left; - } - } - - while(!output.empty()){ - res.push_back((output.top())->val); - output.pop(); - } - - return res; - } -}; - -int main() { - - return 0; -} - diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main6.cpp b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main6.cpp deleted file mode 100755 index 63f0a18d..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main6.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-31 - -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// Non-Recursive -// Using a pre pointer to record the last visted node -// -// Time Complexity: O(n) -// Space Complexity: O(h) -class Solution { - -public: - vector postorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack; - TreeNode* pre = NULL; - - stack.push(root); - while(!stack.empty()){ - - TreeNode* node = stack.top(); - stack.pop(); - if((node->left == NULL && node->right == NULL) || - (pre != NULL && pre == node->left && node->right == NULL) || - (pre != NULL && pre == node->right)){ - res.push_back(node->val); - pre = node; - } - else{ - stack.push(node); - - if(node->right != NULL) - stack.push(node->right); - if(node->left != NULL) - stack.push(node->left); - } - } - - return res; - } -}; - - -void print_vec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - TreeNode* root = new TreeNode(1); - root->right = new TreeNode(2); - root->right->left = new TreeNode(3); - print_vec(Solution().postorderTraversal(root)); - - return 0; -} - diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main7.cpp b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main7.cpp deleted file mode 100755 index d3b436ec..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main7.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-31 - -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// Classic Non-Recursive -// Using a pre pointer to record the last visted node -// -// Time Complexity: O(n) -// Space Complexity: O(h) -class Solution { - -public: - vector postorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack; - TreeNode* pre = NULL; - TreeNode* cur = root; - - while(cur != NULL || !stack.empty()){ - - while(cur != NULL){ - stack.push(cur); - cur = cur->left; - } - - cur = stack.top(); - stack.pop(); - - if(cur->right == NULL || pre == cur->right){ - res.push_back(cur->val); - pre = cur; - cur = NULL; - } - else{ - stack.push(cur); - cur = cur->right; - } - } - - return res; - } -}; - - -void print_vec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - TreeNode* root = new TreeNode(1); - root->right = new TreeNode(2); - root->right->left = new TreeNode(3); - print_vec(Solution().postorderTraversal(root)); - - return 0; -} - diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main8.cpp b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main8.cpp deleted file mode 100755 index c122f894..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main8.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-31 - -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// Classic Non-Recursive -// Using a pre pointer to record the last visted node -// -// Time Complexity: O(n) -// Space Complexity: O(h) -class Solution { - -public: - vector postorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - stack stack; - TreeNode* pre = NULL; - TreeNode* cur = root; - - while(cur != NULL || !stack.empty()){ - if(cur != NULL){ - stack.push(cur); - cur = cur->left; - } - else{ - cur = stack.top(); - stack.pop(); - - if(cur->right == NULL || pre == cur->right){ - res.push_back(cur->val); - pre = cur; - cur = NULL; - } - else{ - stack.push(cur); - cur = cur->right; - } - } - } - - return res; - } -}; - - -void print_vec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - TreeNode* root = new TreeNode(1); - root->right = new TreeNode(2); - root->right->left = new TreeNode(3); - print_vec(Solution().postorderTraversal(root)); - - return 0; -} - diff --git a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main9.cpp b/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main9.cpp deleted file mode 100755 index e898fec0..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/cpp-0145/main9.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-31 - -#include -#include -#include - -using namespace std; - -/// Definition for a binary tree node. -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - - -// Morris PostOrder Traversal -// -// Time Complexity: O(n) -// Space Complexity: O(1) -class Solution { - -public: - vector postorderTraversal(TreeNode* root) { - - vector res; - if(root == NULL) - return res; - - TreeNode* dummyRoot = new TreeNode(-1); - dummyRoot->left = root; - - TreeNode* cur = dummyRoot; - while(cur != NULL){ - if(cur->left == NULL) - cur = cur->right; - else{ - TreeNode* prev = cur->left; - while(prev->right != NULL && prev->right != cur) - prev = prev->right; - - if(prev->right == NULL){ - prev->right = cur; - cur = cur->left; - } - else{ - prev->right = NULL; - reverseTraversal(cur->left, res); - cur = cur->right; - } - } - } - delete dummyRoot; - - return res; - } - -private: - void reverseTraversal(TreeNode* node, vector& res){ - - int start = res.size(); - while(node != NULL){ - res.push_back(node->val); - node = node->right; - } - reverse(res.begin() + start, res.end()); - } -}; - - -void print_vec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - TreeNode* root = new TreeNode(1); - root->right = new TreeNode(2); - root->right->left = new TreeNode(3); - print_vec(Solution().postorderTraversal(root)); - - return 0; -} - diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution1.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution1.java deleted file mode 100755 index ee2f01b2..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution1.java +++ /dev/null @@ -1,27 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.ArrayList; -import java.util.List; - -// Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution1 { - - public List postorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - postorderTraversal(root, res); - return res; - } - - private void postorderTraversal(TreeNode node, List list){ - if(node != null){ - postorderTraversal(node.left, list); - postorderTraversal(node.right, list); - list.add(node.val); - } - } -} diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution2.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution2.java deleted file mode 100755 index b6fb9ed5..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution2.java +++ /dev/null @@ -1,48 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// My Non-Recursive -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution2 { - - private class Command{ - String s; // go, print - TreeNode node; - Command(String s, TreeNode node){ - this.s = s; - this.node = node; - } - }; - - public List postorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack(); - stack.push(new Command("go", root)); - while(!stack.empty()){ - Command command = stack.pop(); - - if(command.s.equals("print")) - res.add(command.node.val); - else{ - assert command.s.equals("go"); - stack.push(new Command("print", command.node)); - if(command.node.right != null) - stack.push(new Command("go",command.node.right)); - if(command.node.left != null) - stack.push(new Command("go",command.node.left)); - } - } - return res; - } - -} diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution3.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution3.java deleted file mode 100755 index 892f59a3..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution3.java +++ /dev/null @@ -1,54 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Non-Recursive -// Using a tag to record whether the node has been visited -// -// Time Complexity: O(n), n is the node number in the tree -// Space Complexity: O(h), h is the height of the tree -public class Solution3 { - - private class TagNode{ - TreeNode node; - boolean isFirst; - TagNode(TreeNode node){ - this.node = node; - this.isFirst = false; - } - }; - - public List postorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack<>(); - TreeNode cur = root; - while(cur != null || !stack.empty()){ - - while(cur != null){ - stack.push(new TagNode(cur)); - cur = cur.left; - } - - TagNode tagNode = stack.pop(); - cur = tagNode.node; - if(tagNode.isFirst == false){ - tagNode.isFirst = true; - stack.push(tagNode); - cur = cur.right; - } - else{ - res.add(cur.val); - cur = null; - } - } - return res; - } -} diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution4.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution4.java deleted file mode 100755 index 18a950d2..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution4.java +++ /dev/null @@ -1,41 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Non-Recursive -// Using two stacks, Reverse the Preorder Traversal! -// -// Time Complexity: O(n) -// Space Complexity: O(n) -public class Solution4 { - - public List postorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack<>(); - Stack output = new Stack<>(); - - stack.push(root); - while(!stack.empty()){ - - TreeNode cur = stack.pop(); - output.push(cur.val); - - if(cur.left != null) - stack.push(cur.left); - if(cur.right != null) - stack.push(cur.right); - } - - while(!output.empty()) - res.add(output.pop()); - return res; - } -} diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution5.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution5.java deleted file mode 100755 index 5d05befa..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution5.java +++ /dev/null @@ -1,40 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-30 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; -import java.util.LinkedList; - -// Non-Recursive -// Using two stacks, Reverse the Preorder Traversal! -// -// Time Complexity: O(n) -// Space Complexity: O(n) -public class Solution5 { - - public List postorderTraversal(TreeNode root){ - - Stack stack = new Stack<>(); - LinkedList output = new LinkedList<>(); - - TreeNode p = root; - while(p != null || !stack.isEmpty()){ - if(p != null){ - stack.push(p); - output.push(p); - p = p.right; - } - else{ - p = stack.pop(); - p = p.left; - } - } - - List res = new ArrayList<>(); - while(!output.isEmpty()) - res.add(output.pop().val); - return res; - } -} diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution6.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution6.java deleted file mode 100755 index ae187962..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution6.java +++ /dev/null @@ -1,45 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-31 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Non-Recursive -// Using a pre pointer to record the last visted node -// -// Time Complexity: O(n) -// Space Complexity: O(h) -public class Solution6 { - - public List postorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack<>(); - TreeNode pre = null; - - stack.push(root); - while(!stack.empty()){ - - TreeNode cur = stack.pop(); - if((cur.left == null && cur.right == null) || - (pre != null && pre == cur.left && cur.right == null) || - (pre != null && pre == cur.right)){ - res.add(cur.val); - pre = cur; - } - else{ - stack.push(cur); - if(cur.right != null) - stack.push(cur.right); - if(cur.left != null) - stack.push(cur.left); - } - } - return res; - } -} diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution7.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution7.java deleted file mode 100755 index f945dfa5..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution7.java +++ /dev/null @@ -1,46 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-31 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Classic Non-Recursive -// Using a pre pointer to record the last visted node -// -// Time Complexity: O(n) -// Space Complexity: O(h) -public class Solution7 { - - public List postorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack<>(); - TreeNode pre = null; - TreeNode cur = root; - - while(cur != null || !stack.empty()){ - - while(cur != null){ - stack.push(cur); - cur = cur.left; - } - - cur = stack.pop(); - if(cur.right == null || pre == cur.right){ - res.add(cur.val); - pre = cur; - cur = null; - } - else{ - stack.push(cur); - cur = cur.right; - } - } - return res; - } -} diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution8.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution8.java deleted file mode 100755 index d0c3d68d..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution8.java +++ /dev/null @@ -1,47 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-31 - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -// Classic Non-Recursive -// Using a pre pointer to record the last visted node -// -// Time Complexity: O(n) -// Space Complexity: O(h) -public class Solution8 { - - public List postorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - Stack stack = new Stack<>(); - TreeNode pre = null; - TreeNode cur = root; - - while(cur != null || !stack.empty()){ - - if(cur != null){ - stack.push(cur); - cur = cur.left; - } - else{ - cur = stack.pop(); - if(cur.right == null || pre == cur.right){ - res.add(cur.val); - pre = cur; - cur = null; - } - else{ - stack.push(cur); - cur = cur.right; - } - } - } - return res; - } -} diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution9.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution9.java deleted file mode 100755 index 2862c0cc..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution9.java +++ /dev/null @@ -1,65 +0,0 @@ -/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -/// Author : liuyubobobo -/// Time : 2018-05-31 - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Stack; - -// Morris PostOrder Traversal -// -// Time Complexity: O(n) -// Space Complexity: O(1) -public class Solution9 { - - public List postorderTraversal(TreeNode root) { - - ArrayList res = new ArrayList(); - if(root == null) - return res; - - TreeNode dummyRoot = new TreeNode(-1); - dummyRoot.left = root; - - TreeNode cur = dummyRoot; - while(cur != null){ - if(cur.left == null) - cur = cur.right; - else{ - TreeNode pre = cur.left; - while(pre.right != null && pre.right != cur) - pre = pre.right; - - if(pre.right == null){ - pre.right = cur; - cur = cur.left; - } - else{ - pre.right = null; - reverseTraversal(cur.left, res); - cur = cur.right; - } - } - } - return res; - } - - private void reverseTraversal(TreeNode node, ArrayList res){ - int start = res.size(); - while(node != null){ - res.add(node.val); - node = node.right; - } - - int i = start, j = res.size() - 1; - while(i < j){ - Integer t = res.get(i); - res.set(i, res.get(j)); - res.set(j, t); - - i ++; - j --; - } - } -} diff --git a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/TreeNode.java b/0145-Binary-Tree-Postorder-Traversal/java-0145/src/TreeNode.java deleted file mode 100755 index f408f764..00000000 --- a/0145-Binary-Tree-Postorder-Traversal/java-0145/src/TreeNode.java +++ /dev/null @@ -1,7 +0,0 @@ -// Definition for a binary tree node. -public class TreeNode { - int val; - TreeNode left; - TreeNode right; - TreeNode(int x) { val = x; } -} \ No newline at end of file diff --git a/0150-Evaluate-Reverse-Polish-Notation/cpp-0150/CMakeLists.txt b/0150-Evaluate-Reverse-Polish-Notation/cpp-0150/CMakeLists.txt deleted file mode 100755 index 1ced3499..00000000 --- a/0150-Evaluate-Reverse-Polish-Notation/cpp-0150/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0150) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0150 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0150-Evaluate-Reverse-Polish-Notation/cpp-0150/main.cpp b/0150-Evaluate-Reverse-Polish-Notation/cpp-0150/main.cpp deleted file mode 100755 index 5c10301f..00000000 --- a/0150-Evaluate-Reverse-Polish-Notation/cpp-0150/main.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/// Source : https://leetcode.com/problems/evaluate-reverse-polish-notation/description/ -/// Author : liuyubobobo -/// Time : 2018-08-29 - -#include -#include -#include - -using namespace std; - - -/// Two stacks -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { -public: - int evalRPN(vector& tokens) { - - stack nums; - stack ops; - for(const string& s: tokens){ - if(s == "+" || s == "-" || s == "*" || s == "/"){ - int a = nums.top(); - nums.pop(); - int b = nums.top(); - nums.pop(); - - if(s == "+"){ - nums.push(b + a); - }else if(s == "-"){ - nums.push(b - a); - } else if(s == "*"){ - nums.push(b * a); - }else if(s == "/"){ - nums.push(b / a); - } - } - else{ - nums.push(atoi(s.c_str())); - } - } - return nums.top(); - } -}; - - -int main() { - - return 0; -} diff --git a/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/CMakeLists.txt b/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/CMakeLists.txt deleted file mode 100755 index cb5efa14..00000000 --- a/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(04_Two_Sum_II) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main3.cpp) -add_executable(04_Two_Sum_II ${SOURCE_FILES}) \ No newline at end of file diff --git a/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main.cpp b/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main.cpp deleted file mode 100755 index e8be76d9..00000000 --- a/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -#include -#include -#include - -using namespace std; - -// Brute Force -// Time Complexity: O(n^2) -// Space Complexity: O(1) -class Solution { -public: - vector twoSum(vector& numbers, int target) { - - assert(numbers.size() >= 2); - // assert(isSorted(numbers)); - - for(int i = 0 ; i < numbers.size() ; i ++) - for(int j = i+1 ; j < numbers.size() ; j ++) - if(numbers[i] + numbers[j] == target){ - int res[2] = {i+1, j+1}; - return vector(res, res+2); - } - - throw invalid_argument("the input has no solution"); - } - -private: - bool isSorted(const vector& numbers){ - for(int i = 1 ; i < numbers.size() ; i ++) - if(numbers[i] < numbers[i-1]) - return false; - return true; - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - int nums[] = {2, 7, 11, 15}; - vector vec(nums, nums + sizeof(nums) / sizeof(int)); - int target = 9; - printVec(Solution().twoSum(vec, target)); - - return 0; -} \ No newline at end of file diff --git a/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main2.cpp b/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main2.cpp deleted file mode 100755 index c993b74e..00000000 --- a/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main2.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -#include -#include -#include - -using namespace std; - -// Binary Search -// Time Complexity: O(nlogn) -// Space Complexity: O(1) -class Solution { -public: - vector twoSum(vector& numbers, int target) { - - assert(numbers.size() >= 2); - // assert(isSorted(numbers)); - - for(int i = 0 ; i < numbers.size() - 1 ; i ++){ - int j = binarySearch(numbers, i+1, numbers.size()-1, target - numbers[i]); - if(j != -1){ - int res[2] = {i+1, j+1}; - return vector(res, res+2); - } - } - - throw invalid_argument("the input has no solution"); - } - -private: - int binarySearch(const vector &nums, int l, int r, int target){ - - assert(l >= 0 && l < nums.size()); - assert(r >= 0 && r < nums.size()); - - while(l <= r){ - - int mid = l + (r - l)/2; - if(nums[mid] == target) - return mid; - if(target > nums[mid]) - l = mid + 1; - else - r = mid - 1; - } - - return -1; - } - - bool isSorted(const vector& numbers){ - for(int i = 1 ; i < numbers.size() ; i ++) - if(numbers[i] < numbers[i-1]) - return false; - return true; - } -}; - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - int nums[] = {2, 7, 11, 15}; - vector vec(nums, nums + sizeof(nums) / sizeof(int)); - int target = 9; - printVec(Solution().twoSum(vec, target)); - - return 0; -} \ No newline at end of file diff --git a/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main3.cpp b/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main3.cpp deleted file mode 100755 index ac41c1c5..00000000 --- a/0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main3.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -#include -#include -#include - -using namespace std; - -// Two Pointers -// Time Complexity: O(n) -// Space Complexity: O(1) -class Solution { - -public: - vector twoSum(vector& numbers, int target) { - - assert(numbers.size() >= 2); - // assert(isSorted(numbers)); - - int l = 0, r = numbers.size() - 1; - while(l < r){ - - if(numbers[l] + numbers[r] == target){ - int res[2] = {l+1, r+1}; - return vector(res, res+2); - } - else if(numbers[l] + numbers[r] < target) - l ++; - else // numbers[l] + numbers[r] > target - r --; - } - - throw invalid_argument("the input has no solution"); - } - -private: - bool isSorted(const vector& numbers){ - for(int i = 1 ; i < numbers.size() ; i ++) - if(numbers[i] < numbers[i-1]) - return false; - return true; - } - -}; - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - int nums[] = {2, 7, 11, 15}; - vector vec(nums, nums + sizeof(nums) / sizeof(int)); - int target = 9; - printVec(Solution().twoSum(vec, target)); - - return 0; -} \ No newline at end of file diff --git a/0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution1.java b/0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution1.java deleted file mode 100755 index 314e4f33..00000000 --- a/0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution1.java +++ /dev/null @@ -1,44 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -// Brute Force -// Time Complexity: O(n^2) -// Space Complexity: O(1) -public class Solution1 { - - public int[] twoSum(int[] numbers, int target) { - - if(numbers.length < 2 /*|| !isSorted(numbers)*/) - throw new IllegalArgumentException("Illegal argument numbers"); - - for(int i = 0 ; i < numbers.length ; i ++) - for(int j = i+1 ; j < numbers.length ; j ++) - if(numbers[i] + numbers[j] == target){ - int[] res = {i+1, j+1}; - return res; - } - - throw new IllegalStateException("The input has no solution"); - } - - private boolean isSorted(int[] numbers){ - for(int i = 1 ; i < numbers.length ; i ++) - if(numbers[i] < numbers[i-1]) - return false; - return true; - } - - private static void printArr(int[] nums){ - for(int num: nums) - System.out.print(num + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums = {2, 7, 11, 15}; - int target = 9; - printArr((new Solution1()).twoSum(nums, target)); - } -} diff --git a/0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution2.java b/0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution2.java deleted file mode 100755 index 7abb4935..00000000 --- a/0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution2.java +++ /dev/null @@ -1,66 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -// Binary Search -// Time Complexity: O(nlogn) -// Space Complexity: O(1) -public class Solution2 { - - public int[] twoSum(int[] numbers, int target) { - - if(numbers.length < 2 /*|| !isSorted(numbers)*/) - throw new IllegalArgumentException("Illegal argument numbers"); - - for(int i = 0 ; i < numbers.length - 1 ; i ++){ - int j = binarySearch(numbers, i+1, numbers.length-1, target - numbers[i]); - if(j != -1){ - int[] res = {i+1, j+1}; - return res; - } - } - - throw new IllegalStateException("The input has no solution"); - } - - private int binarySearch(int[] nums, int l, int r, int target){ - - if(l < 0 || l > nums.length) - throw new IllegalArgumentException("l is out of bound"); - - if(r < 0 || r > nums.length) - throw new IllegalArgumentException("r is out of bound"); - - while(l <= r){ - int mid = l + (r - l)/2; - if(nums[mid] == target) - return mid; - if(target > nums[mid]) - l = mid + 1; - else - r = mid - 1; - } - - return -1; - } - - private boolean isSorted(int[] numbers){ - for(int i = 1 ; i < numbers.length ; i ++) - if(numbers[i] < numbers[i-1]) - return false; - return true; - } - - private static void printArr(int[] nums){ - for(int num: nums) - System.out.print(num + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums = {2, 7, 11, 15}; - int target = 9; - printArr((new Solution2()).twoSum(nums, target)); - } -} diff --git a/0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution3.java b/0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution3.java deleted file mode 100755 index 30337b3f..00000000 --- a/0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution3.java +++ /dev/null @@ -1,50 +0,0 @@ -/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/ -/// Author : liuyubobobo -/// Time : 2017-11-13 - -// Two Pointers -// Time Complexity: O(n) -// Space Complexity: O(1) -public class Solution3 { - - public int[] twoSum(int[] numbers, int target) { - - if(numbers.length < 2 /*|| !isSorted(numbers)*/) - throw new IllegalArgumentException("Illegal argument numbers"); - - int l = 0, r = numbers.length - 1; - while(l < r){ - - if(numbers[l] + numbers[r] == target){ - int[] res = {l+1, r+1}; - return res; - } - else if(numbers[l] + numbers[r] < target) - l ++; - else // numbers[l] + numbers[r] > target - r --; - } - - throw new IllegalStateException("The input has no solution"); - } - - private boolean isSorted(int[] numbers){ - for(int i = 1 ; i < numbers.length ; i ++) - if(numbers[i] < numbers[i-1]) - return false; - return true; - } - - private static void printArr(int[] nums){ - for(int num: nums) - System.out.print(num + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums = {2, 7, 11, 15}; - int target = 9; - printArr((new Solution3()).twoSum(nums, target)); - } -} diff --git a/0203-Remove-Linked-List-Elements/cpp-0203/CMakeLists.txt b/0203-Remove-Linked-List-Elements/cpp-0203/CMakeLists.txt deleted file mode 100755 index b7e1db3e..00000000 --- a/0203-Remove-Linked-List-Elements/cpp-0203/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0203) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main2.cpp) -add_executable(cpp_0203 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0203-Remove-Linked-List-Elements/cpp-0203/main.cpp b/0203-Remove-Linked-List-Elements/cpp-0203/main.cpp deleted file mode 100755 index c5dbf03d..00000000 --- a/0203-Remove-Linked-List-Elements/cpp-0203/main.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include - -using namespace std; - -///Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -/// LinkedList Test Helper Functions -ListNode* createLinkedList(int arr[], int n){ - - if(n == 0) - return NULL; - - ListNode* head = new ListNode(arr[0]); - ListNode* curNode = head; - for(int i = 1 ; i < n ; i ++){ - curNode->next = new ListNode(arr[i]); - curNode = curNode->next; - } - - return head; -} - -void printLinkedList(ListNode* head){ - - if(head == NULL){ - cout << "NULL" << endl; - return; - } - - ListNode* curNode = head; - while(curNode != NULL){ - cout << curNode->val; - if(curNode->next != NULL) - cout << " -> "; - curNode = curNode->next; - } - - cout << endl; - - return; -} - -void deleteLinkedList(ListNode* head){ - - ListNode* curNode = head; - while(curNode != NULL){ - ListNode* delNode = curNode; - curNode = curNode->next; - delete delNode; - } - - return; -} - - -/// Linear Scan with dummy head -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - ListNode* removeElements(ListNode* head, int val) { - - ListNode* dummyHead = new ListNode(0); - dummyHead->next = head; - - ListNode* cur = dummyHead; - while(cur->next != NULL){ - if(cur->next->val == val){ - ListNode* delNode = cur->next; - cur->next = delNode->next; - delete delNode; - } - else - cur = cur->next; - } - - ListNode* retNode = dummyHead->next; - delete dummyHead; - - return retNode; - } -}; - - -int main() { - - int arr[] = {1, 2, 6, 3, 4, 5, 6}; - int n = sizeof(arr) / sizeof(int); - - ListNode* head = createLinkedList(arr, n); - printLinkedList(head); - - Solution().removeElements(head, 6); - printLinkedList(head); - - deleteLinkedList(head); - - return 0; -} \ No newline at end of file diff --git a/0203-Remove-Linked-List-Elements/cpp-0203/main2.cpp b/0203-Remove-Linked-List-Elements/cpp-0203/main2.cpp deleted file mode 100755 index 5858fdfa..00000000 --- a/0203-Remove-Linked-List-Elements/cpp-0203/main2.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/ -/// Author : liuyubobobo -/// Time : 2018-09-17 - -#include - -using namespace std; - -///Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - - -/// Recursive -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { -public: - ListNode* removeElements(ListNode* head, int val) { - - if(!head) - return head; - - if(head->val == val){ - ListNode* node = head->next; - delete head; - return removeElements(node, val); - } - - head->next = removeElements(head->next, val); - return head; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/0203-Remove-Linked-List-Elements/java-0203/src/ListNode.java b/0203-Remove-Linked-List-Elements/java-0203/src/ListNode.java deleted file mode 100755 index 8844dbea..00000000 --- a/0203-Remove-Linked-List-Elements/java-0203/src/ListNode.java +++ /dev/null @@ -1,36 +0,0 @@ -public class ListNode { - - public int val; - public ListNode next = null; - - public ListNode(int x) { - val = x; - } - - public ListNode (int[] arr){ - - if(arr == null || arr.length == 0) - throw new IllegalArgumentException("arr can not be empty"); - - this.val = arr[0]; - ListNode curNode = this; - for(int i = 1 ; i < arr.length ; i ++){ - curNode.next = new ListNode(arr[i]); - curNode = curNode.next; - } - } - - @Override - public String toString(){ - - StringBuilder s = new StringBuilder(""); - ListNode curNode = this; - while(curNode != null){ - s.append(Integer.toString(curNode.val)); - s.append(" -> "); - curNode = curNode.next; - } - s.append("NULL"); - return s.toString(); - } -} \ No newline at end of file diff --git a/0203-Remove-Linked-List-Elements/java-0203/src/Solution.java b/0203-Remove-Linked-List-Elements/java-0203/src/Solution.java deleted file mode 100755 index 1a993e8b..00000000 --- a/0203-Remove-Linked-List-Elements/java-0203/src/Solution.java +++ /dev/null @@ -1,38 +0,0 @@ -/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -/// Time Complexity: O(n) -/// Space Complexity: O(1) -public class Solution { - - public ListNode removeElements(ListNode head, int val) { - - ListNode dummyHead = new ListNode(0); - dummyHead.next = head; - - ListNode cur = dummyHead; - while(cur.next != null){ - if(cur.next.val == val ){ - ListNode delNode = cur.next; - cur.next = delNode.next; - } - else - cur = cur.next; - } - - return dummyHead.next; - } - - public static void main(String[] args) { - - int[] arr = {1, 2, 6, 3, 4, 5, 6}; - int val = 6; - - ListNode head = new ListNode(arr); - System.out.println(head); - - (new Solution()).removeElements(head, val); - System.out.println(head); - } -} diff --git a/0203-Remove-Linked-List-Elements/java-0203/src/Solution2.java b/0203-Remove-Linked-List-Elements/java-0203/src/Solution2.java deleted file mode 100755 index 583ae0c6..00000000 --- a/0203-Remove-Linked-List-Elements/java-0203/src/Solution2.java +++ /dev/null @@ -1,24 +0,0 @@ -public class Solution2 { - - public ListNode removeElements(ListNode head, int val) { - - if(head == null) - return head; - - ListNode node = removeElements(head.next, val); - head.next = node; - return head.val == val ? node : head; - } - - public static void main(String[] args) { - - int[] arr = {1, 2, 6, 3, 4, 5, 6}; - int val = 6; - - ListNode head = new ListNode(arr); - System.out.println(head); - - (new Solution()).removeElements(head, val); - System.out.println(head); - } -} diff --git a/0219-Contains-Duplicate-II/cpp-0219/CMakeLists.txt b/0219-Contains-Duplicate-II/cpp-0219/CMakeLists.txt deleted file mode 100755 index ba486e44..00000000 --- a/0219-Contains-Duplicate-II/cpp-0219/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0219) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0219 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0219-Contains-Duplicate-II/cpp-0219/main.cpp b/0219-Contains-Duplicate-II/cpp-0219/main.cpp deleted file mode 100755 index 52338145..00000000 --- a/0219-Contains-Duplicate-II/cpp-0219/main.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/// Source : https://leetcode.com/problems/contains-duplicate-ii/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include -#include - -using namespace std; - -/// Using Hash Set -/// Time Complexity: O(n) -/// Space Complexity: O(k) -class Solution { -public: - bool containsNearbyDuplicate(vector& nums, int k) { - - if(nums.size() <= 1) - return false; - - if(k <= 0) - return false; - - unordered_set record; - for(int i = 0 ; i < nums.size() ; i ++){ - - if(record.find(nums[i]) != record.end()) - return true; - - record.insert(nums[i]); - - // 保持record中最多有k个元素 - // 因为在下一次循环中会添加一个新元素,使得总共考虑k+1个元素 - if(record.size() == k + 1) - record.erase(nums[i - k]); - } - - return false; - } -}; - - -void printBool(bool b){ - cout << (b ? "True" : "False") << endl; -} - -int main() { - - int nums[] = {1, 2, 1}; - vector vec(nums, nums + sizeof(nums)/sizeof(int)); - int k = 1; - - printBool(Solution().containsNearbyDuplicate(vec, k)); - - return 0; -} \ No newline at end of file diff --git a/0219-Contains-Duplicate-II/java-0219/src/Solution.java b/0219-Contains-Duplicate-II/java-0219/src/Solution.java deleted file mode 100755 index cc1df7bf..00000000 --- a/0219-Contains-Duplicate-II/java-0219/src/Solution.java +++ /dev/null @@ -1,43 +0,0 @@ -/// Source : https://leetcode.com/problems/contains-duplicate-ii/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -import java.util.HashSet; - -/// Using Hash Set -/// Time Complexity: O(n) -/// Space Complexity: O(k) -public class Solution { - - public boolean containsNearbyDuplicate(int[] nums, int k) { - - if(nums == null || nums.length <= 1) - return false; - - if(k <= 0) - return false; - - HashSet record = new HashSet(); - for(int i = 0 ; i < nums.length; i ++){ - if(record.contains(nums[i])) - return true; - - record.add(nums[i]); - if(record.size() == k + 1) - record.remove(nums[i-k]); - } - - return false; - } - - private static void printBool(boolean b){ - System.out.println(b ? "True" : "False"); - } - - public static void main(String[] args) { - - int[] nums = {1, 2, 1}; - int k = 1; - printBool((new Solution()).containsNearbyDuplicate(nums, k)); - } -} diff --git a/0237-Delete-Node-in-a-Linked-List/cpp-0237/CMakeLists.txt b/0237-Delete-Node-in-a-Linked-List/cpp-0237/CMakeLists.txt deleted file mode 100755 index 3c8c304e..00000000 --- a/0237-Delete-Node-in-a-Linked-List/cpp-0237/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0237) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0237 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0237-Delete-Node-in-a-Linked-List/cpp-0237/main.cpp b/0237-Delete-Node-in-a-Linked-List/cpp-0237/main.cpp deleted file mode 100755 index 1fbc3c8e..00000000 --- a/0237-Delete-Node-in-a-Linked-List/cpp-0237/main.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/// Source : https://leetcode.com/problems/delete-node-in-a-linked-list/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include - -using namespace std; - - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - - -/// LinkedList Test Helper Functions -ListNode* createLinkedList(int arr[], int n){ - - if(n == 0) - return NULL; - - ListNode* head = new ListNode(arr[0]); - ListNode* curNode = head; - for(int i = 1 ; i < n ; i ++){ - curNode->next = new ListNode(arr[i]); - curNode = curNode->next; - } - - return head; -} - -void printLinkedList(ListNode* head){ - - ListNode* curNode = head; - while(curNode != NULL){ - cout << curNode->val << " -> "; - curNode = curNode->next; - } - - cout << "NULL" << endl; - - return; -} - -void deleteLinkedList(ListNode* head){ - - ListNode* curNode = head; - while(curNode != NULL){ - ListNode* delNode = curNode; - curNode = curNode->next; - delete delNode; - } - - return; -} - -ListNode* findNode(ListNode* head, int x){ - - ListNode* curNode = head; - while(curNode != NULL){ - if(curNode->val == x) - return curNode; - curNode = curNode->next; - } - return NULL; -} - -// 时间复杂度: O(1) -// 空间复杂度: O(1) -class Solution { -public: - void deleteNode(ListNode* node) { - - assert(node != NULL && node->next != NULL); - - node->val = node->next->val; - ListNode* delNode = node->next; - node->next = delNode->next; - - delete delNode; - - return; - } -}; - -int main() { - - int arr[] = {1, 2, 3, 4}; - int n = sizeof(arr)/sizeof(int); - - ListNode* head = createLinkedList(arr, n); - printLinkedList(head); - - ListNode* node2 = findNode(head, 2); - Solution().deleteNode(node2); - printLinkedList(head); - - deleteLinkedList(head); - - return 0; -} \ No newline at end of file diff --git a/0237-Delete-Node-in-a-Linked-List/java-0237/src/ListNode.java b/0237-Delete-Node-in-a-Linked-List/java-0237/src/ListNode.java deleted file mode 100755 index 9852897e..00000000 --- a/0237-Delete-Node-in-a-Linked-List/java-0237/src/ListNode.java +++ /dev/null @@ -1,47 +0,0 @@ -public class ListNode { - - public int val; - public ListNode next = null; - - public ListNode(int x) { - val = x; - } - - public ListNode (int[] arr){ - - if(arr == null || arr.length == 0) - throw new IllegalArgumentException("arr can not be empty"); - - this.val = arr[0]; - ListNode curNode = this; - for(int i = 1 ; i < arr.length ; i ++){ - curNode.next = new ListNode(arr[i]); - curNode = curNode.next; - } - } - - ListNode findNode(int x){ - - ListNode curNode = this; - while(curNode != null){ - if(curNode.val == x) - return curNode; - curNode = curNode.next; - } - return null; - } - - @Override - public String toString(){ - - StringBuilder s = new StringBuilder(""); - ListNode curNode = this; - while(curNode != null){ - s.append(Integer.toString(curNode.val)); - s.append(" -> "); - curNode = curNode.next; - } - s.append("NULL"); - return s.toString(); - } -} \ No newline at end of file diff --git a/0237-Delete-Node-in-a-Linked-List/java-0237/src/Solution.java b/0237-Delete-Node-in-a-Linked-List/java-0237/src/Solution.java deleted file mode 100755 index f3175db8..00000000 --- a/0237-Delete-Node-in-a-Linked-List/java-0237/src/Solution.java +++ /dev/null @@ -1,29 +0,0 @@ -/// Source : https://leetcode.com/problems/delete-node-in-a-linked-list/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 -/// -/// 时间复杂度: O(1) -/// 空间复杂度: O(1) -public class Solution { - - public void deleteNode(ListNode node) { - - if(node == null || node.next == null) - throw new IllegalArgumentException("node should be valid and can not be the tail node."); - - node.val = node.next.val; - node.next = node.next.next; - } - - public static void main(String[] args) { - - int[] arr = {1, 2, 3, 4}; - - ListNode head = new ListNode(arr); - System.out.println(head); - - ListNode node2 = head.findNode(2); - (new Solution()).deleteNode(node2); - System.out.println(head); - } -} diff --git a/0279-Perfect-Squares/cpp-0279/CMakeLists.txt b/0279-Perfect-Squares/cpp-0279/CMakeLists.txt deleted file mode 100755 index b4e9216f..00000000 --- a/0279-Perfect-Squares/cpp-0279/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0279) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main3.cpp) -add_executable(cpp_0279 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0279-Perfect-Squares/cpp-0279/main.cpp b/0279-Perfect-Squares/cpp-0279/main.cpp deleted file mode 100755 index 69ba9614..00000000 --- a/0279-Perfect-Squares/cpp-0279/main.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/// Source : https://leetcode.com/problems/perfect-squares/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include -#include -#include - -using namespace std; - -/// BFS -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { -public: - int numSquares(int n) { - - if(n == 0) - return 0; - - queue> q; - q.push(make_pair(n, 0)); - - vector visited(n + 1, false); - visited[n] = true; - - while(!q.empty()){ - int num = q.front().first; - int step = q.front().second; - q.pop(); - - for(int i = 1; num - i * i >= 0; i ++){ - int a = num - i * i; - if(!visited[a]){ - if(a == 0) return step + 1; - q.push(make_pair(a, step + 1)); - visited[a] = true; - } - } - } - - throw invalid_argument("No Solution."); - } -}; - -int main() { - - cout << Solution().numSquares(12) << endl; - cout << Solution().numSquares(13) << endl; - - return 0; -} \ No newline at end of file diff --git a/0279-Perfect-Squares/cpp-0279/main2.cpp b/0279-Perfect-Squares/cpp-0279/main2.cpp deleted file mode 100755 index b22d1bc2..00000000 --- a/0279-Perfect-Squares/cpp-0279/main2.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/// Source : https://leetcode.com/problems/perfect-squares/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include - -using namespace std; - -/// Memory Search -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { - -public: - int numSquares(int n) { - - vector mem(n + 1, -1); - - return numSquares(n, mem); - } - -private: - int numSquares(int n, vector& mem){ - - if(n == 0) - return 0; - - if(mem[n] != -1) - return mem[n]; - - int res = INT_MAX; - for(int i = 1; n - i * i >= 0; i ++ ) - res = min(res, 1 + numSquares(n - i * i, mem)); - return mem[n] = res; - } -}; - -int main() { - - cout << Solution().numSquares(12) << endl; - cout << Solution().numSquares(13) << endl; - - return 0; -} \ No newline at end of file diff --git a/0279-Perfect-Squares/cpp-0279/main3.cpp b/0279-Perfect-Squares/cpp-0279/main3.cpp deleted file mode 100755 index fe1da60b..00000000 --- a/0279-Perfect-Squares/cpp-0279/main3.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/// Source : https://leetcode.com/problems/perfect-squares/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -#include -#include - -using namespace std; - -/// Dynamic Programming -/// Time Complexity: O(n) -/// Space Complexity: O(n) -class Solution { - -public: - int numSquares(int n) { - - vector mem(n + 1, INT_MAX); - mem[0] = 0; - for(int i = 1; i <= n ; i ++) - for(int j = 1 ; i - j * j >= 0 ; j ++) - mem[i] = min(mem[i], 1 + mem[i - j * j]); - - return mem[n]; - } -}; - -int main() { - - cout << Solution().numSquares(12) << endl; - cout << Solution().numSquares(13) << endl; - - return 0; -} \ No newline at end of file diff --git a/0279-Perfect-Squares/java-0279/src/Solution1.java b/0279-Perfect-Squares/java-0279/src/Solution1.java deleted file mode 100755 index 3449f328..00000000 --- a/0279-Perfect-Squares/java-0279/src/Solution1.java +++ /dev/null @@ -1,50 +0,0 @@ -/// Source : https://leetcode.com/problems/perfect-squares/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.LinkedList; -import javafx.util.Pair; - -/// BFS -/// Time Complexity: O(n) -/// Space Complexity: O(n) -public class Solution1 { - - public int numSquares(int n) { - - if(n == 0) - return 0; - - LinkedList> queue = new LinkedList>(); - queue.addLast(new Pair(n, 0)); - - boolean[] visited = new boolean[n+1]; - visited[n] = true; - - while(!queue.isEmpty()){ - Pair front = queue.removeFirst(); - int num = front.getKey(); - int step = front.getValue(); - - if(num == 0) - return step; - - for(int i = 1 ; num - i*i >= 0 ; i ++){ - int a = num - i*i; - if(!visited[a]){ - if(a == 0) return step + 1; - queue.addLast(new Pair(num - i * i, step + 1)); - visited[num - i * i] = true; - } - } - } - - throw new IllegalStateException("No Solution."); - } - - public static void main(String[] args) { - - System.out.println((new Solution1()).numSquares(12)); - System.out.println((new Solution1()).numSquares(13)); - } -} diff --git a/0279-Perfect-Squares/java-0279/src/Solution2.java b/0279-Perfect-Squares/java-0279/src/Solution2.java deleted file mode 100755 index fa4dcf12..00000000 --- a/0279-Perfect-Squares/java-0279/src/Solution2.java +++ /dev/null @@ -1,39 +0,0 @@ -/// Source : https://leetcode.com/problems/perfect-squares/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.Arrays; - -/// Memory Search -/// Time Complexity: O(n) -/// Space Complexity: O(n) -public class Solution2 { - - public int numSquares(int n) { - - int[] mem = new int[n+1]; - Arrays.fill(mem, -1); - - return numSquares(n, mem); - } - - private int numSquares(int n, int[] mem){ - - if(n == 0) - return 0; - - if(mem[n] != -1) - return mem[n]; - - int res = Integer.MAX_VALUE; - for(int i = 1; n - i * i >= 0; i ++ ) - res = Math.min(res, 1 + numSquares(n - i * i, mem)); - return mem[n] = res; - } - - public static void main(String[] args) { - - System.out.println((new Solution2()).numSquares(12)); - System.out.println((new Solution2()).numSquares(13)); - } -} diff --git a/0279-Perfect-Squares/java-0279/src/Solution3.java b/0279-Perfect-Squares/java-0279/src/Solution3.java deleted file mode 100755 index 2cc9c7ad..00000000 --- a/0279-Perfect-Squares/java-0279/src/Solution3.java +++ /dev/null @@ -1,29 +0,0 @@ -/// Source : https://leetcode.com/problems/perfect-squares/description/ -/// Author : liuyubobobo -/// Time : 2017-11-17 - -import java.util.Arrays; - -/// Dynamic Programming -/// Time Complexity: O(n) -/// Space Complexity: O(n) -public class Solution3 { - - public int numSquares(int n) { - - int[] mem = new int[n+1]; - Arrays.fill(mem, Integer.MAX_VALUE); - mem[0] = 0; - for(int i = 1; i <= n ; i ++) - for(int j = 1 ; i - j * j >= 0 ; j ++) - mem[i] = Math.min(mem[i], 1 + mem[i - j * j]); - - return mem[n]; - } - - public static void main(String[] args) { - - System.out.println((new Solution3()).numSquares(12)); - System.out.println((new Solution3()).numSquares(13)); - } -} diff --git a/0283-Move-Zeroes/cpp-0283/CMakeLists.txt b/0283-Move-Zeroes/cpp-0283/CMakeLists.txt deleted file mode 100755 index d3a5e58e..00000000 --- a/0283-Move-Zeroes/cpp-0283/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0283) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0283 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0283-Move-Zeroes/cpp-0283/main.cpp b/0283-Move-Zeroes/cpp-0283/main.cpp deleted file mode 100755 index 938da23e..00000000 --- a/0283-Move-Zeroes/cpp-0283/main.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/// Source : https://leetcode.com/problems/move-zeroes/description/ -/// Author : liuyubobobo -/// Time : 2017-02-09 - -#include -#include - -using namespace std; - -// Time Complexity: O(n) -// Space Complexity: O(n) -class Solution { -public: - void moveZeroes(vector& nums) { - - vector nonZeroElements; - - // put all the non zero elements into a new vector - for(int i = 0 ; i < nums.size() ; i ++) - if(nums[i]) - nonZeroElements.push_back(nums[i]); - - // make nums[0...nonZeroElements.size()) all non zero elements - for(int i = 0 ; i < nonZeroElements.size() ; i ++) - nums[i] = nonZeroElements[i]; - - // make nums[nonZeroElements.size()...nums.size()) all zero elements - for(int i = nonZeroElements.size() ; i < nums.size() ; i ++) - nums[i] = 0; - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - int arr[] = {0, 1, 0, 3, 12}; - vector vec(arr, arr + sizeof(arr) / sizeof(int)); - Solution().moveZeroes(vec); - printVec(vec); - - return 0; -} \ No newline at end of file diff --git a/0283-Move-Zeroes/cpp-0283/main2.cpp b/0283-Move-Zeroes/cpp-0283/main2.cpp deleted file mode 100755 index 6389b44c..00000000 --- a/0283-Move-Zeroes/cpp-0283/main2.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/// Source : https://leetcode.com/problems/move-zeroes/description/ -/// Author : liuyubobobo -/// Time : 2017-02-09 - -#include -#include - -using namespace std; - -// Time Complexity: O(n) -// Space Complexity: O(1) -class Solution { -public: - void moveZeroes(vector& nums) { - - int k = 0; // keep nums[0...k) are all zero elements - for(int i = 0 ; i < nums.size() ; i ++ ) - if(nums[i]) - nums[k++] = nums[i]; - - // make the nums[k...end) zeros - for(int i = k ; i < nums.size() ; i ++) - nums[i] = 0; - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - int arr[] = {0, 1, 0, 3, 12}; - vector vec(arr, arr + sizeof(arr) / sizeof(int)); - Solution().moveZeroes(vec); - printVec(vec); - - return 0; -} \ No newline at end of file diff --git a/0283-Move-Zeroes/cpp-0283/main3.cpp b/0283-Move-Zeroes/cpp-0283/main3.cpp deleted file mode 100755 index e5d18731..00000000 --- a/0283-Move-Zeroes/cpp-0283/main3.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/// Source : https://leetcode.com/problems/move-zeroes/description/ -/// Author : liuyubobobo -/// Time : 2017-02-09 - -#include -#include - -using namespace std; - -// Time Complexity: O(n) -// Space Complexity: O(1) -class Solution { -public: - void moveZeroes(vector& nums) { - - int k = 0; // keep nums[0...k) are all zero elements - for(int i = 0 ; i < nums.size() ; i ++) - if(nums[i]) - swap(nums[k++] , nums[i]); - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - int arr[] = {0, 1, 0, 3, 12}; - vector vec(arr, arr + sizeof(arr) / sizeof(int)); - Solution().moveZeroes(vec); - printVec(vec); - - return 0; -} \ No newline at end of file diff --git a/0283-Move-Zeroes/cpp-0283/main4.cpp b/0283-Move-Zeroes/cpp-0283/main4.cpp deleted file mode 100755 index 56821673..00000000 --- a/0283-Move-Zeroes/cpp-0283/main4.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/// Source : https://leetcode.com/problems/move-zeroes/description/ -/// Author : liuyubobobo -/// Time : 2017-02-09 - -#include -#include - -using namespace std; - -// Time Complexity: O(n) -// Space Complexity: O(1) -class Solution { -public: - void moveZeroes(vector& nums) { - - int k = 0; // keep nums[0...k) are all zero elements - for(int i = 0 ; i < nums.size() ; i ++ ) - if(nums[i]) - // avoid self swapping - if(k != i) - swap(nums[k++], nums[i]); - else - k ++; - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - int arr[] = {0, 1, 0, 3, 12}; - vector vec(arr, arr + sizeof(arr) / sizeof(int)); - Solution().moveZeroes(vec); - printVec(vec); - - return 0; -} \ No newline at end of file diff --git a/0283-Move-Zeroes/java-0283/src/Solution1.java b/0283-Move-Zeroes/java-0283/src/Solution1.java deleted file mode 100755 index 45f8223f..00000000 --- a/0283-Move-Zeroes/java-0283/src/Solution1.java +++ /dev/null @@ -1,41 +0,0 @@ -/// Source : https://leetcode.com/problems/move-zeroes/description/ -/// Author : liuyubobobo -/// Time : 2017-02-09 - -import java.util.*; - -// Time Complexity: O(n) -// Space Complexity: O(n) -class Solution1 { - - public void moveZeroes(int[] nums) { - - ArrayList nonZeroElements = new ArrayList(); - - // put all the non zero elements into a new vector - for (int i = 0; i < nums.length; i++) - if (nums[i] != 0) - nonZeroElements.add(nums[i]); - - // make nums[0...nonZeroElements.size()) all non zero elements - for (int i = 0; i < nonZeroElements.size(); i++) - nums[i] = nonZeroElements.get(i); - - // make nums[nonZeroElements.size()...nums.size()) all zero elements - for (int i = nonZeroElements.size(); i < nums.length; i++) - nums[i] = 0; - } - - private static void printArr(int[] arr){ - for(int i = 0 ; i < arr.length ; i ++) - System.out.print(arr[i] + " "); - System.out.println(); - } - - public static void main(String args[]){ - - int[] arr = {0, 1, 0, 3, 12}; - (new Solution1()).moveZeroes(arr); - printArr(arr); - } -} \ No newline at end of file diff --git a/0283-Move-Zeroes/java-0283/src/Solution2.java b/0283-Move-Zeroes/java-0283/src/Solution2.java deleted file mode 100755 index c61c0748..00000000 --- a/0283-Move-Zeroes/java-0283/src/Solution2.java +++ /dev/null @@ -1,35 +0,0 @@ -/// Source : https://leetcode.com/problems/move-zeroes/description/ -/// Author : liuyubobobo -/// Time : 2017-02-09 - -import java.util.*; - -// Time Complexity: O(n) -// Space Complexity: O(n) -class Solution2 { - - public void moveZeroes(int[] nums) { - - int k = 0; // keep nums[0...k) are all zero elements - for(int i = 0 ; i < nums.length ; i ++) - if(nums[i] != 0) - nums[k++] = nums[i]; - - // make the nums[k...end) zeros - for(int i = k ; i < nums.length ; i ++) - nums[i] = 0; - } - - private static void printArr(int[] arr){ - for(int i = 0 ; i < arr.length ; i ++) - System.out.print(arr[i] + " "); - System.out.println(); - } - - public static void main(String args[]){ - - int[] arr = {0, 1, 0, 3, 12}; - (new Solution2()).moveZeroes(arr); - printArr(arr); - } -} \ No newline at end of file diff --git a/0283-Move-Zeroes/java-0283/src/Solution3.java b/0283-Move-Zeroes/java-0283/src/Solution3.java deleted file mode 100755 index 7667769b..00000000 --- a/0283-Move-Zeroes/java-0283/src/Solution3.java +++ /dev/null @@ -1,37 +0,0 @@ -/// Source : https://leetcode.com/problems/move-zeroes/description/ -/// Author : liuyubobobo -/// Time : 2017-02-09 - -import java.util.*; - -// Time Complexity: O(n) -// Space Complexity: O(n) -class Solution3 { - - public void moveZeroes(int[] nums) { - - int k = 0; // keep nums[0...k) are all zero elements - for(int i = 0 ; i < nums.length ; i ++) - if(nums[i] != 0) - swap(nums, k++, i); - } - - private void swap(int[] nums, int i, int j){ - int t = nums[i]; - nums[i] = nums[j]; - nums[j] = t; - } - - private static void printArr(int[] arr){ - for(int i = 0 ; i < arr.length ; i ++) - System.out.print(arr[i] + " "); - System.out.println(); - } - - public static void main(String args[]){ - - int[] arr = {0, 1, 0, 3, 12}; - (new Solution3()).moveZeroes(arr); - printArr(arr); - } -} \ No newline at end of file diff --git a/0283-Move-Zeroes/java-0283/src/Solution4.java b/0283-Move-Zeroes/java-0283/src/Solution4.java deleted file mode 100755 index ff0d280e..00000000 --- a/0283-Move-Zeroes/java-0283/src/Solution4.java +++ /dev/null @@ -1,40 +0,0 @@ -/// Source : https://leetcode.com/problems/move-zeroes/description/ -/// Author : liuyubobobo -/// Time : 2017-02-09 - -import java.util.*; - -// Time Complexity: O(n) -// Space Complexity: O(n) -class Solution4 { - - public void moveZeroes(int[] nums) { - - int k = 0; // keep nums[0...k) are all zero elements - for(int i = 0 ; i < nums.length ; i ++) - if(nums[i] != 0) - if(k != i) - swap(nums, k ++, i); - else - k ++; - } - - private void swap(int[] nums, int i, int j){ - int t = nums[i]; - nums[i] = nums[j]; - nums[j] = t; - } - - private static void printArr(int[] arr){ - for(int i = 0 ; i < arr.length ; i ++) - System.out.print(arr[i] + " "); - System.out.println(); - } - - public static void main(String args[]){ - - int[] arr = {0, 1, 0, 3, 12}; - (new Solution4()).moveZeroes(arr); - printArr(arr); - } -} \ No newline at end of file diff --git a/0328-Odd-Even-Linked-List/cpp-0328/CMakeLists.txt b/0328-Odd-Even-Linked-List/cpp-0328/CMakeLists.txt deleted file mode 100755 index cd5099a2..00000000 --- a/0328-Odd-Even-Linked-List/cpp-0328/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0328) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main2.cpp) -add_executable(cpp_0328 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0328-Odd-Even-Linked-List/cpp-0328/main.cpp b/0328-Odd-Even-Linked-List/cpp-0328/main.cpp deleted file mode 100755 index f1261ff7..00000000 --- a/0328-Odd-Even-Linked-List/cpp-0328/main.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/// Source : https://leetcode.com/problems/odd-even-linked-list/description/ -/// Author : liuyubobobo -/// Time : 2018-10-01 - -#include - -using namespace std; - - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - - -/// Split the Linked List into two and then merge -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - ListNode* oddEvenList(ListNode* head) { - - if(head == NULL || head->next == NULL || head->next->next == NULL) - return head; - - ListNode* dummyHead1 = new ListNode(-1); - ListNode* dummyHead2 = new ListNode(-1); - ListNode* p1 = dummyHead1; - ListNode* p2 = dummyHead2; - ListNode* p = head; - for(int i = 0; p; i ++) - if(i % 2 == 0){ - p1->next = p; - p = p->next; - p1 = p1->next; - p1->next = NULL; - } - else{ - p2->next = p; - p = p->next; - p2 = p2->next; - p2->next = NULL; - } - - p1->next = dummyHead2->next; - ListNode* ret = dummyHead1->next; - - delete dummyHead1; - delete dummyHead2; - return ret; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/0328-Odd-Even-Linked-List/cpp-0328/main2.cpp b/0328-Odd-Even-Linked-List/cpp-0328/main2.cpp deleted file mode 100755 index 36dae46f..00000000 --- a/0328-Odd-Even-Linked-List/cpp-0328/main2.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/// Source : https://leetcode.com/problems/odd-even-linked-list/description/ -/// Author : liuyubobobo -/// Time : 2018-10-01 - -#include - -using namespace std; - - -/// Definition for singly-linked list. -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - - -/// Split the Linked List into two and then merge -/// Keep one in original Linked List -/// -/// Time Complexity: O(n) -/// Space Complexity: O(1) -class Solution { -public: - ListNode* oddEvenList(ListNode* head) { - - if(head == NULL || head->next == NULL || head->next->next == NULL) - return head; - - ListNode* dummyHead2 = new ListNode(-1); - ListNode* p2 = dummyHead2; - ListNode* p = head; - while(p->next){ - p2->next = p->next; - if(p->next->next == NULL){ - p->next = NULL; - break; - } - p->next = p->next->next; - p = p->next; - p2 = p2->next; - p2->next = NULL; - } - - p->next = dummyHead2->next; - delete dummyHead2; - return head; - } -}; - - -int main() { - - return 0; -} \ No newline at end of file diff --git a/0344-Reverse-String/cpp-0344/CMakeLists.txt b/0344-Reverse-String/cpp-0344/CMakeLists.txt deleted file mode 100755 index fbfb09af..00000000 --- a/0344-Reverse-String/cpp-0344/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0344) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0344 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0344-Reverse-String/cpp-0344/main.cpp b/0344-Reverse-String/cpp-0344/main.cpp deleted file mode 100755 index 388d7ce0..00000000 --- a/0344-Reverse-String/cpp-0344/main.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/// Source : https://leetcode.com/problems/reverse-string/description/ -/// Author : liuyubobobo -/// Time : 2018-06-04 - -#include - -using namespace std; - -// 344. Reverse String -// https://leetcode.com/problems/reverse-string/description/ -// Two Pointers -// 时间复杂度: O(n) -// 空间复杂度: O(1) -class Solution { -public: - string reverseString(string s) { - - int i = 0, j = s.size() - 1; - while(i < j){ - swap(s[i], s[j]); - i ++; - j --; - } - - return s; - } -}; - - -int main() { - - cout << Solution().reverseString("hello") << endl; - - return 0; -} diff --git a/0349-Intersection-of-Two-Arrays/cpp-0349/CMakeLists.txt b/0349-Intersection-of-Two-Arrays/cpp-0349/CMakeLists.txt deleted file mode 100755 index 36c8c789..00000000 --- a/0349-Intersection-of-Two-Arrays/cpp-0349/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0349) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0349 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0349-Intersection-of-Two-Arrays/cpp-0349/main.cpp b/0349-Intersection-of-Two-Arrays/cpp-0349/main.cpp deleted file mode 100755 index 0764a531..00000000 --- a/0349-Intersection-of-Two-Arrays/cpp-0349/main.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/// Source : https://leetcode.com/problems/intersection-of-two-arrays/description/ -/// Author : liuyubobobo -/// Time : 2017-07-12 - -#include -#include -#include - -using namespace std; - -/// Hash Set -/// Time complexity: O(len(nums1) + len(nums2)) -/// Space Complexity: O(len(nums1)) -class Solution { -public: - vector intersection(vector& nums1, vector& nums2) { - - unordered_set record(nums1.begin(), nums1.end()); - - unordered_set resultSet; - for( int i = 0 ; i < nums2.size() ; i ++ ) - if( record.find(nums2[i]) != record.end() ) - resultSet.insert( nums2[i] ); - - return vector(resultSet.begin(), resultSet.end()); - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - int nums1[] = {1, 2, 2, 1}; - vector vec1(nums1, nums1 + sizeof(nums1)/sizeof(int)); - - int nums2[] = {2, 2}; - vector vec2(nums2, nums2 + sizeof(nums2)/sizeof(int)); - - printVec(Solution().intersection(vec1, vec2)); - - return 0; -} \ No newline at end of file diff --git a/0349-Intersection-of-Two-Arrays/java-0349/src/Solution.java b/0349-Intersection-of-Two-Arrays/java-0349/src/Solution.java deleted file mode 100755 index 7e4b99d1..00000000 --- a/0349-Intersection-of-Two-Arrays/java-0349/src/Solution.java +++ /dev/null @@ -1,44 +0,0 @@ -/// Source : https://leetcode.com/problems/intersection-of-two-arrays/description/ -/// Author : liuyubobobo -/// Time : 2017-07-12 - -import java.util.HashSet; - -/// Hash Set -/// Time complexity: O(len(nums1) + len(nums2)) -/// Space Complexity: O(len(nums1)) -public class Solution { - - public int[] intersection(int[] nums1, int[] nums2) { - - HashSet record = new HashSet(); - for(int num: nums1) - record.add(num); - - HashSet resultSet = new HashSet(); - for(int num: nums2) - if(record.contains(num)) - resultSet.add(num); - - int[] res = new int[resultSet.size()]; - int index = 0; - for(Integer num: resultSet) - res[index++] = num; - - return res; - } - - private static void printArr(int[] arr){ - for(int e: arr) - System.out.print(e + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums1 = {1, 2, 2, 1}; - int[] nums2 = {2, 2}; - int[] res = (new Solution()).intersection(nums1, nums2); - printArr(res); - } -} \ No newline at end of file diff --git a/0350-Intersection-of-Two-Arrays-II/cpp-0350/CMakeLists.txt b/0350-Intersection-of-Two-Arrays-II/cpp-0350/CMakeLists.txt deleted file mode 100755 index 4a9a5821..00000000 --- a/0350-Intersection-of-Two-Arrays-II/cpp-0350/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0350) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0350 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0350-Intersection-of-Two-Arrays-II/cpp-0350/main.cpp b/0350-Intersection-of-Two-Arrays-II/cpp-0350/main.cpp deleted file mode 100755 index ed790390..00000000 --- a/0350-Intersection-of-Two-Arrays-II/cpp-0350/main.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ -/// Author : liuyubobobo -/// Time : 2017-11-14 - -#include -#include -#include -using namespace std; - -/// Using Hash Map -/// Time Complexity: O(len(nums1) + len(nums2)) -/// Space Complexity: O(len(nums1)) -class Solution { -public: - vector intersect(vector& nums1, vector& nums2) { - - unordered_map record; - for(int i = 0 ; i < nums1.size() ; i ++) - record[nums1[i]] += 1; - - vector resultVector; - for(int i = 0 ; i < nums2.size() ; i ++) - if(record[nums2[i]] > 0){ - resultVector.push_back(nums2[i]); - record[nums2[i]] --; - } - - return resultVector; - } -}; - - -void printVec(const vector& vec){ - for(int e: vec) - cout << e << " "; - cout << endl; -} - -int main() { - - int nums1[] = {1, 2, 2, 1}; - vector vec1(nums1, nums1 + sizeof(nums1)/sizeof(int)); - - int nums2[] = {2, 2}; - vector vec2(nums2, nums2 + sizeof(nums2)/sizeof(int)); - - printVec(Solution().intersect(vec1, vec2)); - - return 0; -} \ No newline at end of file diff --git a/0350-Intersection-of-Two-Arrays-II/cpp-0350/main2.cpp b/0350-Intersection-of-Two-Arrays-II/cpp-0350/main2.cpp deleted file mode 100755 index 2d4a9b5e..00000000 --- a/0350-Intersection-of-Two-Arrays-II/cpp-0350/main2.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ -/// Author : liuyubobobo -/// Time : 2017-11-14 - -#include -#include -#include - -using namespace std; - -/// Using Hash Map -/// Time Complexity: O(len(nums1) + len(nums2)*log(len(nums1))) -/// Space Complexity: O(len(nums1)) -class Solution { -public: - vector intersect(vector& nums1, vector& nums2) { - - multiset record; - for(int num: nums1) - record.insert(num); - - multiset result; - for(int num: nums2){ - multiset::iterator iter = record.find(num); - if( iter != record.end()){ - result.insert(num); - record.erase(iter); - } - } - - return vector(result.begin(), result.end()); - } -}; - -int main() { - - int nums1[] = {1, 2, 2, 1}; - vector vec1(nums1, nums1 + sizeof(nums1)/sizeof(int)); - - int nums2[] = {2, 2}; - vector vec2(nums2, nums2 + sizeof(nums2)/sizeof(int)); - - printVec(Solution().intersect(vec1, vec2)); - - return 0; -} \ No newline at end of file diff --git a/0350-Intersection-of-Two-Arrays-II/java-0350/src/Solution.java b/0350-Intersection-of-Two-Arrays-II/java-0350/src/Solution.java deleted file mode 100755 index b3bfa94f..00000000 --- a/0350-Intersection-of-Two-Arrays-II/java-0350/src/Solution.java +++ /dev/null @@ -1,50 +0,0 @@ -/// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ -/// Author : liuyubobobo -/// Time : 2017-11-14 - -import java.util.HashMap; -import java.util.ArrayList; - -/// Using Hash Map -/// Time Complexity: O(len(nums1) + len(nums2)*log(len(nums1))) -/// Space Complexity: O(len(nums1)) -public class Solution { - - public int[] intersect(int[] nums1, int[] nums2) { - - HashMap record = new HashMap(); - for(int num: nums1) - if(!record.containsKey(num)) - record.put(num, 1); - else - record.put(num, record.get(num) + 1); - - ArrayList result = new ArrayList(); - for(int num: nums2) - if(record.containsKey(num) && record.get(num) > 0){ - result.add(num); - record.put(num, record.get(num) - 1); - } - - int[] ret = new int[result.size()]; - int index = 0; - for(Integer num: result) - ret[index++] = num; - - return ret; - } - - private static void printArr(int[] arr){ - for(int e: arr) - System.out.print(e + " "); - System.out.println(); - } - - public static void main(String[] args) { - - int[] nums1 = {1, 2, 2, 1}; - int[] nums2 = {2, 2}; - int[] res = (new Solution()).intersect(nums1, nums2); - printArr(res); - } -} diff --git a/0447-Number-of-Boomerangs/cpp-0447/CMakeLists.txt b/0447-Number-of-Boomerangs/cpp-0447/CMakeLists.txt deleted file mode 100755 index d806bfb1..00000000 --- a/0447-Number-of-Boomerangs/cpp-0447/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0447) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0447 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0447-Number-of-Boomerangs/cpp-0447/main.cpp b/0447-Number-of-Boomerangs/cpp-0447/main.cpp deleted file mode 100755 index 9bba5f7a..00000000 --- a/0447-Number-of-Boomerangs/cpp-0447/main.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/// Source : https://leetcode.com/problems/number-of-boomerangs/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include -#include -#include -#include - -using namespace std; - -/// Using Hash Map -/// Time Complexity: O(n^2) -/// Space Complexity: O(n) -class Solution { -public: - int numberOfBoomerangs(vector>& points) { - - int res = 0; - for( int i = 0 ; i < points.size() ; i ++ ){ - - // record中存储 点i 到所有其他点的距离出现的频次 - unordered_map record; - for(int j = 0 ; j < points.size() ; j ++) - if(j != i) - // 计算距离时不进行开根运算, 以保证精度 - record[dis(points[i], points[j])] += 1; - - for(unordered_map::iterator iter = record.begin() ; iter != record.end() ; iter ++) - res += (iter->second) * (iter->second - 1); - } - return res; - } - -private: - int dis(const pair &pa, const pair &pb){ - return (pa.first - pb.first) * (pa.first - pb.first) + - (pa.second - pb.second) * (pa.second - pb.second); - } -}; - -int main() { - - vector> vec; - vec.push_back(make_pair(0, 0)); - vec.push_back(make_pair(1, 0)); - vec.push_back(make_pair(2, 0)); - - cout << Solution().numberOfBoomerangs(vec) << endl; - - return 0; -} \ No newline at end of file diff --git a/0447-Number-of-Boomerangs/java-0447/src/Solution.java b/0447-Number-of-Boomerangs/java-0447/src/Solution.java deleted file mode 100755 index 0896c4c8..00000000 --- a/0447-Number-of-Boomerangs/java-0447/src/Solution.java +++ /dev/null @@ -1,46 +0,0 @@ -/// Source : https://leetcode.com/problems/number-of-boomerangs/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -import java.util.HashMap; - -/// Using Hash Map -/// Time Complexity: O(n^2) -/// Space Complexity: O(n) -public class Solution { - - public int numberOfBoomerangs(int[][] points) { - - int res = 0; - for( int i = 0 ; i < points.length ; i ++ ){ - - // record中存储 点i 到所有其他点的距离出现的频次 - HashMap record = new HashMap(); - for(int j = 0 ; j < points.length ; j ++) - if(j != i){ - // 计算距离时不进行开根运算, 以保证精度 - int dis = dis(points[i], points[j]); - if(record.containsKey(dis)) - record.put(dis, record.get(dis) + 1); - else - record.put(dis, 1); - } - - for(Integer dis: record.keySet()) - res += record.get(dis) * (record.get(dis) - 1); - } - - return res; - } - - private int dis(int[] pa, int pb[]){ - return (pa[0] - pb[0]) * (pa[0] - pb[0]) + - (pa[1] - pb[1]) * (pa[1] - pb[1]); - } - - public static void main(String[] args) { - - int[][] points = {{0, 0}, {1, 0}, {2, 0}}; - System.out.println((new Solution()).numberOfBoomerangs(points)); - } -} diff --git a/0454-4Sum-II/cpp-0454/CMakeLists.txt b/0454-4Sum-II/cpp-0454/CMakeLists.txt deleted file mode 100755 index 4930c8db..00000000 --- a/0454-4Sum-II/cpp-0454/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(cpp_0454) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - -set(SOURCE_FILES main.cpp) -add_executable(cpp_0454 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0454-4Sum-II/cpp-0454/main.cpp b/0454-4Sum-II/cpp-0454/main.cpp deleted file mode 100755 index cf6efe91..00000000 --- a/0454-4Sum-II/cpp-0454/main.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/// https://leetcode.com/problems/4sum-ii/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include -#include -#include - -using namespace std; - -/// Using Hash Map -/// Time Complexity: O(n^2) -/// Space Complexity: O(n^2) -class Solution { -public: - int fourSumCount(vector& A, vector& B, vector& C, vector& D) { - - unordered_map hashtable; - for(int i = 0 ; i < C.size() ; i ++) - for(int j = 0 ; j < D.size() ; j ++) - hashtable[C[i]+D[j]] += 1; - - int res = 0; - for(int i = 0 ; i < A.size() ; i ++) - for(int j = 0 ; j < B.size() ; j ++) - if(hashtable.find(-A[i]-B[j]) != hashtable.end()) - res += hashtable[-A[i]-B[j]]; - - return res; - } -}; - -int main() { - - int a[] = {1, 2}; - int b[] = {-2, -1}; - int c[] = {-1, 2}; - int d[] = {0, 2}; - vector a_vec = vector(a, a + sizeof(a)/sizeof(int)); - vector b_vec = vector(b, b + sizeof(b)/sizeof(int)); - vector c_vec = vector(c, c + sizeof(c)/sizeof(int)); - vector d_vec = vector(d, d + sizeof(d)/sizeof(int)); - - cout << Solution().fourSumCount(a_vec, b_vec, c_vec, d_vec) << endl; - - return 0; -} - diff --git a/0454-4Sum-II/cpp-0454/main2.cpp b/0454-4Sum-II/cpp-0454/main2.cpp deleted file mode 100755 index 1edcea4a..00000000 --- a/0454-4Sum-II/cpp-0454/main2.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/// https://leetcode.com/problems/4sum-ii/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -#include -#include -#include -#include -#include - -using namespace std; - -/// Another Way to use Hash Map -/// Time Complexity: O(n^2) -/// Space Complexity: O(n^2) -class Solution { -public: - int fourSumCount(vector& A, vector& B, vector& C, vector& D) { - - unordered_map hashtable1; - unordered_map hashtable2; - - for(int i = 0 ; i < A.size() ; i ++) - for(int j = 0 ; j < B.size() ; j ++) - hashtable1[A[i]+B[j]] += 1; - - for(int i = 0 ; i < C.size() ; i ++) - for(int j = 0 ; j < D.size() ; j ++) - hashtable2[C[i]+D[j]] += 1; - - int res = 0; - for(unordered_map::iterator iter = hashtable1.begin() ; iter != hashtable1.end() ; iter ++) - if(hashtable2.find(-(iter->first)) != hashtable2.end()) - res += iter->second * hashtable2[-(iter->first)]; - - return res; - } -}; - -int main() { - - int a[] = {1, 2}; - int b[] = {-2, -1}; - int c[] = {-1, 2}; - int d[] = {0, 2}; - vector a_vec = vector(a, a + sizeof(a)/sizeof(int)); - vector b_vec = vector(b, b + sizeof(b)/sizeof(int)); - vector c_vec = vector(c, c + sizeof(c)/sizeof(int)); - vector d_vec = vector(d, d + sizeof(d)/sizeof(int)); - - cout << Solution().fourSumCount(a_vec, b_vec, c_vec, d_vec) << endl; - - return 0; -} - diff --git a/0454-4Sum-II/java-0454/src/Solution1.java b/0454-4Sum-II/java-0454/src/Solution1.java deleted file mode 100755 index a9d0dfde..00000000 --- a/0454-4Sum-II/java-0454/src/Solution1.java +++ /dev/null @@ -1,44 +0,0 @@ -/// https://leetcode.com/problems/4sum-ii/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -import java.util.HashMap; - -/// Using Hash Map -/// Time Complexity: O(n^2) -/// Space Complexity: O(n^2) -public class Solution1 { - - public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { - - if(A == null || B == null || C == null || D == null) - throw new IllegalArgumentException("Illegal argument"); - - HashMap map = new HashMap(); - for(int i = 0 ; i < C.length ; i ++) - for(int j = 0 ; j < D.length ; j ++){ - int sum = C[i] + D[j]; - if(map.containsKey(sum)) - map.put(sum, map.get(sum) + 1); - else - map.put(sum, 1); - } - - int res = 0; - for(int i = 0 ; i < A.length ; i ++) - for(int j = 0 ; j < B.length ; j ++) - if(map.containsKey(-A[i]-B[j])) - res += map.get(-A[i]-B[j]); - - return res; - } - - public static void main(String[] args) { - - int[] a = {1, 2}; - int[] b = {-2, -1}; - int[] c = {-1, 2}; - int[] d = {0, 2}; - System.out.println((new Solution1()).fourSumCount(a, b, c, d)); - } -} diff --git a/0454-4Sum-II/java-0454/src/Solution2.java b/0454-4Sum-II/java-0454/src/Solution2.java deleted file mode 100755 index bb9fc7ed..00000000 --- a/0454-4Sum-II/java-0454/src/Solution2.java +++ /dev/null @@ -1,54 +0,0 @@ -/// https://leetcode.com/problems/4sum-ii/description/ -/// Author : liuyubobobo -/// Time : 2017-11-15 - -import java.util.HashMap; - -/// Another Way to use Hash Map -/// Time Complexity: O(n^2) -/// Space Complexity: O(n^2) -public class Solution2 { - - public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { - - if(A == null || B == null || C == null || D == null) - throw new IllegalArgumentException("Illegal argument"); - - HashMap mapAB = new HashMap(); - for(int i = 0 ; i < A.length ; i ++) - for(int j = 0 ; j < B.length ; j ++){ - int sum = A[i] + B[j]; - if(mapAB.containsKey(sum)) - mapAB.put(sum, mapAB.get(sum) + 1); - else - mapAB.put(sum, 1); - } - - HashMap mapCD = new HashMap(); - for(int i = 0 ; i < C.length ; i ++) - for(int j = 0 ; j < D.length ; j ++){ - int sum = C[i] + D[j]; - if(mapCD.containsKey(sum)) - mapCD.put(sum, mapCD.get(sum) + 1); - else - mapCD.put(sum, 1); - } - - int res = 0; - for(Integer sumab: mapAB.keySet()){ - if(mapCD.containsKey(-sumab)) - res += mapAB.get(sumab) * mapCD.get(-sumab); - } - - return res; - } - - public static void main(String[] args) { - - int[] a = {1, 2}; - int[] b = {-2, -1}; - int[] c = {-1, 2}; - int[] d = {0, 2}; - System.out.println((new Solution2()).fourSumCount(a, b, c, d)); - } -}