From b62612a8d7505d2c9c89ab09de6ac8ced69fd40c Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Mon, 29 Apr 2024 05:53:01 +0800 Subject: [PATCH] leetcode: Remove cpp impl --- .../CMakeLists.txt | 4 - .../main.c | 52 ------- .../CMakeLists.txt | 4 - .../main.cpp | 19 --- .../CMakeLists.txt | 4 - .../cpp/0008.string-to-integer-atoi/main.c | 79 ---------- .../0014.longest-common-prefix/CMakeLists.txt | 4 - .../cpp/0014.longest-common-prefix/main.c | 50 ------ .../cpp/0016.3sum-closest/CMakeLists.txt | 4 - src/leetcode/cpp/0016.3sum-closest/main.cpp | 59 -------- src/leetcode/cpp/0018.4sum/CMakeLists.txt | 4 - src/leetcode/cpp/0018.4sum/main.cpp | 82 ---------- .../CMakeLists.txt | 4 - .../main.c | 143 ------------------ .../cpp/0020.valid-parentheses/CMakeLists.txt | 4 - .../cpp/0020.valid-parentheses/main.cpp | 43 ------ .../CMakeLists.txt | 4 - .../cpp/0021.merge-two-sorted-lists/main.c | 93 ------------ .../CMakeLists.txt | 4 - .../cpp/0027.remove-element/CMakeLists.txt | 4 - src/leetcode/cpp/0027.remove-element/main.c | 39 ----- .../cpp/0031.next-permutation/CMakeLists.txt | 4 - src/leetcode/cpp/0031.next-permutation/main.c | 0 .../CMakeLists.txt | 4 - .../main.cpp | 59 -------- .../CMakeLists.txt | 4 - .../cpp/0035.search-insert-position/main.c | 48 ------ .../cpp/0053.maximum-subarray/CMakeLists.txt | 4 - src/leetcode/cpp/0053.maximum-subarray/main.c | 0 .../cpp/0061.rotate-list/CMakeLists.txt | 4 - src/leetcode/cpp/0061.rotate-list/main.c | 138 ----------------- src/leetcode/cpp/0061.rotate-list/time.md | 3 - .../cpp/0062.unique-paths/CMakeLists.txt | 4 - src/leetcode/cpp/0066.plus-one/CMakeLists.txt | 4 - src/leetcode/cpp/0066.plus-one/main.c | 56 ------- src/leetcode/cpp/0069.sqrtx/CMakeLists.txt | 4 - src/leetcode/cpp/0069.sqrtx/main.cpp | 98 ------------ .../cpp/0070.climbing-stairs/CMakeLists.txt | 4 - src/leetcode/cpp/0070.climbing-stairs/main.c | 41 ----- .../cpp/0075.sort-colors/CMakeLists.txt | 4 - src/leetcode/cpp/0075.sort-colors/main.cpp | 49 ------ .../CMakeLists.txt | 4 - .../main.c | 103 ------------- .../CMakeLists.txt | 4 - .../main.c | 83 ---------- .../cpp/0086.partition-list/CMakeLists.txt | 4 - src/leetcode/cpp/0086.partition-list/main.c | 110 -------------- .../CMakeLists.txt | 4 - .../cpp/0092.reverse-linked-list-ii/main.c | 110 -------------- .../0093.restore-ip-addresses/CMakeLists.txt | 4 - .../cpp/0093.restore-ip-addresses/main.cpp | 78 ---------- .../CMakeLists.txt | 4 - .../0098.validate-binary-search-tree/main.c | 82 ---------- .../cpp/0101.symmetric-tree/CMakeLists.txt | 4 - src/leetcode/cpp/0101.symmetric-tree/main.c | 36 ----- .../CMakeLists.txt | 4 - .../main.cpp | 86 ----------- .../CMakeLists.txt | 4 - .../main.cpp | 56 ------- .../CMakeLists.txt | 4 - .../main.cpp | 51 ------- .../CMakeLists.txt | 4 - .../main.c | 18 --- .../CMakeLists.txt | 4 - .../main.c | 138 ----------------- .../cpp/0137.single-number-ii/CMakeLists.txt | 4 - .../cpp/0137.single-number-ii/main.cpp | 44 ------ .../cpp/0139.word-break/CMakeLists.txt | 4 - src/leetcode/cpp/0139.word-break/main.cpp | 18 --- .../cpp/0141.linked-list-cycle/CMakeLists.txt | 4 - .../cpp/0141.linked-list-cycle/main.c | 119 --------------- .../0142.linked-list-cycle-ii/CMakeLists.txt | 4 - .../cpp/0142.linked-list-cycle-ii/main.c | 102 ------------- .../CMakeLists.txt | 4 - .../main.cpp | 36 ----- .../cpp/0146.lru-cache/CMakeLists.txt | 4 - src/leetcode/cpp/0146.lru-cache/main.cpp | 108 ------------- .../CMakeLists.txt | 4 - .../main.c | 87 ----------- .../cpp/0162.find-peak-element/CMakeLists.txt | 4 - .../CMakeLists.txt | 4 - .../cpp/0200.number-of-islands/CMakeLists.txt | 4 - .../CMakeLists.txt | 4 - .../0203.remove-linked-list-elements/main.c | 113 -------------- .../0206.reverse-linked-list/CMakeLists.txt | 4 - .../cpp/0206.reverse-linked-list/main.c | 79 ---------- .../CMakeLists.txt | 4 - .../CMakeLists.txt | 4 - .../cpp/0234.palindrome-linked-list/main.c | 102 ------------- .../CMakeLists.txt | 4 - .../0237.delete-node-in-a-linked-list/main.c | 73 --------- .../cpp/0268.missing-number/CMakeLists.txt | 4 - src/leetcode/cpp/0268.missing-number/main.cpp | 44 ------ .../0308.odd-even-linked-list/CMakeLists.txt | 4 - .../cpp/0308.odd-even-linked-list/main.c | 92 ----------- .../0367.valid-perfect-square/CMakeLists.txt | 4 - .../cpp/0367.valid-perfect-square/main.cpp | 45 ------ .../0414.third-maximum-number/CMakeLists.txt | 4 - .../cpp/0414.third-maximum-number/main.cpp | 58 ------- .../0443.string-compression/CMakeLists.txt | 4 - .../0445.add-two-numbers-ii/CMakeLists.txt | 4 - .../cpp/0445.add-two-numbers-ii/main.c | 89 ----------- .../0485.max-consecutive-ones/CMakeLists.txt | 4 - .../cpp/0485.max-consecutive-ones/main.cpp | 42 ----- .../cpp/0605.can-place-flowers/CMakeLists.txt | 4 - .../cpp/0605.can-place-flowers/main.cpp | 75 --------- .../CMakeLists.txt | 4 - .../main.cpp | 44 ------ .../0665.non-decreasing-array/CMakeLists.txt | 4 - .../cpp/0665.non-decreasing-array/main.cpp | 60 -------- .../0695.max-area-of-island/CMakeLists.txt | 4 - .../cpp/0704.binary-search/CMakeLists.txt | 4 - src/leetcode/cpp/0704.binary-search/main.c | 30 ---- .../cpp/0925.long-pressed-name/CMakeLists.txt | 4 - .../cpp/0925.long-pressed-name/main.cpp | 45 ------ .../cpp/0994.rotting-oranges/CMakeLists.txt | 4 - .../CMakeLists.txt | 4 - .../1090.largest-values-from-labels/main.c | 0 .../CMakeLists.txt | 4 - .../main.c | 0 .../CMakeLists.txt | 4 - .../cpp/1446.consecutive-characters/main.cpp | 44 ------ src/leetcode/cpp/CMakeLists.txt | 75 --------- src/leetcode/cpp/template/CMakeLists.txt | 4 - src/leetcode/cpp/template/main.cpp | 24 --- 125 files changed, 4014 deletions(-) delete mode 100644 src/leetcode/cpp/0003.longest-substring-without-repeating-characters/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0003.longest-substring-without-repeating-characters/main.c delete mode 100644 src/leetcode/cpp/0005.longest-palindromic-substring/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0005.longest-palindromic-substring/main.cpp delete mode 100644 src/leetcode/cpp/0008.string-to-integer-atoi/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0008.string-to-integer-atoi/main.c delete mode 100644 src/leetcode/cpp/0014.longest-common-prefix/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0014.longest-common-prefix/main.c delete mode 100644 src/leetcode/cpp/0016.3sum-closest/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0016.3sum-closest/main.cpp delete mode 100644 src/leetcode/cpp/0018.4sum/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0018.4sum/main.cpp delete mode 100644 src/leetcode/cpp/0019.remove-nth-node-from-end-of-list/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0019.remove-nth-node-from-end-of-list/main.c delete mode 100644 src/leetcode/cpp/0020.valid-parentheses/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0020.valid-parentheses/main.cpp delete mode 100644 src/leetcode/cpp/0021.merge-two-sorted-lists/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0021.merge-two-sorted-lists/main.c delete mode 100644 src/leetcode/cpp/0025.reverse-nodes-in-k-group/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0027.remove-element/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0027.remove-element/main.c delete mode 100644 src/leetcode/cpp/0031.next-permutation/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0031.next-permutation/main.c delete mode 100644 src/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/main.cpp delete mode 100644 src/leetcode/cpp/0035.search-insert-position/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0035.search-insert-position/main.c delete mode 100644 src/leetcode/cpp/0053.maximum-subarray/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0053.maximum-subarray/main.c delete mode 100644 src/leetcode/cpp/0061.rotate-list/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0061.rotate-list/main.c delete mode 100644 src/leetcode/cpp/0061.rotate-list/time.md delete mode 100644 src/leetcode/cpp/0062.unique-paths/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0066.plus-one/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0066.plus-one/main.c delete mode 100644 src/leetcode/cpp/0069.sqrtx/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0069.sqrtx/main.cpp delete mode 100644 src/leetcode/cpp/0070.climbing-stairs/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0070.climbing-stairs/main.c delete mode 100644 src/leetcode/cpp/0075.sort-colors/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0075.sort-colors/main.cpp delete mode 100644 src/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/main.c delete mode 100644 src/leetcode/cpp/0083.remove-duplicates-from-sorted-list/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0083.remove-duplicates-from-sorted-list/main.c delete mode 100644 src/leetcode/cpp/0086.partition-list/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0086.partition-list/main.c delete mode 100644 src/leetcode/cpp/0092.reverse-linked-list-ii/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0092.reverse-linked-list-ii/main.c delete mode 100644 src/leetcode/cpp/0093.restore-ip-addresses/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0093.restore-ip-addresses/main.cpp delete mode 100644 src/leetcode/cpp/0098.validate-binary-search-tree/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0098.validate-binary-search-tree/main.c delete mode 100644 src/leetcode/cpp/0101.symmetric-tree/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0101.symmetric-tree/main.c delete mode 100644 src/leetcode/cpp/0102.binary-tree-level-order-traversal/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0102.binary-tree-level-order-traversal/main.cpp delete mode 100644 src/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/main.cpp delete mode 100644 src/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/main.cpp delete mode 100644 src/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/main.c delete mode 100644 src/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/main.c delete mode 100644 src/leetcode/cpp/0137.single-number-ii/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0137.single-number-ii/main.cpp delete mode 100644 src/leetcode/cpp/0139.word-break/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0139.word-break/main.cpp delete mode 100644 src/leetcode/cpp/0141.linked-list-cycle/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0141.linked-list-cycle/main.c delete mode 100644 src/leetcode/cpp/0142.linked-list-cycle-ii/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0142.linked-list-cycle-ii/main.c delete mode 100644 src/leetcode/cpp/0144.binary-tree-preorder-traversal/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0144.binary-tree-preorder-traversal/main.cpp delete mode 100644 src/leetcode/cpp/0146.lru-cache/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0146.lru-cache/main.cpp delete mode 100644 src/leetcode/cpp/0160.intersection-of-two-linked-lists/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0160.intersection-of-two-linked-lists/main.c delete mode 100644 src/leetcode/cpp/0162.find-peak-element/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0199.binary-tree-right-side-view/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0200.number-of-islands/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0203.remove-linked-list-elements/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0203.remove-linked-list-elements/main.c delete mode 100644 src/leetcode/cpp/0206.reverse-linked-list/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0206.reverse-linked-list/main.c delete mode 100644 src/leetcode/cpp/0215.kth-largest-element-in-an-array/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0234.palindrome-linked-list/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0234.palindrome-linked-list/main.c delete mode 100644 src/leetcode/cpp/0237.delete-node-in-a-linked-list/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0237.delete-node-in-a-linked-list/main.c delete mode 100644 src/leetcode/cpp/0268.missing-number/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0268.missing-number/main.cpp delete mode 100644 src/leetcode/cpp/0308.odd-even-linked-list/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0308.odd-even-linked-list/main.c delete mode 100644 src/leetcode/cpp/0367.valid-perfect-square/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0367.valid-perfect-square/main.cpp delete mode 100644 src/leetcode/cpp/0414.third-maximum-number/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0414.third-maximum-number/main.cpp delete mode 100644 src/leetcode/cpp/0443.string-compression/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0445.add-two-numbers-ii/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0445.add-two-numbers-ii/main.c delete mode 100644 src/leetcode/cpp/0485.max-consecutive-ones/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0485.max-consecutive-ones/main.cpp delete mode 100644 src/leetcode/cpp/0605.can-place-flowers/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0605.can-place-flowers/main.cpp delete mode 100644 src/leetcode/cpp/0628.maximum-product-of-three-numbers/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0628.maximum-product-of-three-numbers/main.cpp delete mode 100644 src/leetcode/cpp/0665.non-decreasing-array/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0665.non-decreasing-array/main.cpp delete mode 100644 src/leetcode/cpp/0695.max-area-of-island/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0704.binary-search/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0704.binary-search/main.c delete mode 100644 src/leetcode/cpp/0925.long-pressed-name/CMakeLists.txt delete mode 100644 src/leetcode/cpp/0925.long-pressed-name/main.cpp delete mode 100644 src/leetcode/cpp/0994.rotting-oranges/CMakeLists.txt delete mode 100644 src/leetcode/cpp/1090.largest-values-from-labels/CMakeLists.txt delete mode 100644 src/leetcode/cpp/1090.largest-values-from-labels/main.c delete mode 100644 src/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/CMakeLists.txt delete mode 100644 src/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/main.c delete mode 100644 src/leetcode/cpp/1446.consecutive-characters/CMakeLists.txt delete mode 100644 src/leetcode/cpp/1446.consecutive-characters/main.cpp delete mode 100644 src/leetcode/cpp/CMakeLists.txt delete mode 100644 src/leetcode/cpp/template/CMakeLists.txt delete mode 100644 src/leetcode/cpp/template/main.cpp diff --git a/src/leetcode/cpp/0003.longest-substring-without-repeating-characters/CMakeLists.txt b/src/leetcode/cpp/0003.longest-substring-without-repeating-characters/CMakeLists.txt deleted file mode 100644 index 9b295693..00000000 --- a/src/leetcode/cpp/0003.longest-substring-without-repeating-characters/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0003-longest-substring-without-repeating-characters - main.c - ) diff --git a/src/leetcode/cpp/0003.longest-substring-without-repeating-characters/main.c b/src/leetcode/cpp/0003.longest-substring-without-repeating-characters/main.c deleted file mode 100644 index 87ceec72..00000000 --- a/src/leetcode/cpp/0003.longest-substring-without-repeating-characters/main.c +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -int lengthOfLongestSubstring(char* s) { - int n = strlen(s); - if (n == 0) { - return 0; - } - if (n == 1) { - return 1; - } - - int max_val = 1; - int len = 0; - - for (int i = 0; i < n; ++i) { - for (int j = i + 1; j < n; ++j) { - if (s[i] == s[j]) { - break; - } - - len = j - i - 1; - if (max_val < len) { - max_val = len; - } - } - } - return max_val; -} - -void checkSolutioin(char* s, int exp) { - int out = lengthOfLongestSubstring(s); - printf("out: %d, exp: %d\n", out, exp); - assert(out == exp); -} - -int main() { - char* s1 = "abcabcbb"; - checkSolutioin(s1, 3); - char* s2 = "bbbbbb"; - checkSolutioin(s2, 1); - char* s3 = "pwwkew"; - checkSolutioin(s3, 3); - - return 0; -} diff --git a/src/leetcode/cpp/0005.longest-palindromic-substring/CMakeLists.txt b/src/leetcode/cpp/0005.longest-palindromic-substring/CMakeLists.txt deleted file mode 100644 index dd37efa5..00000000 --- a/src/leetcode/cpp/0005.longest-palindromic-substring/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0005-longest-palindromic-substring - main.cpp - ) diff --git a/src/leetcode/cpp/0005.longest-palindromic-substring/main.cpp b/src/leetcode/cpp/0005.longest-palindromic-substring/main.cpp deleted file mode 100644 index 4b1a077e..00000000 --- a/src/leetcode/cpp/0005.longest-palindromic-substring/main.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -#include - -class Solution { - public: - static std::string longestPalindrome(std::string s) { - } -}; - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0008.string-to-integer-atoi/CMakeLists.txt b/src/leetcode/cpp/0008.string-to-integer-atoi/CMakeLists.txt deleted file mode 100644 index b83f2dbd..00000000 --- a/src/leetcode/cpp/0008.string-to-integer-atoi/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0008-string-to-integer-atoi - main.c - ) diff --git a/src/leetcode/cpp/0008.string-to-integer-atoi/main.c b/src/leetcode/cpp/0008.string-to-integer-atoi/main.c deleted file mode 100644 index fcef919d..00000000 --- a/src/leetcode/cpp/0008.string-to-integer-atoi/main.c +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -int myAtoi(char* s) { - char* ptr = s; - long val = 0; - int is_negative = 0; - int has_sign = 0; - int digit_count = 0; - const long kIntMax = -(long)INT_MIN; - - while (*ptr != '\0') { - char c = *ptr; - ptr += 1; - - if (c == ' ' && has_sign == 0 && digit_count == 0) { - continue; - } - - if (c == '-' || c == '+') { - if (has_sign || digit_count) { - // Invalid sign. - break; - } - - has_sign = 1; - if (c == '-') { - is_negative = 1; - } - } else if (c >= '0' && c <= '9') { - int digit = c - '0'; - val = val * 10 + digit; - digit_count += 1; - // FIXME(shaohua): overflow - if (val > kIntMax) { - val = kIntMax; - } - } else { - // End of valid digits. - break; - } - } - - if (val >= kIntMax) { - if (is_negative) { - val = INT_MIN; - } else { - val = INT_MAX; - } - } else if (is_negative) { - val = -val; - } - - return val; -} - -void checkSolution(char* s, int exp) { - int val = myAtoi(s); - printf("val: %d, exp: %d\n", val, exp); - assert(val == exp); -} - -int main() { - checkSolution("42", 42); - checkSolution(" -42", -42); - checkSolution("4193 with words", 4193); - checkSolution("-91283472332", -2147483648); - checkSolution(" +0 123", 0); - checkSolution("20000000000000000000", 2147483647); - checkSolution("-2147483647", -2147483647); - checkSolution("-5-", -5); - return 0; -} diff --git a/src/leetcode/cpp/0014.longest-common-prefix/CMakeLists.txt b/src/leetcode/cpp/0014.longest-common-prefix/CMakeLists.txt deleted file mode 100644 index d4dc19b0..00000000 --- a/src/leetcode/cpp/0014.longest-common-prefix/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0014-longest-common-prefix - main.c - ) diff --git a/src/leetcode/cpp/0014.longest-common-prefix/main.c b/src/leetcode/cpp/0014.longest-common-prefix/main.c deleted file mode 100644 index 27d86d02..00000000 --- a/src/leetcode/cpp/0014.longest-common-prefix/main.c +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -char* longestCommonPrefix(char** strs, int strsSize) { - int index = 0; - int stop = 0; - while (!stop) { - if (strs[0][index] == '\0') { - break; - } - - for (int i = 1; i < strsSize; ++i) { - if (strs[0][index] != strs[i][index]) { - stop = 1; - break; - } - } - if (!stop) { - index += 1; - } - } - - char* out = (char*)malloc(index + 1); - assert(out); - memcpy(out, strs[0], index); - out[index] = '\0'; - return out; -} - -void checkSolutioin(char** strs, int strsSize, char* expected) { - char* got = longestCommonPrefix(strs, strsSize); - printf("got: %s, expected: %s\n", got, expected); - assert(strcmp(got, expected)); -} - -int main() { - char* strs[] = {"flower","flow","flight"}; - checkSolutioin(strs, 3, "fl"); - - char* strs2[] = {"dog","racecar","car"}; - checkSolutioin(strs2, 3, ""); - - return 0; -} diff --git a/src/leetcode/cpp/0016.3sum-closest/CMakeLists.txt b/src/leetcode/cpp/0016.3sum-closest/CMakeLists.txt deleted file mode 100644 index 6a4c3bf7..00000000 --- a/src/leetcode/cpp/0016.3sum-closest/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0016-3sum-closest - main.cpp - ) diff --git a/src/leetcode/cpp/0016.3sum-closest/main.cpp b/src/leetcode/cpp/0016.3sum-closest/main.cpp deleted file mode 100644 index a553bece..00000000 --- a/src/leetcode/cpp/0016.3sum-closest/main.cpp +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include - -class Solution { - public: - static - int threeSumClosest(std::vector& nums, int target) { - std::sort(nums.begin(), nums.end()); - int min_diff = INT_MAX; - int result = 0; - - for (size_t i = 0; i < nums.size() - 2; ++i) { - int left = i + 1; - int right = nums.size() - 1; - int first = nums[i]; - - while (left < right) { - int sum = first + nums[left] + nums[right]; - if (sum == target) { - return sum; - } - - int d = std::abs(sum - target); - if (d < min_diff) { - min_diff = d; - result = sum; - } - if (sum < target) { - left += 1; - } else if (sum > target) { - right -= 1; - } - } - } - - return result; - } - -}; - -void checkSolution(int* nums, int numsSize, int target, int expectedSum) { - std::vector vector(nums, nums + numsSize); - int sum = Solution::threeSumClosest(vector, target); - assert(sum == expectedSum); -} - -int main() { - int arr1[] = {-1, 2, 1, -4}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 1, 2); - - return 0; -} diff --git a/src/leetcode/cpp/0018.4sum/CMakeLists.txt b/src/leetcode/cpp/0018.4sum/CMakeLists.txt deleted file mode 100644 index 051c4881..00000000 --- a/src/leetcode/cpp/0018.4sum/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0018-4sum - main.cpp - ) diff --git a/src/leetcode/cpp/0018.4sum/main.cpp b/src/leetcode/cpp/0018.4sum/main.cpp deleted file mode 100644 index 48cfe3bc..00000000 --- a/src/leetcode/cpp/0018.4sum/main.cpp +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include -#include - -class Solution { -public: - static - std::vector> fourSum(std::vector& nums, int target) { - int len = nums.size(); - std::vector> result; - std::sort(nums.begin(), nums.end()); - - for (int i = 0; i < len-3; ++i) { - if (i > 0 && nums[i] == nums[i-1]) { - continue; - } - - for (int j = i+1; j < len-2; ++j) { - if (j > i + 1 && nums[j] == nums[j-1]) { - continue; - } - - int left = j + 1; - int right = len - 1; - long remains = (long)target - (long)nums[i] - (long)nums[j]; - while (left < right) { - long sum = (long)nums[left] + (long)nums[right]; - if (remains < sum) { - right -= 1; - } else if (remains > sum) { - left += 1; - } else { - result.push_back({nums[i], nums[j], nums[left], nums[right]}); - while (left < right && nums[left] == nums[left+1]) { - left += 1; - } - while (left < right && nums[right] == nums[right-1]) { - right -= 1; - } - left += 1; - right -= 1; - } - } - } - } - - return result; - } -}; - -void checkSolution(int* arr, int arr_len, int target, int out_len) { - std::vector nums{arr, arr + arr_len}; - std::vector> result = Solution::fourSum(nums, target); - for (const auto& entry : result) { - printf("[%d, %d, %d, %d]\n", entry[0], entry[1], entry[2], entry[3]); - } - printf("result size: %d, expected; %d\n", result.size(), out_len); - assert(result.size() == out_len); -} - -int main() { - int arr1[] = {1, 0, -1, 0, -2, 2}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 0, 3); - - int arr2[] = {2, 2, 2, 2, 2}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 8, 1); - - int arr3[] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}; - checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 8, 1); - - int arr4[] = {1, -2, -5, -4, -3, 3, 3, 5}; - checkSolution(arr4, sizeof(arr4) / sizeof(arr4[0]), -11, 1); - - return 0; -} diff --git a/src/leetcode/cpp/0019.remove-nth-node-from-end-of-list/CMakeLists.txt b/src/leetcode/cpp/0019.remove-nth-node-from-end-of-list/CMakeLists.txt deleted file mode 100644 index a1c73c62..00000000 --- a/src/leetcode/cpp/0019.remove-nth-node-from-end-of-list/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0019-remove-nth-node-from-end-of-list - main.c - ) diff --git a/src/leetcode/cpp/0019.remove-nth-node-from-end-of-list/main.c b/src/leetcode/cpp/0019.remove-nth-node-from-end-of-list/main.c deleted file mode 100644 index 2dfbc678..00000000 --- a/src/leetcode/cpp/0019.remove-nth-node-from-end-of-list/main.c +++ /dev/null @@ -1,143 +0,0 @@ - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -static struct ListNode* listNodeNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -static struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNodeNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -static int listSize(struct ListNode* list) { - int size = 0; - while (list != NULL) { - size += 1; - list = list->next; - } - return size; -} - -static struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { - assert(head != NULL); - assert(n >= 0); - - struct ListNode* tmp = NULL; - int size = listSize(head); - assert(n <= size); - - if (size == n) { - // Remove head node. - tmp = head; - head = head->next; - free(tmp); - return head; - } - - int index = 0; - struct ListNode* head_clone = head; - while (index + 1 < size - n && head != NULL) { - head = head->next; - index += 1; - } - assert(head != NULL); - if (head->next != NULL) { - tmp = head->next; - head->next = head->next->next; - free(tmp); - } - - return head_clone; -} - -static struct ListNode* removeNthFromEnd2Helper(struct ListNode* current, int n, int* count) { - // End of list. - if (current == NULL) { - return NULL; - } - - struct ListNode* next = removeNthFromEnd2Helper(current->next, n, count); - *count += 1; - if (*count == n) { - free(current); - return next; - } - - current->next = next; - return current; -} - -static struct ListNode* removeNthFromEnd2(struct ListNode* head, int n) { - int count = 0; - return removeNthFromEnd2Helper(head, n, &count); -} - -static struct ListNode* removeNthFromEnd3(struct ListNode* head, int n) { - assert(n >= 0); - if (head == NULL) { - return NULL; - } - - // Used for counting. - struct ListNode* fast = head; - struct ListNode* slow = head; - - for (int i = 0; i < n; ++i) { - fast = fast->next; - } - if (fast == NULL) { - return NULL; - } - - while (fast->next != NULL) { - fast = fast->next; - slow = slow->next; - } - struct ListNode* tmp = slow->next; - assert(tmp != NULL); - slow->next = slow->next->next; - free(tmp); - return head; -} - -static void checkSolution(int arr[], int arr_len, int target) { - struct ListNode* l1 = listFromArray(arr, arr_len); - //struct ListNode* l2 = removeNthFromEnd(l1, target); - struct ListNode* l2 = removeNthFromEnd2(l1, target); - //struct ListNode* l2 = removeNthFromEnd3(l1, target); - while (l2 != NULL) { - printf("%d, ", l2->val); - l2 = l2->next; - } - printf("\n"); -} - -int main() { - int arr1[] = {1, 2, 3, 4, 5}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 2); - - int arr2[] = {1}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 1); - - int arr3[] = {1, 2}; - checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 1); - - return 0; -} diff --git a/src/leetcode/cpp/0020.valid-parentheses/CMakeLists.txt b/src/leetcode/cpp/0020.valid-parentheses/CMakeLists.txt deleted file mode 100644 index 540e3485..00000000 --- a/src/leetcode/cpp/0020.valid-parentheses/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0020-valid-parentheses - main.cpp - ) diff --git a/src/leetcode/cpp/0020.valid-parentheses/main.cpp b/src/leetcode/cpp/0020.valid-parentheses/main.cpp deleted file mode 100644 index aa98be98..00000000 --- a/src/leetcode/cpp/0020.valid-parentheses/main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include - -#include -#include - -bool isValid(std::string s) { - std::stack stack; - - for (char c : s) { - if (c == '(' || c == '[' || c == '{') { - stack.push(c); - } else { - if (stack.empty()) { - return false; - } - char old_c = stack.top(); - stack.pop(); - if ((old_c == '(' && c != ')') || - (old_c == '[' && c != ']') || - (old_c == '{' && c != '}')) { - return false; - } - } - } - - return stack.empty(); -} - -void checkSolution(char* s, bool exp_valid) { - bool is_valid = isValid(s); - assert(is_valid == exp_valid); -} - -int main() { - checkSolution("()", true); - checkSolution("()[]{}", true); - - return 0; -} diff --git a/src/leetcode/cpp/0021.merge-two-sorted-lists/CMakeLists.txt b/src/leetcode/cpp/0021.merge-two-sorted-lists/CMakeLists.txt deleted file mode 100644 index cb2b96ec..00000000 --- a/src/leetcode/cpp/0021.merge-two-sorted-lists/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0021-merge-two-sorted-lists - main.c - ) diff --git a/src/leetcode/cpp/0021.merge-two-sorted-lists/main.c b/src/leetcode/cpp/0021.merge-two-sorted-lists/main.c deleted file mode 100644 index 694667fd..00000000 --- a/src/leetcode/cpp/0021.merge-two-sorted-lists/main.c +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode *next; -}; - -struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = arr[i]; - node->next = list; - list = node; - } - return list; -} - -static -struct ListNode* listReverse(struct ListNode* list) { - assert(list != NULL); - struct ListNode* prev = NULL; - struct ListNode* next = NULL; - while (list != NULL) { - next = list->next; - list->next = prev; - prev = list; - list = next; - } - return prev; -} - -struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { - if (l1 == NULL) { - return l2; - } - if (l2 == NULL) { - return l1; - } - l1 = listReverse(l1); - l2 = listReverse(l2); - - struct ListNode* l3 = NULL; - struct ListNode* node = NULL; - - while (l1 != NULL && l2 != NULL) { - if (l1->val >= l2->val) { - node = l1; - l1 = l1->next; - } else { - node = l2; - l2 = l2->next; - } - node->next = l3; - l3 = node; - } - while (l1 != NULL) { - node = l1; - l1 = l1->next; - node->next = l3; - l3 = node; - } - while (l2 != NULL) { - node = l2; - l2 = l2->next; - node->next = l3; - l3 = node; - } - - return l3; -} - -int main() { - int arr1[] = {1, 2, 4}; - int arr2[] = {1, 3, 4}; - struct ListNode* l1 = listFromArray(arr1, sizeof(arr1) / sizeof(arr1[0])); - struct ListNode* l2 = listFromArray(arr2, sizeof(arr2) / sizeof(arr2[0])); - struct ListNode* l3 = mergeTwoLists(l1, l2); - while (l3 != NULL) { - printf("%d, ", l3->val); - l3 = l3->next; - } - printf("\n"); - - return 0; -} diff --git a/src/leetcode/cpp/0025.reverse-nodes-in-k-group/CMakeLists.txt b/src/leetcode/cpp/0025.reverse-nodes-in-k-group/CMakeLists.txt deleted file mode 100644 index 79e865c3..00000000 --- a/src/leetcode/cpp/0025.reverse-nodes-in-k-group/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0025.reverse-nodes-in-k-group - main.c - ) diff --git a/src/leetcode/cpp/0027.remove-element/CMakeLists.txt b/src/leetcode/cpp/0027.remove-element/CMakeLists.txt deleted file mode 100644 index 1d5e168c..00000000 --- a/src/leetcode/cpp/0027.remove-element/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0027-remove-element - main.c - ) diff --git a/src/leetcode/cpp/0027.remove-element/main.c b/src/leetcode/cpp/0027.remove-element/main.c deleted file mode 100644 index 78881098..00000000 --- a/src/leetcode/cpp/0027.remove-element/main.c +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -int removeElement(int* nums, int numsSize, int val) { - for (int i = 0; i < numsSize; /* empty */) { - if (nums[i] == val) { - // Implements swap_remove() - nums[i] = nums[numsSize - 1]; - numsSize -= 1; - } else { - i += 1; - } - } - return numsSize; -} - -void checkSolution(int* nums, int numsSize, int val, int expected_k) { - int k = removeElement(nums, numsSize, val); - for (int i = 0; i < k; ++i) { - printf("%d, ", nums[i]); - } - printf("\n"); - assert(k == expected_k); -} - -int main() { - int arr1[] = {0, 1, 2, 2, 3, 0, 4, 2}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 2, 5); - - int arr2[] = {3, 2, 2, 3}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 3, 2); - - return 0; -} diff --git a/src/leetcode/cpp/0031.next-permutation/CMakeLists.txt b/src/leetcode/cpp/0031.next-permutation/CMakeLists.txt deleted file mode 100644 index df8bb6bc..00000000 --- a/src/leetcode/cpp/0031.next-permutation/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0031-next-permutation - main.c - ) diff --git a/src/leetcode/cpp/0031.next-permutation/main.c b/src/leetcode/cpp/0031.next-permutation/main.c deleted file mode 100644 index e69de29b..00000000 diff --git a/src/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/CMakeLists.txt b/src/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/CMakeLists.txt deleted file mode 100644 index 19cdd24c..00000000 --- a/src/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0034-find-first-and-last-position-of-element-in-sorted-array - main.cpp - ) diff --git a/src/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/main.cpp b/src/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/main.cpp deleted file mode 100644 index 8a28f8f4..00000000 --- a/src/leetcode/cpp/0034.find-first-and-last-position-of-element-in-sorted-array/main.cpp +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include - -class Solution { - public: - static - std::vector searchRange(std::vector& nums, int target) { - std::vector range; - range.push_back(-1); - range.push_back(-1); - if (nums.empty()) { - return range; - } - - int left = 0; - int right = nums.size() - 1; - - while (left <= right) { - int middle = left + (right - left) / 2; - if (nums[middle] == target) { - for (int i = middle; i >= 0 && nums[i] == target; --i) { - range[0] = i; - } - for (int i = middle; i < nums.size() && nums[i] == target; ++i) { - range[1] = i; - } - break; - } else if (nums[middle] < target) { - left = middle + 1; - } else { - right = middle - 1; - } - } - - return range; - } -}; - -void checkSolution(int* nums, int numsSize, int target, int left, int right) { - std::vector vec{nums, nums + numsSize}; - std::vector range = Solution::searchRange(vec, target); - assert(range[0] == left); - assert(range[1] == right); -} - -int main() { - int arr1[] = {5,7,7,8,8,10}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 8, 3, 4); - - int arr2[] = {1}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 1, 0, 0); - return 0; -} diff --git a/src/leetcode/cpp/0035.search-insert-position/CMakeLists.txt b/src/leetcode/cpp/0035.search-insert-position/CMakeLists.txt deleted file mode 100644 index b927e51b..00000000 --- a/src/leetcode/cpp/0035.search-insert-position/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0035-search-insert-position - main.c - ) diff --git a/src/leetcode/cpp/0035.search-insert-position/main.c b/src/leetcode/cpp/0035.search-insert-position/main.c deleted file mode 100644 index b6b32048..00000000 --- a/src/leetcode/cpp/0035.search-insert-position/main.c +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -// Binary search -int searchInsert(int* nums, int numsSize, int target) { - if (nums[numsSize - 1] < target) { - return numSize; - } - - int left = 0; - int right = numsSize - 1; - int middle = 0; - while (left < right) { - middle = left + (right - left) / 2; - if (nums[middle] == target) { - return middle; - } - if (nums[middle] > target) { - right = middle - 1; - } else { - left = middle + 1; - } - } - return right; -} - -void checkSolution(int* nums, int numsSize, int target, int expected) { - int out = searchInsert(nums, numsSize, target); - printf("out: %d, expected: %d\n", out, expected); - assert(out == expected); -} -int main() { - int arr1[] = {1, 3, 5, 6}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 5, 2); - - int arr2[] = {1, 3, 5, 6}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 2, 1); - - int arr3[] = {1, 3, 5, 6}; - checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 7, 4); - - return 0; -} diff --git a/src/leetcode/cpp/0053.maximum-subarray/CMakeLists.txt b/src/leetcode/cpp/0053.maximum-subarray/CMakeLists.txt deleted file mode 100644 index cce5f150..00000000 --- a/src/leetcode/cpp/0053.maximum-subarray/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0053-maximum-subarray - main.c - ) diff --git a/src/leetcode/cpp/0053.maximum-subarray/main.c b/src/leetcode/cpp/0053.maximum-subarray/main.c deleted file mode 100644 index e69de29b..00000000 diff --git a/src/leetcode/cpp/0061.rotate-list/CMakeLists.txt b/src/leetcode/cpp/0061.rotate-list/CMakeLists.txt deleted file mode 100644 index 82eb9c2c..00000000 --- a/src/leetcode/cpp/0061.rotate-list/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0061-rotate-list - main.c - ) diff --git a/src/leetcode/cpp/0061.rotate-list/main.c b/src/leetcode/cpp/0061.rotate-list/main.c deleted file mode 100644 index e545fc8f..00000000 --- a/src/leetcode/cpp/0061.rotate-list/main.c +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -static struct ListNode* listNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -static struct ListNode* listFromArray(int arr[], int len) { - assert(len >= 0); - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -static void listFree(struct ListNode* list) { - assert(list != NULL); - while (list != NULL) { - struct ListNode* tmp = list; - list = list->next; - free(tmp); - } -} - -static int listSize(struct ListNode* list) { - assert(list != NULL); - int len = 0; - while (list != NULL) { - len += 1; - list = list->next; - } - return len; -} - -struct ListNode* rotateRight(struct ListNode* head, int k) { - if (head == NULL) { - return NULL; - } - - assert(k >= 0); - - //const int len = listSize(head); - int len = 1; - struct ListNode* tail = head; - while (tail != NULL && tail->next != NULL) { - tail = tail->next; - len += 1; - } - - k = len - (k % len); - if (k == 0) { - return head; - } - - // Connect tail to head. - tail->next = head; - - // Count node from head to tail. - while (k > 0) { - head = head->next; - tail = tail->next; - k -= 1; - } - // Break the ring at k point. - tail->next = NULL; - - return head; -} - -struct ListNode* rotateRight2(struct ListNode* head, int k) { - if (head == NULL) { - return NULL; - } - assert(k >= 0); - - struct ListNode* fast = head; - struct ListNode* slow = head; - // FIXME(Shaohua): k might > len - while (k > 0) { - fast = fast->next; - k -= 1; - } - - while (fast != NULL && fast->next != NULL) { - fast = fast->next; - slow = slow->next; - } - // Connect tail to head node. - fast->next = head; - // Now slow is the new tail node. - head = slow->next; - slow->next = NULL; - - return head; -} - -static void checkResult(int arr[], int len, int k) { - struct ListNode* head = listFromArray(arr, len); - struct ListNode* new_head = rotateRight(head, k); - //struct ListNode* new_head = rotateRight2(head, k); - head = new_head; - while (head != NULL) { - printf("%d, ", head->val); - head = head->next; - } - printf("\n"); - free(new_head); -} - -int main() { - int arr1[] = {1,2,3,4,5}; - checkResult(arr1, sizeof(arr1) / sizeof(arr1[0]), 2); - - int arr2[] = {1,2}; - checkResult(arr2, sizeof(arr2) / sizeof(arr2[0]), 0); - - int arr3[] = {1}; - checkResult(arr3, sizeof(arr3) / sizeof(arr3[0]), 10); - - return 0; -} diff --git a/src/leetcode/cpp/0061.rotate-list/time.md b/src/leetcode/cpp/0061.rotate-list/time.md deleted file mode 100644 index 689fdcde..00000000 --- a/src/leetcode/cpp/0061.rotate-list/time.md +++ /dev/null @@ -1,3 +0,0 @@ - -# Time -- 00:32:30 diff --git a/src/leetcode/cpp/0062.unique-paths/CMakeLists.txt b/src/leetcode/cpp/0062.unique-paths/CMakeLists.txt deleted file mode 100644 index f1b713d6..00000000 --- a/src/leetcode/cpp/0062.unique-paths/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0062-unique-paths - main.c - ) diff --git a/src/leetcode/cpp/0066.plus-one/CMakeLists.txt b/src/leetcode/cpp/0066.plus-one/CMakeLists.txt deleted file mode 100644 index 2bf7ec53..00000000 --- a/src/leetcode/cpp/0066.plus-one/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0066-plus-one - main.c - ) diff --git a/src/leetcode/cpp/0066.plus-one/main.c b/src/leetcode/cpp/0066.plus-one/main.c deleted file mode 100644 index f05b9bf0..00000000 --- a/src/leetcode/cpp/0066.plus-one/main.c +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -int* plusOne(int* digits, int digitsSize, int* returnSize) { - int carry = 1; - int new_val = 0; - for (int i = digitsSize - 1; i >= 0; --i) { - new_val = carry + digits[i]; - digits[i] = new_val % 10; - carry = new_val / 10; - } - - int* out_digits; - if (carry != 0) { - *returnSize = digitsSize + 1; - out_digits = (int*)malloc(*returnSize * sizeof(int)); - assert(out_digits); - memcpy(out_digits + 1, digits, digitsSize * sizeof(int)); - out_digits[0] = carry; - } else { - *returnSize = digitsSize; - out_digits = (int*)malloc(*returnSize * sizeof(int)); - assert(out_digits); - memcpy(out_digits, digits, digitsSize * sizeof(int)); - } - return out_digits; -} - -void checkSolution(int* digits, int digitsSize, int expectedReturnSize) { - int returnSize; - int* out_digits = plusOne(digits, digitsSize, &returnSize); - assert(returnSize == expectedReturnSize); - for (int i = 0; i < returnSize; ++i) { - printf("%d, ", out_digits[i]); - } - printf("\n"); -} - -int main() { - int arr1[] = {1, 2, 3}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 3); - - int arr2[] = {4, 3, 2, 1}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 4); - - int arr3[] = {9}; - checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 2); - - return 0; -} diff --git a/src/leetcode/cpp/0069.sqrtx/CMakeLists.txt b/src/leetcode/cpp/0069.sqrtx/CMakeLists.txt deleted file mode 100644 index 95ec61c0..00000000 --- a/src/leetcode/cpp/0069.sqrtx/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0069-sqrtx - main.cpp - ) diff --git a/src/leetcode/cpp/0069.sqrtx/main.cpp b/src/leetcode/cpp/0069.sqrtx/main.cpp deleted file mode 100644 index a8a07151..00000000 --- a/src/leetcode/cpp/0069.sqrtx/main.cpp +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -class Solution { - public: - static int mySqrt1(int x) { - long x1 = x; - long high = x1; - long low = 0; - long middle = 0; - long ans = 0; - - while (low <= high) { - middle = low + (high - low) / 2.0; - long mid_sq = middle * middle; - - if (mid_sq > x1) { - high = middle - 1; - } else if (mid_sq < x1) { - ans = middle; - low = middle + 1; - } else { - return middle; - } - } - - return ans; - } - - static int mySqrt(int x) { - int high = x; - int low = 0; - int ans = 0; - - while (low <= high) { - int64_t middle = low + (high - low) / 2.0; - int64_t mid_sq = middle * middle; - - if (mid_sq > x) { - high = middle - 1; - } else { - ans = middle; - low = middle + 1; - } - } - - return ans; - } - - static int mySqrt3(int x) { - long x1 = x; - int high = x1; - long low = 0; - long middle = 0; - long ans = 0; - - while (low <= high) { - middle = low + (high - low) / 2.0; - long mid_sq = middle * middle; - - if (mid_sq > x1) { - high = middle - 1; - } else if (mid_sq < x1) { - ans = middle; - low = middle + 1; - } else { - return middle; - } - } - - return ans; - } -}; - - -void checkSolution() { - assert(Solution::mySqrt(36) == 6); - assert(Solution::mySqrt(4) == 2); - assert(Solution::mySqrt(10) == 3); - assert(Solution::mySqrt(8) == 2); - assert(Solution::mySqrt(9) == 3); - assert(Solution::mySqrt(0) == 0); - assert(Solution::mySqrt(INT_MAX) == 46340); - assert(Solution::mySqrt(1) == 1); - assert(Solution::mySqrt(2) == 1); - assert(Solution::mySqrt(3) == 1); -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0070.climbing-stairs/CMakeLists.txt b/src/leetcode/cpp/0070.climbing-stairs/CMakeLists.txt deleted file mode 100644 index 8d9aa2e4..00000000 --- a/src/leetcode/cpp/0070.climbing-stairs/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0070-climbing-stairs - main.c - ) diff --git a/src/leetcode/cpp/0070.climbing-stairs/main.c b/src/leetcode/cpp/0070.climbing-stairs/main.c deleted file mode 100644 index 5c967b80..00000000 --- a/src/leetcode/cpp/0070.climbing-stairs/main.c +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -int climbStairs(int n) { - if (n == 0 || n == 1) { - return 1; - } - return climbStairs(n - 1) + climbStairs(n - 2); -} - -int climbStairs2(int n) { - if (n == 0 || n == 1) { - return 1; - } - int f1 = 1; - int f2 = 1; - int f3 = f1 + f2; - for (int i = 2; i <= n; ++i) { - f3 = f1 + f2; - f1 = f2; - f2 = f3; - } - return f3; -} - -void checkSolution(int n, int expected) { - int out = climbStairs2(n); - printf("out: %d, exp: %d\n", out, expected); - assert(out == expected); -} - -int main() { - checkSolution(2, 2); - checkSolution(3, 3); - return 0; -} diff --git a/src/leetcode/cpp/0075.sort-colors/CMakeLists.txt b/src/leetcode/cpp/0075.sort-colors/CMakeLists.txt deleted file mode 100644 index fdfa9101..00000000 --- a/src/leetcode/cpp/0075.sort-colors/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0075-sort-colors - main.cpp - ) diff --git a/src/leetcode/cpp/0075.sort-colors/main.cpp b/src/leetcode/cpp/0075.sort-colors/main.cpp deleted file mode 100644 index 2d817bb4..00000000 --- a/src/leetcode/cpp/0075.sort-colors/main.cpp +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include - -class Solution { - public: - static void sortColors(std::vector& nums) { - int low = 0; - int mid = 0; - int high = nums.size() - 1; - - while (mid <= high) { - if (nums[mid] == 0) { - std::swap(nums[low], nums[mid]); - low += 1; - mid += 1; - } else if (nums[mid] == 2) { - std::swap(nums[mid], nums[high]); - high -= 1; - } else { - mid += 1; - } - } - } - -}; - - -void checkSolution() { - //std::vector vec = {2,0,2,1,1,0}; - //std::vector vec = {2,0,1}; - std::vector vec = {1, 2, 0}; - Solution::sortColors(vec); - for (int i : vec) { - printf("%d, ", i); - } - printf("\n"); -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/CMakeLists.txt b/src/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/CMakeLists.txt deleted file mode 100644 index 69000923..00000000 --- a/src/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0082-remove-duplicates-from-sorted-list-ii - main.c - ) diff --git a/src/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/main.c b/src/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/main.c deleted file mode 100644 index 557cd1e3..00000000 --- a/src/leetcode/cpp/0082.remove-duplicates-from-sorted-list-ii/main.c +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -static struct ListNode* listNew(int val) { - struct ListNode* list = malloc(sizeof(struct ListNode)); - assert(list != NULL); - list->val = val; - list->next = NULL; - return list; -} - -static struct ListNode* listFromArray(int arr[], int len) { - assert(len >= 0); - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -static void listFree(struct ListNode* list) { - while (list != NULL) { - struct ListNode* tmp = list; - list = list->next; - free(tmp); - } -} - -static struct ListNode* deleteDuplicates(struct ListNode* head) { - if (head == NULL) { - return NULL; - } - - struct ListNode* current = head; - struct ListNode* prev = NULL; - struct ListNode* duplicated_node = NULL; - - while (current != NULL) { - if (current->next != NULL && current->next->val == current->val) { - // Remove current node. - if (head == current) { - head = current->next; - } - if (prev != NULL) { - prev->next = current->next->next; - } - duplicated_node = current->next; - free(current); - current = duplicated_node; - // Keep prev position unchanged. - } else if (duplicated_node != NULL) { - // Remove last duplicated node. - assert(current == duplicated_node); - if (head == current) { - head = current->next; - } - current = current->next; - free(duplicated_node); - duplicated_node = NULL; - } else { - // Go to next node. - prev = current; - current = current->next; - } - } - - return head; -} - -static void checkSolution(int arr[], int len) { - struct ListNode* list = listFromArray(arr, len); - struct ListNode* list_uniq = deleteDuplicates(list); - while (list_uniq != NULL) { - printf("%d, ", list_uniq->val); - list_uniq = list_uniq->next; - } - printf("\n"); - listFree(list_uniq); -} - -int main() { - // {1, 2, 5} - int arr1[] = {1, 2, 3, 3, 4, 4, 5}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0])); - - // {2, 3} - int arr2[] = {1, 1, 1, 2, 3}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0])); - - return 0; -} diff --git a/src/leetcode/cpp/0083.remove-duplicates-from-sorted-list/CMakeLists.txt b/src/leetcode/cpp/0083.remove-duplicates-from-sorted-list/CMakeLists.txt deleted file mode 100644 index 3c6697e0..00000000 --- a/src/leetcode/cpp/0083.remove-duplicates-from-sorted-list/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0083-remove-duplicates-from-sorted-list - main.c - ) diff --git a/src/leetcode/cpp/0083.remove-duplicates-from-sorted-list/main.c b/src/leetcode/cpp/0083.remove-duplicates-from-sorted-list/main.c deleted file mode 100644 index 2107dc44..00000000 --- a/src/leetcode/cpp/0083.remove-duplicates-from-sorted-list/main.c +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -static struct ListNode* listNew(int val) { - struct ListNode* list = malloc(sizeof(struct ListNode)); - assert(list != NULL); - list->val = val; - list->next = NULL; - return list; -} - -static void listFree(struct ListNode* list) { - while (list != NULL) { - struct ListNode* tmp = list; - list = list->next; - free(tmp); - } -} - -static struct ListNode* listFromArray(int arr[], int len) { - assert(len >= 0); - struct ListNode* list; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -static struct ListNode* deleteDuplicates(struct ListNode* head) { - if (head == NULL) { - return NULL; - } - - struct ListNode* current = head; - struct ListNode* previous = NULL; - while (current != NULL) { - if (previous != NULL && previous->val == current->val) { - // Remove current node. - previous->next = current->next; - struct ListNode* tmp = current; - current = current->next; - free(tmp); - } else { - previous = current; - current = current->next; - } - } - - return head; -} - -static void checkSolution(int arr[], int len) { - struct ListNode* list = listFromArray(arr, len); - struct ListNode* list_uniq = deleteDuplicates(list); - while (list_uniq != NULL) { - printf("%d, ", list_uniq->val); - list_uniq = list_uniq->next; - } - printf("\n"); - - listFree(list_uniq); -} - -int main() { - int arr1[] = {1, 1, 2}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0])); - - int arr2[] = {1, 1, 2, 3, 3}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0])); - return 0; -} diff --git a/src/leetcode/cpp/0086.partition-list/CMakeLists.txt b/src/leetcode/cpp/0086.partition-list/CMakeLists.txt deleted file mode 100644 index 93409609..00000000 --- a/src/leetcode/cpp/0086.partition-list/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0086-partition-list - main.c - ) diff --git a/src/leetcode/cpp/0086.partition-list/main.c b/src/leetcode/cpp/0086.partition-list/main.c deleted file mode 100644 index 1f959232..00000000 --- a/src/leetcode/cpp/0086.partition-list/main.c +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -static struct ListNode* listNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -static struct ListNode* listFromArray(int arr[], int len) { - assert(len >= 0); - struct ListNode* head = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = head; - head = node; - } - - return head; -} - -static void listFree(struct ListNode* head) { - while (head != NULL) { - struct ListNode* tmp = head; - head = head->next; - free(tmp); - } -} - -static struct ListNode* partition(struct ListNode* head, int x) { - if (head == NULL) { - return NULL; - } - - struct ListNode* small_head = NULL; - struct ListNode* small_tail = NULL; - struct ListNode* large_head = NULL; - struct ListNode* large_tail = NULL; - struct ListNode* tmp = NULL; - while (head != NULL) { - tmp = head; - if (tmp->val < x) { - if (small_tail == NULL) { - small_head = tmp; - small_tail = tmp; - } else { - small_tail->next = tmp; - small_tail = tmp; - } - } else { - if (large_tail == NULL) { - large_head = tmp; - large_tail = tmp; - } else { - large_tail->next = tmp; - large_tail = tmp; - } - } - - // Go to next node. - head = head->next; - } - - if (small_tail == NULL) { - return large_head; - } - // Connect two parts. - small_tail->next = large_head; - if (large_tail != NULL) { - large_tail->next = NULL; - } - - return small_head; -} - -static void checkSolution(int arr[], int len, int x) { - struct ListNode* list = listFromArray(arr, len); - struct ListNode* list2 = partition(list, x); - list = list2; - - while (list2 != NULL) { - printf("%d, ", list2->val); - list2 = list2->next; - } - printf("\n"); - - listFree(list); -} - -int main() { - int arr1[] = {1,4,3,2,5,2}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 3); - - int arr2[] = {2, 1}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 2); - - return 0; -} diff --git a/src/leetcode/cpp/0092.reverse-linked-list-ii/CMakeLists.txt b/src/leetcode/cpp/0092.reverse-linked-list-ii/CMakeLists.txt deleted file mode 100644 index 4d5f7f6d..00000000 --- a/src/leetcode/cpp/0092.reverse-linked-list-ii/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0092-reverse-linked-list-ii - main.c - ) diff --git a/src/leetcode/cpp/0092.reverse-linked-list-ii/main.c b/src/leetcode/cpp/0092.reverse-linked-list-ii/main.c deleted file mode 100644 index dcfc9998..00000000 --- a/src/leetcode/cpp/0092.reverse-linked-list-ii/main.c +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -static struct ListNode* listNew(int val) { - struct ListNode* list = malloc(sizeof(struct ListNode)); - assert(list != NULL); - list->val = val; - list->next = NULL; - return list; -} - -static struct ListNode* listFromArray(int arr[], int len) { - assert(len >= 0); - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -static void listFree(struct ListNode* list) { - assert(list != NULL); - while (list != NULL) { - struct ListNode* node = list; - list = list->next; - free(node); - } -} - -static struct ListNode* reverseBetween(struct ListNode* head, int left, int right) { - assert(head != NULL); - assert(left >= 1 && left <= right); - if (left == right) { - return head; - } - left -= 1; - right -= 1; - - struct ListNode* range_prev = NULL; - struct ListNode* range_start = NULL; - struct ListNode* range_end = NULL; - struct ListNode* curr = head; - struct ListNode* prev = NULL; - struct ListNode* next = NULL; - - int index = 0; - while (index < left) { - range_prev = curr; - curr = curr->next; - index += 1; - } - - range_start = curr; - - while (index < right) { - // switch direction - next = curr->next; - curr->next = prev; - prev = curr; - curr = next; - index += 1; - } - range_end = curr; - if (range_prev == NULL) { - head = range_end; - } else { - range_prev->next = range_end; - } - range_start->next = range_end->next; - range_end->next = prev; - - return head; -} - -static void checkSolution(int arr[], int len, int left, int right) { - struct ListNode* list = listFromArray(arr, len); - struct ListNode* reversed = reverseBetween(list, left, right); - list = reversed; - while (reversed != NULL) { - printf("%d, ", reversed->val); - reversed = reversed->next; - } - printf("\n"); - listFree(list); -} - -int main() { - int arr1[] = {1, 2, 3, 4, 5}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0]), 2, 4); - - int arr2[] = {5}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0]), 1, 1); - - int arr3[] = {3, 5}; - checkSolution(arr3, sizeof(arr3) / sizeof(arr3[0]), 1, 2); - - return 0; -} diff --git a/src/leetcode/cpp/0093.restore-ip-addresses/CMakeLists.txt b/src/leetcode/cpp/0093.restore-ip-addresses/CMakeLists.txt deleted file mode 100644 index f240a137..00000000 --- a/src/leetcode/cpp/0093.restore-ip-addresses/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0093-restore-ip-addresses - main.cpp - ) diff --git a/src/leetcode/cpp/0093.restore-ip-addresses/main.cpp b/src/leetcode/cpp/0093.restore-ip-addresses/main.cpp deleted file mode 100644 index fa5a648b..00000000 --- a/src/leetcode/cpp/0093.restore-ip-addresses/main.cpp +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include -#include - -class Solution { - public: - static bool isValidIpSector(const std::string& s) { - if (s.empty()) { - return false; - } - if (s.size() > 3) { - return false; - } - // Leading zero - if (s.size() > 1 && s[0] == '0') { - return false; - } - int val = std::stoi(s); - return val >=0 && val <= 255; - } - - static - std::vector restoreIpAddresses(std::string s) { - std::vector out; - for (int i = 1; i <= 3 && i < s.size(); ++i) { - const std::string& first = s.substr(0, i); - if (!isValidIpSector(first)) { - continue; - } - for (int j = 1; j <= 3 && i + j < s.size(); ++j) { - const std::string& second = s.substr(i, j); - if (!isValidIpSector(second)) { - continue; - } - - for (int k = 1; k <= 3 && i + j + k < s.size(); ++k) { - const std::string& third = s.substr(i + j, k); - const std::string& forth = s.substr(i + j + k); - if (isValidIpSector(third) && isValidIpSector(forth)) { - out.emplace_back(first + "." + second + "." + third + "." + forth); - } - } - } - } - return out; - } -}; - -void checkSolution(std::string s, std::vector exp_out) { - std::vector out = Solution::restoreIpAddresses(s); - printf("size: %d, exp: %d\n", out.size(), exp_out.size()); - assert(out.size() == exp_out.size()); - std::sort(out.begin(), out.end()); - std::sort(exp_out.begin(), exp_out.end()); - for (int i = 0; i < out.size(); ++i) { - assert(out[i] == exp_out[i]); - } -} - -int main() { - checkSolution("25525511135", {"255.255.11.135", "255.255.111.35"}); - checkSolution("101023", { - "1.0.10.23", - "1.0.102.3", - "10.1.0.23", - "10.10.2.3", - "101.0.2.3" - }); - - return 0; -} diff --git a/src/leetcode/cpp/0098.validate-binary-search-tree/CMakeLists.txt b/src/leetcode/cpp/0098.validate-binary-search-tree/CMakeLists.txt deleted file mode 100644 index d1c6de36..00000000 --- a/src/leetcode/cpp/0098.validate-binary-search-tree/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0098-validate-binary-search-tree - main.c - ) diff --git a/src/leetcode/cpp/0098.validate-binary-search-tree/main.c b/src/leetcode/cpp/0098.validate-binary-search-tree/main.c deleted file mode 100644 index 7253fb2d..00000000 --- a/src/leetcode/cpp/0098.validate-binary-search-tree/main.c +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include -#include - -struct TreeNode { - int val; - struct TreeNode* left; - struct TreeNode* right; -}; - -struct TreeNode* findMin(struct TreeNode* root) { - if (root == NULL) { - return NULL; - } - // traverse - struct TreeNode* left = findMin(root->left); - struct TreeNode* right = findMin(root->left); - struct TreeNode* min_node = root; - if (left != NULL && left->val < min_node->val) { - min_node = left; - } - if (right != NULL && right->val < min_node->val) { - min_node = right; - } - return min_node; -} - -struct TreeNode* findMax(struct TreeNode* root) { - if (root == NULL) { - return NULL; - } - struct TreeNode* left = findMax(root->left); - struct TreeNode* right = findMax(root->left); - struct TreeNode* max_node = root; - if (left != NULL && left->val > max_node->val) { - max_node = left; - } - if (right != NULL && right->val > max_node->val) { - max_node = right; - } - return max_node; -} - -bool isValidBST(struct TreeNode* root) { - if (root == NULL) { - return true; - } - struct TreeNode* left_max = findMax(root->left); - if (left_max != NULL && left_max->val >= root->val) { - return false; - } - struct TreeNode* right_min = findMin(root->right); - if (right_min != NULL && right_min->val <= root->val) { - return false; - } - return true; -} - -bool isValidBST2Helper(struct TreeNode* root, long min, long max) { - if (root == NULL) { - return true; - } - if (root->val < max && root->val > min) { - return isValidBST2Helper(root->left, min, root->val) && - isValidBST2Helper(root->right, root->val, max); - } - return false; -} - -bool isValidBST2(struct TreeNode* root) { - return isValidBST2Helper(root, LONG_MIN, LONG_MAX); -} - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0101.symmetric-tree/CMakeLists.txt b/src/leetcode/cpp/0101.symmetric-tree/CMakeLists.txt deleted file mode 100644 index de99cb81..00000000 --- a/src/leetcode/cpp/0101.symmetric-tree/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0101-symmetric-tree - main.c - ) diff --git a/src/leetcode/cpp/0101.symmetric-tree/main.c b/src/leetcode/cpp/0101.symmetric-tree/main.c deleted file mode 100644 index 6aea52c1..00000000 --- a/src/leetcode/cpp/0101.symmetric-tree/main.c +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -struct TreeNode { - int val; - struct TreeNode* left; - struct TreeNode* right; -}; - -bool isSymmetricHelper(struct TreeNode* left, struct TreeNode* right) { - if (left == NULL && right == NULL) { - return true; - } - if (left == NULL || right == NULL) { - return false; - } - if (left->val != right->val) { - return false; - } - return isSymmetricHelper(left->left, right->right) && - isSymmetricHelper(left->right, right->left); -} - -bool isSymmetric(struct TreeNode* root) { - return isSymmetricHelper(root->left, root->right); -} - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0102.binary-tree-level-order-traversal/CMakeLists.txt b/src/leetcode/cpp/0102.binary-tree-level-order-traversal/CMakeLists.txt deleted file mode 100644 index e7ae4972..00000000 --- a/src/leetcode/cpp/0102.binary-tree-level-order-traversal/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0102-binary-tree-level-order-traversal - main.cpp - ) diff --git a/src/leetcode/cpp/0102.binary-tree-level-order-traversal/main.cpp b/src/leetcode/cpp/0102.binary-tree-level-order-traversal/main.cpp deleted file mode 100644 index 95dc62e0..00000000 --- a/src/leetcode/cpp/0102.binary-tree-level-order-traversal/main.cpp +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include - -#include -#include - -struct TreeNode { - int val; - struct TreeNode* left; - struct TreeNode* right; -}; - -class Solution { - public: - void levelOrderHelper(std::queue queue, std::vector>& out) { - std::queue next_queue; - std::vector values; - - while (!queue.empty()) { - TreeNode* node = queue.front(); - queue.pop(); - values.push_back(node->val); - if (node->left != nullptr) { - next_queue.push(node->left); - } - if (node->right != nullptr) { - next_queue.push(node->right); - } - } - out.push_back(values); - - if (!next_queue.empty()) { - levelOrderHelper(next_queue, out); - } - } - - std::vector> levelOrder(TreeNode* root) { - std::vector> out; - if (root == nullptr) { - return out; - } - - std::queue queue; - queue.push(root); - levelOrderHelper(queue, out); - - return out; - } - - std::vector> levelOrderIterative(TreeNode* root) { - std::vector> out; - if (root == nullptr) { - return out; - } - - std::queue queue; - queue.push(root); - - while (!queue.empty()) { - size_t size = queue.size(); - std::vector values; - // current level. - for (size_t i = 0; i < size; ++i) { - TreeNode* node = queue.front(); - queue.pop(); - values.push_back(node->val); - if (node->left != nullptr) { - queue.push(node->left); - } - if (node->right != nullptr) { - queue.push(node->right); - } - } - out.push_back(values); - } - - return out; - } -}; - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/CMakeLists.txt b/src/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/CMakeLists.txt deleted file mode 100644 index 664fb798..00000000 --- a/src/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0103-binary-tree-zigzag-level-order-traversal - main.cpp - ) diff --git a/src/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/main.cpp b/src/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/main.cpp deleted file mode 100644 index b3cd6070..00000000 --- a/src/leetcode/cpp/0103.binary-tree-zigzag-level-order-traversal/main.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct TreeNode { - int val; - struct TreeNode* left; - struct TreeNode* right; -}; - -class Solution { - public: - std::vector> zigzagLevelOrder(TreeNode* root) { - std::vector> out; - if (root == nullptr) { - return out; - } - - std::queue queue; - queue.push(root); - bool do_reverse = false; - - while (!queue.empty()) { - size_t size = queue.size(); - std::vector values; - // current level. - for (size_t i = 0; i < size; ++i) { - TreeNode* node = queue.front(); - queue.pop(); - values.push_back(node->val); - if (node->left != nullptr) { - queue.push(node->left); - } - if (node->right != nullptr) { - queue.push(node->right); - } - } - - if (do_reverse) { - std::reverse(values.begin(), values.end()); - } - out.push_back(values); - do_reverse = !do_reverse; - } - - return out; - } -}; - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/CMakeLists.txt b/src/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/CMakeLists.txt deleted file mode 100644 index c96235ac..00000000 --- a/src/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0107-binary-tree-level-order-traversal-ii - main.cpp - ) diff --git a/src/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/main.cpp b/src/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/main.cpp deleted file mode 100644 index e7e76cbe..00000000 --- a/src/leetcode/cpp/0107.binary-tree-level-order-traversal-ii/main.cpp +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct TreeNode { - int val; - struct TreeNode* left; - struct TreeNode* right; -}; - -class Solution { - public: - std::vector> levelOrderBottom(TreeNode* root) { - std::vector> out; - if (root == nullptr) { - return out; - } - - std::queue queue; - queue.push(root); - while (!queue.empty()) { - - // current level - size_t size = queue.size(); - std::vector values; - for (size_t i = 0; i < size; ++i) { - TreeNode* node = queue.front(); - queue.pop(); - values.push_back(node->val); - if (node->left != nullptr) { - queue.push(node->left); - } - if (node->right != nullptr) { - queue.push(node->right); - } - } - out.push_back(values); - } - - std::reverse(out.begin(), out.end()); - return out; - } -}; - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/CMakeLists.txt b/src/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/CMakeLists.txt deleted file mode 100644 index 1eedfb5c..00000000 --- a/src/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0108-convert-sorted-array-to-binary-search-tree - main.c - ) diff --git a/src/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/main.c b/src/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/main.c deleted file mode 100644 index 48f37e01..00000000 --- a/src/leetcode/cpp/0108.convert-sorted-array-to-binary-search-tree/main.c +++ /dev/null @@ -1,18 +0,0 @@ - -#include -#include -#include - -struct TreeNode { - int val; - struct TreeNode* left; - struct TreeNode* right; -}; - -struct TreeNode* sortedArrayToBST(int* nums, int numsSize) { - -} - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/CMakeLists.txt b/src/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/CMakeLists.txt deleted file mode 100644 index 239d5bed..00000000 --- a/src/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0114-flatten-binary-tree-to-linked-list - main.c - ) diff --git a/src/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/main.c b/src/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/main.c deleted file mode 100644 index 1a40e101..00000000 --- a/src/leetcode/cpp/0114.flatten-binary-tree-to-linked-list/main.c +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -struct TreeNode { - int val; - struct TreeNode* left; - struct TreeNode* right; -}; - -static struct TreeNode* treeNew(int val) { - struct TreeNode* node = malloc(sizeof(struct TreeNode)); - assert(node != NULL); - node->val = val; - node->left = NULL; - node->right = NULL; - return node; -} - -static struct TreeNode* treeFromArrayHelper(int arr[], int len, int index) { - if (index >= len) { - return NULL; - } - - if (arr[index] == INT_MIN) { - return NULL; - } - - struct TreeNode* node = treeNew(arr[index]); - node->left = treeFromArrayHelper(arr, len, index * 2 + 1); - node->right = treeFromArrayHelper(arr, len, index * 2 + 2); - return node; -} - -static struct TreeNode* treeFromArray(int arr[], int len) { - assert(len >= 0); - struct TreeNode* root = treeFromArrayHelper(arr, len, 0); - - return root; -} - -static void treeFree(struct TreeNode* root) { - if (root != NULL) { - treeFree(root->left); - treeFree(root->right); - free(root); - } -} - -static void treeTraversePreOrder( - struct TreeNode* node, - void (*apply)(struct TreeNode* node, void* user_data), - void* user_data) { - if (node != NULL) { - apply(node, user_data); - treeTraversePreOrder(node->left, apply, user_data); - treeTraversePreOrder(node->right, apply, user_data); - } -} - -static void debugTreeNodeHelper(struct TreeNode* node) { - if (node != NULL) { - printf("%d, ", node->val); - debugTreeNodeHelper(node->left); - debugTreeNodeHelper(node->right); - } else { - printf("null, "); - } -} - -static void debugTreeNode(struct TreeNode* root) { - assert(root != NULL); - debugTreeNodeHelper(root); - printf("\n"); -} - -static struct TreeNode* treeGetRightLeaf(struct TreeNode* root) { - if (root == NULL) { - return NULL; - } - while (root != NULL) { - if (root->right == NULL) { - return root; - } - root = root->right; - } - return NULL; -} - -static void flatten(struct TreeNode* root) { - while (root != NULL) { - if (root->left != NULL) { - struct TreeNode* right_leaf = treeGetRightLeaf(root->left); - right_leaf->right = root->right; - root->right = root->left; - root->left = NULL; - } - root = root->right; - } -} - -static void flatten2(struct TreeNode* root) { - while (root != NULL) { - if (root->left != NULL) { - struct TreeNode* right_leaf = root->left; - while (right_leaf->right != NULL) { - right_leaf = right_leaf->right; - } - - right_leaf->right = root->right; - root->right = root->left; - root->left = NULL; - } - root = root->right; - } -} - -static void checkSolution(int arr[], int len) { - struct TreeNode* root = treeFromArray(arr, len); - flatten(root); - //flatten2(root); - debugTreeNode(root); - - treeFree(root); -} - -int main() { - int arr[] = {1, 2, 5, 3, 4, INT_MIN, 6}; - //int arr[] = {1, 2, INT_MIN, 3}; - checkSolution(arr, sizeof(arr) / sizeof(arr[0])); - - return 0; -} diff --git a/src/leetcode/cpp/0137.single-number-ii/CMakeLists.txt b/src/leetcode/cpp/0137.single-number-ii/CMakeLists.txt deleted file mode 100644 index ff72844f..00000000 --- a/src/leetcode/cpp/0137.single-number-ii/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0137-single-number-ii - main.cpp - ) diff --git a/src/leetcode/cpp/0137.single-number-ii/main.cpp b/src/leetcode/cpp/0137.single-number-ii/main.cpp deleted file mode 100644 index 5292246d..00000000 --- a/src/leetcode/cpp/0137.single-number-ii/main.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include - -class Solution { - public: - static int singleNumber(std::vector& nums) { - std::unordered_map cache; - cache.reserve(nums.size() / 3 + 1); - for (int num: nums) { - cache[num] += 1; - } - for (auto pair: cache) { - if (pair.second == 1) { - return pair.first; - } - } - return 0; - } -}; - - -void checkSolution() { - { - std::vector nums = {2, 2, 3, 2}; - assert(Solution::singleNumber(nums) == 3); - } - - { - std::vector nums = {0,1,0,1,0,1,99}; - assert(Solution::singleNumber(nums) == 99); - } -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0139.word-break/CMakeLists.txt b/src/leetcode/cpp/0139.word-break/CMakeLists.txt deleted file mode 100644 index 307bf7e0..00000000 --- a/src/leetcode/cpp/0139.word-break/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0139-word-break - main.cpp - ) diff --git a/src/leetcode/cpp/0139.word-break/main.cpp b/src/leetcode/cpp/0139.word-break/main.cpp deleted file mode 100644 index 04ef8ca3..00000000 --- a/src/leetcode/cpp/0139.word-break/main.cpp +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -class Solution { - public: - static - bool wordBreak(std::string s, std::vector& wordDict) { - } -}; - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0141.linked-list-cycle/CMakeLists.txt b/src/leetcode/cpp/0141.linked-list-cycle/CMakeLists.txt deleted file mode 100644 index a69de6ce..00000000 --- a/src/leetcode/cpp/0141.linked-list-cycle/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0141-linked-list-cycle - main.c - ) diff --git a/src/leetcode/cpp/0141.linked-list-cycle/main.c b/src/leetcode/cpp/0141.linked-list-cycle/main.c deleted file mode 100644 index 995d4875..00000000 --- a/src/leetcode/cpp/0141.linked-list-cycle/main.c +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -struct ListNode* listNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -struct ListNode* listConnectTailTo(struct ListNode* head, int pos) { - assert(head != NULL); - assert(pos > 0); - - struct ListNode* curr = head; - struct ListNode* pos_node = NULL; - while (pos > 0) { - curr = curr->next; - pos -= 1; - } - assert(curr != NULL); - pos_node = curr; - while (curr->next != NULL) { - curr = curr->next; - } - curr->next = pos_node; - - return head; -} - -void listFree(struct ListNode* list) { - while (list != NULL) { - struct ListNode* tmp = list; - list = list->next; - free(tmp); - } -} - -bool hasCycle(struct ListNode *head) { - struct ListNode* alert_node = listNew(INT_MIN); - struct ListNode* tmp = NULL; - while (head != NULL) { - if (head->next == alert_node) { - return true; - } - tmp = head; - head = head->next; - tmp->next = alert_node; - } - return false; -} - -bool hasCycle2(struct ListNode *head) { - const int kResetVal = INT_MIN; - while (head != NULL) { - if (head->val == kResetVal) { - return true; - } - head->val = kResetVal; - head = head->next; - } - return false; -} - -// Floyd’s Cycle-Finding Algorithm -bool hasCycle3(struct ListNode *head) { - struct ListNode* fast = head; - struct ListNode* slow = head; - - while (fast != NULL && fast->next != NULL) { - // 2 nodes forward - fast = fast->next->next; - // 1 node forward - slow = slow->next; - - // Got the same node. - if (fast == slow) { - return true; - } - } - return false; -} - -void checkSolution(int arr[], int len, int pos) { - struct ListNode* list = listFromArray(arr, len); - struct ListNode* list2 = listConnectTailTo(list, pos); - bool ret = hasCycle(list2); - printf("has cycle: %d\n", ret); -} - -int main() { - int arr[] = {3, 2, 0, -4}; - checkSolution(arr, sizeof(arr) / sizeof(arr[0]), 1); - - return 0; -} diff --git a/src/leetcode/cpp/0142.linked-list-cycle-ii/CMakeLists.txt b/src/leetcode/cpp/0142.linked-list-cycle-ii/CMakeLists.txt deleted file mode 100644 index 02026e98..00000000 --- a/src/leetcode/cpp/0142.linked-list-cycle-ii/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0142-linked-list-cycle-ii - main.c - ) diff --git a/src/leetcode/cpp/0142.linked-list-cycle-ii/main.c b/src/leetcode/cpp/0142.linked-list-cycle-ii/main.c deleted file mode 100644 index f65b8b55..00000000 --- a/src/leetcode/cpp/0142.linked-list-cycle-ii/main.c +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -struct ListNode* listNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -struct ListNode* listConnectTailTo(struct ListNode* head, int pos) { - assert(head != NULL); - assert(pos > 0); - - struct ListNode* curr = head; - struct ListNode* pos_node = NULL; - while (pos > 0) { - curr = curr->next; - pos -= 1; - } - assert(curr != NULL); - pos_node = curr; - while (curr->next != NULL) { - curr = curr->next; - } - curr->next = pos_node; - - return head; -} - -void listFree(struct ListNode* list) { - while (list != NULL) { - struct ListNode* tmp = list; - list = list->next; - free(tmp); - } -} - -// Floyd’s Cycle-Finding Algorithm -struct ListNode* detectCycle(struct ListNode* head) { - struct ListNode* fast = head; - struct ListNode* slow = head; - while (fast != NULL && fast->next != NULL) { - // 2 nodes forward - fast = fast->next->next; - // 1 node forward - slow = slow->next; - - if (fast == slow) { - // Found cycle. - - // Reset slow to head of list. - slow = head; - while (slow != fast) { - // Wait until they meat again. - slow = slow->next; - fast = fast->next; - } - return slow; - } - } - return NULL; -} - -void checkSolution(int arr[], int len, int pos) { - struct ListNode* list = listFromArray(arr, len); - struct ListNode* list2 = listConnectTailTo(list, pos); - struct ListNode* cycle_list = detectCycle(list2); - while (cycle_list != NULL) { - printf("%d, ", cycle_list->val); - cycle_list = cycle_list->next; - } -} - -int main() { - int arr[] = {3, 2, 0, -4}; - checkSolution(arr, sizeof(arr) / sizeof(arr[0]), 1); - - return 0; -} diff --git a/src/leetcode/cpp/0144.binary-tree-preorder-traversal/CMakeLists.txt b/src/leetcode/cpp/0144.binary-tree-preorder-traversal/CMakeLists.txt deleted file mode 100644 index b6338df7..00000000 --- a/src/leetcode/cpp/0144.binary-tree-preorder-traversal/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0144-binary-tree-preorder-traversal - main.cpp - ) diff --git a/src/leetcode/cpp/0144.binary-tree-preorder-traversal/main.cpp b/src/leetcode/cpp/0144.binary-tree-preorder-traversal/main.cpp deleted file mode 100644 index d61f99ff..00000000 --- a/src/leetcode/cpp/0144.binary-tree-preorder-traversal/main.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include - -struct TreeNode { - int val; - struct TreeNode* left; - struct TreeNode* right; -}; - -class Solution { -public: - static - void helper(TreeNode* root, std::vector& values) { - if (root == nullptr) { - return; - } - values.push_back(root->val); - helper(root->left, values); - helper(root->right, values); - } - - std::vector preorderTraversal(TreeNode* root) { - std::vector values; - helper(root, values); - return values; - } -}; - - -int main() { - - return 0; -} diff --git a/src/leetcode/cpp/0146.lru-cache/CMakeLists.txt b/src/leetcode/cpp/0146.lru-cache/CMakeLists.txt deleted file mode 100644 index e31f6970..00000000 --- a/src/leetcode/cpp/0146.lru-cache/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0146-lru-cache - main.cpp - ) diff --git a/src/leetcode/cpp/0146.lru-cache/main.cpp b/src/leetcode/cpp/0146.lru-cache/main.cpp deleted file mode 100644 index 3763f64b..00000000 --- a/src/leetcode/cpp/0146.lru-cache/main.cpp +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include -#include - -class LRUCache { - public: - explicit LRUCache(int capacity) : capacity_(capacity) { - iter_map_.reserve(capacity); - } - ~LRUCache() { - iter_map_.clear(); - items_.clear(); - } - - int get(int key) { - const auto iter = iter_map_.find(key); - if (iter == iter_map_.end()) { - return -1; - } - - // found, change order in list if needed. - const int value = iter->second->second; - if (iter->second != items_.begin()) { - items_.erase(iter->second); - const auto new_iter = items_.emplace(items_.begin(), std::make_pair(key, value)); - iter_map_.emplace(key, new_iter); - } - return value; - } - - void put(int key, int value) { - printf("put(%d, %d)\n", key, value); - auto old_iter = iter_map_.find(key); - if (old_iter == iter_map_.end()) { - // not found, check capacity. - if (iter_map_.size() >= capacity_) { - const int front_key = items_.back().first; - items_.pop_back(); - iter_map_.erase(front_key); - } - - // push to front of list. - const auto new_iter = items_.emplace(items_.begin(), std::make_pair(key, value)); - iter_map_.emplace(key, new_iter); - } else { - // found, update value - old_iter->second->second = value; - if (old_iter->second != items_.begin()) { - // change order in list if needed. - items_.erase(old_iter->second); - const auto new_iter = items_.emplace(items_.begin(), std::make_pair(key, value)); - iter_map_.emplace(key, new_iter); - } - } - } - - void debug_print() const { - printf("iters: "); - for (const auto& item: iter_map_) { - printf("%d, ", item.first); - } - printf("items: "); - for (const auto& item: items_) { - printf("(%d, %d), ", item.first, item.second); - } - printf("\n"); - } - - private: - using Items = std::list>; - using ItemIter = std::list>::iterator; - - int capacity_; - // (key, value) pair - Items items_; - // key -> list iterator - std::unordered_map iter_map_; -}; - -void checkSolution() { - LRUCache cache(2); - cache.put(1, 1); // cache is {1=1} - cache.debug_print(); - cache.put(2, 2); // cache is {1=1, 2=2} - cache.debug_print(); - assert(cache.get(1) == 1); // return 1 - cache.debug_print(); - cache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} - cache.debug_print(); - assert(cache.get(2) == -1); // returns -1 (not found) - cache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} - cache.debug_print(); - assert(cache.get(1) == -1); // return -1 (not found) - assert(cache.get(3) == 3); // return 3 - assert(cache.get(4) == 4); // return 4 -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0160.intersection-of-two-linked-lists/CMakeLists.txt b/src/leetcode/cpp/0160.intersection-of-two-linked-lists/CMakeLists.txt deleted file mode 100644 index 3c0ecea7..00000000 --- a/src/leetcode/cpp/0160.intersection-of-two-linked-lists/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0160-intersection-of-two-linked-lists - main.c - ) diff --git a/src/leetcode/cpp/0160.intersection-of-two-linked-lists/main.c b/src/leetcode/cpp/0160.intersection-of-two-linked-lists/main.c deleted file mode 100644 index 76b4440d..00000000 --- a/src/leetcode/cpp/0160.intersection-of-two-linked-lists/main.c +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -struct ListNode* listSkip(struct ListNode* head, int count) { - while (count > 0) { - head = head->next; - assert(head != NULL); - count -= 1; - } - return head; -} - -int listLen(struct ListNode* head) { - int count = 0; - while (head != NULL) { - head = head->next; - count += 1; - } - return count; -} - -struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) { - int len_a = listLen(headA); - int len_b = listLen(headB); - if (len_a > len_b) { - headA = listSkip(headA, len_a - len_b); - } else { - headB = listSkip(headB, len_b - len_a); - } - - while (headA != NULL) { - if (headA == headB) { - return headA; - } - headA = headA->next; - headB = headB->next; - } - return NULL; -} - -// Do not count length of list directly. -// Walk through two rounds instead. -struct ListNode* getIntersectionNode2(struct ListNode* headA, struct ListNode* headB) { - if (headA == NULL || headB == NULL) { - return NULL; - } - - struct ListNode* originHeadA = headA; - struct ListNode* originHeadB = headB; - bool is_a_end = false; - bool is_b_end = false; - - while (headA != headB) { - headA = headA->next; - headB = headB->next; - - if (headA == NULL && !is_a_end) { - headA = originHeadB; - is_a_end = true; - } - - if (headB == NULL && !is_b_end) { - headB = originHeadA; - is_b_end = true; - } - - if (headA == NULL || headB == NULL) { - return NULL; - } - } - return headA; -} - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0162.find-peak-element/CMakeLists.txt b/src/leetcode/cpp/0162.find-peak-element/CMakeLists.txt deleted file mode 100644 index 91626075..00000000 --- a/src/leetcode/cpp/0162.find-peak-element/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0162-find-peak-element - main.c - ) diff --git a/src/leetcode/cpp/0199.binary-tree-right-side-view/CMakeLists.txt b/src/leetcode/cpp/0199.binary-tree-right-side-view/CMakeLists.txt deleted file mode 100644 index 81c76663..00000000 --- a/src/leetcode/cpp/0199.binary-tree-right-side-view/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0199-binary-tree-right-side-view - main.c - ) diff --git a/src/leetcode/cpp/0200.number-of-islands/CMakeLists.txt b/src/leetcode/cpp/0200.number-of-islands/CMakeLists.txt deleted file mode 100644 index ed4310b9..00000000 --- a/src/leetcode/cpp/0200.number-of-islands/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0200-number-of-islands - main.c - ) diff --git a/src/leetcode/cpp/0203.remove-linked-list-elements/CMakeLists.txt b/src/leetcode/cpp/0203.remove-linked-list-elements/CMakeLists.txt deleted file mode 100644 index dd0bf066..00000000 --- a/src/leetcode/cpp/0203.remove-linked-list-elements/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0203.remove-linked-list-elements - main.c - ) diff --git a/src/leetcode/cpp/0203.remove-linked-list-elements/main.c b/src/leetcode/cpp/0203.remove-linked-list-elements/main.c deleted file mode 100644 index 801c5934..00000000 --- a/src/leetcode/cpp/0203.remove-linked-list-elements/main.c +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -struct ListNode* listNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -void listFree(struct ListNode* list) { - while (list != NULL) { - struct ListNode* tmp = list; - list = list->next; - free(tmp); - } -} - -struct ListNode* removeElements(struct ListNode* head, int val) { - if (head == NULL) { - return NULL; - } - - struct ListNode* curr = head; - struct ListNode* prev = NULL; - - while (curr != NULL) { - if (curr->val == val) { - // Remove current node. - if (prev == NULL) { - assert(curr == head); - curr = curr->next; - free(head); - head = curr; - } else { - prev->next = curr->next; - free(curr); - curr = prev->next; - } - } else { - prev = curr; - curr = curr->next; - } - } - return head; -} - -struct ListNode* removeElements2(struct ListNode* head, int val) { - if (head == NULL) { - return NULL; - } - - struct ListNode* curr = head->next; - struct ListNode* prev = head; - - while (curr != NULL) { - if (curr->val == val) { - // Remove current node. - prev->next = curr->next; - free(curr); - curr = prev->next; - } else { - prev = curr; - curr = curr->next; - } - } - if (head->val == val) { - curr = head; - head = head->next; - free(curr); - } - return head; -} - -void checkSolution(int arr[], int len, int val) { - struct ListNode* list = listFromArray(arr, len); - struct ListNode* list2 = removeElements2(list, val); - list = list2; - while (list != NULL) { - printf("%d, ", list->val); - list = list->next; - } - printf("\n"); - listFree(list2); -} - -int main() { - int arr[] = {1,2,6,3,4,5,6}; - checkSolution(arr, sizeof(arr) / sizeof(arr[0]), 6); - - return 0; -} diff --git a/src/leetcode/cpp/0206.reverse-linked-list/CMakeLists.txt b/src/leetcode/cpp/0206.reverse-linked-list/CMakeLists.txt deleted file mode 100644 index 6affc6ab..00000000 --- a/src/leetcode/cpp/0206.reverse-linked-list/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0206-reverse-linked-list - main.c - ) diff --git a/src/leetcode/cpp/0206.reverse-linked-list/main.c b/src/leetcode/cpp/0206.reverse-linked-list/main.c deleted file mode 100644 index 52142915..00000000 --- a/src/leetcode/cpp/0206.reverse-linked-list/main.c +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -struct ListNode* listNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -void listFree(struct ListNode* head) { - while (head != NULL) { - struct ListNode* tmp = head; - head = head->next; - free(tmp); - } -} - -void listDebug(struct ListNode* head) { - while (head != NULL) { - printf("%d, ", head->val); - head = head->next; - } - printf("\n"); -} - -struct ListNode* reverseList(struct ListNode* head) { - if (head == NULL) { - return NULL; - } - struct ListNode* prev = NULL; - struct ListNode* next = NULL; - while (head != NULL) { - next = head->next; - head->next = prev; - prev = head; - head = next; - } - return prev; -} - -void checkSolution(int arr[], int len) { - struct ListNode* list = listFromArray(arr, len); - listDebug(list); - list = reverseList(list); - listDebug(list); - listFree(list); -} - -int main() { - int arr1[] = {1, 2, 3, 4, 5}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0])); - - int arr2[] = {1, 2}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0])); - - return 0; -} diff --git a/src/leetcode/cpp/0215.kth-largest-element-in-an-array/CMakeLists.txt b/src/leetcode/cpp/0215.kth-largest-element-in-an-array/CMakeLists.txt deleted file mode 100644 index 9c0331b4..00000000 --- a/src/leetcode/cpp/0215.kth-largest-element-in-an-array/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0215-kth-largest-element-in-an-array - main.c - ) diff --git a/src/leetcode/cpp/0234.palindrome-linked-list/CMakeLists.txt b/src/leetcode/cpp/0234.palindrome-linked-list/CMakeLists.txt deleted file mode 100644 index e71ef6a2..00000000 --- a/src/leetcode/cpp/0234.palindrome-linked-list/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0234-palindrome-linked-list - main.c - ) diff --git a/src/leetcode/cpp/0234.palindrome-linked-list/main.c b/src/leetcode/cpp/0234.palindrome-linked-list/main.c deleted file mode 100644 index 5de03065..00000000 --- a/src/leetcode/cpp/0234.palindrome-linked-list/main.c +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -struct ListNode* listNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -void listFree(struct ListNode* head) { - while (head != NULL) { - struct ListNode* node = head; - head = head->next; - free(node); - } -} - -void listDebug(struct ListNode* head) { - while (head != NULL) { - printf("%d, ", head->val); - head = head->next; - } - printf("\n"); -} - -bool isPalindrome(struct ListNode* head) { - if (head == NULL) { - return true; - } - - // 1. Reverse left half of list. - struct ListNode* slow = head; - struct ListNode* fast = head; - struct ListNode* next = NULL; - struct ListNode* prev = NULL; - - while (fast != NULL && fast->next != NULL) { - slow = slow->next; - fast = fast->next->next; - - // Reverse via slow node. - next = head->next; - head->next = prev; - prev = head; - head = next; - } - - if (fast != NULL) { - slow = slow->next; - } - head = prev; - - // 2. Compare from middle. - while (slow != NULL) { - if (slow->val != head->val) { - return false; - } - head = head->next; - slow = slow->next; - } - return true; -} - -void checkSolution(int arr[], int len) { - struct ListNode* list = listFromArray(arr, len); - listDebug(list); - printf("is palindrome: %d\n", isPalindrome(list)); - listFree(list); -} - -int main() { - int arr1[] = {1, 2, 2, 1}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0])); - - int arr2[] = {1, 2,}; - checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0])); - - return 0; -} diff --git a/src/leetcode/cpp/0237.delete-node-in-a-linked-list/CMakeLists.txt b/src/leetcode/cpp/0237.delete-node-in-a-linked-list/CMakeLists.txt deleted file mode 100644 index fe37c2d4..00000000 --- a/src/leetcode/cpp/0237.delete-node-in-a-linked-list/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0237-delete-node-in-a-linked-list - main.c - ) diff --git a/src/leetcode/cpp/0237.delete-node-in-a-linked-list/main.c b/src/leetcode/cpp/0237.delete-node-in-a-linked-list/main.c deleted file mode 100644 index 717a0895..00000000 --- a/src/leetcode/cpp/0237.delete-node-in-a-linked-list/main.c +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -struct ListNode* listNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -void listFree(struct ListNode* head) { - while (head != NULL) { - struct ListNode* node = head; - head = head->next; - free(node); - } -} - -void listDebug(struct ListNode* head) { - while (head != NULL) { - printf("%d, ", head->val); - head = head->next; - } - printf("\n"); -} - -void deleteNode(struct ListNode* node) { - assert(node != NULL); - assert(node->next != NULL); - struct ListNode* tmp = node->next; - node->val = tmp->val; - node->next = tmp->next; - free(tmp); -} - -void checkSolution(int arr[], int len, int target) { - struct ListNode* list = listFromArray(arr, len); - struct ListNode* node = list; - for (int i = 0; i < target; ++i) { - node = node->next; - } - deleteNode(node); - listDebug(list); - listFree(list); -} - -int main() { - int arr[] = {4, 5, 1, 9}; - checkSolution(arr, sizeof(arr) / sizeof(arr[0]), 2); - - return 0; -} diff --git a/src/leetcode/cpp/0268.missing-number/CMakeLists.txt b/src/leetcode/cpp/0268.missing-number/CMakeLists.txt deleted file mode 100644 index f4221cf7..00000000 --- a/src/leetcode/cpp/0268.missing-number/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0268-missing-number - main.cpp - ) diff --git a/src/leetcode/cpp/0268.missing-number/main.cpp b/src/leetcode/cpp/0268.missing-number/main.cpp deleted file mode 100644 index 45932cc4..00000000 --- a/src/leetcode/cpp/0268.missing-number/main.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include - -class Solution { - public: - static int missingNumber(std::vector& nums) { - constexpr size_t MAX_LEN = 10000; - std::bitset bits; - for (int num: nums) { - bits.set(num); - } - for (size_t i = 0; i < nums.size(); ++i) { - if (!bits[i]) { - return i; - } - } - return nums.size(); - } -}; - - -void checkSolution() { - { - std::vector nums = {3,0,1}; - assert(Solution::missingNumber(nums) == 2); - } - - { - std::vector nums = {9,6,4,2,3,5,7,0,1}; - assert(Solution::missingNumber(nums) == 8); - } -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0308.odd-even-linked-list/CMakeLists.txt b/src/leetcode/cpp/0308.odd-even-linked-list/CMakeLists.txt deleted file mode 100644 index 58a7a525..00000000 --- a/src/leetcode/cpp/0308.odd-even-linked-list/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0308-odd-even-linked-list - main.c - ) diff --git a/src/leetcode/cpp/0308.odd-even-linked-list/main.c b/src/leetcode/cpp/0308.odd-even-linked-list/main.c deleted file mode 100644 index b72e6098..00000000 --- a/src/leetcode/cpp/0308.odd-even-linked-list/main.c +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode* next; -}; - -struct ListNode* listNew(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = val; - node->next = NULL; - return node; -} - -struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = listNew(arr[i]); - node->next = list; - list = node; - } - return list; -} - -void listFree(struct ListNode* head) { - while (head != NULL) { - struct ListNode* tmp = head; - head = head->next; - free(tmp); - } -} - -void listDebug(struct ListNode* head) { - while (head != NULL) { - printf("%d, ", head->val); - head = head->next; - } - printf("\n"); -} - -struct ListNode* oddEvenList(struct ListNode* head) { - if (head == NULL || head->next == NULL) { - return NULL; - } - struct ListNode* even_head = head->next; - struct ListNode* even = head->next; - struct ListNode* odd = head; - struct ListNode* old_next = NULL; - - while (odd != NULL && odd->next != NULL) { - printf("odd: %d\n", odd->val); - old_next = odd->next; - odd->next = old_next->next; - odd = old_next->next; - } - - /* - while (even != NULL && even->next != NULL) { - old_next = even->next; - even->next = old_next->next; - } - */ - - assert(odd != NULL); - //odd->next = even_head; - //even->next = NULL; - return head; -} - -void checkSolution(int arr[], int len) { - struct ListNode* list = listFromArray(arr, len); - listDebug(list); - list = oddEvenList(list); - listDebug(list); - listFree(list); -} - -int main() { - int arr1[] = {1, 2, 3, 4, 5, 6, 7}; - checkSolution(arr1, sizeof(arr1) / sizeof(arr1[0])); - - //int arr2[] = {2, 1, 3, 5, 6, 4, 7}; - //checkSolution(arr2, sizeof(arr2) / sizeof(arr2[0])); - return 0; -} diff --git a/src/leetcode/cpp/0367.valid-perfect-square/CMakeLists.txt b/src/leetcode/cpp/0367.valid-perfect-square/CMakeLists.txt deleted file mode 100644 index c18fd7be..00000000 --- a/src/leetcode/cpp/0367.valid-perfect-square/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0367-valid-perfect-square - main.cpp - ) diff --git a/src/leetcode/cpp/0367.valid-perfect-square/main.cpp b/src/leetcode/cpp/0367.valid-perfect-square/main.cpp deleted file mode 100644 index 9352cf2c..00000000 --- a/src/leetcode/cpp/0367.valid-perfect-square/main.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include -#include - -class Solution { - public: - // Binary search - static bool isPerfectSquare(int num) { - int left = 1; - int right = num; - while (left <= right) { - int middle = left + (right - left) / 2; - int64_t square = (int64_t)middle * (int64_t)middle; - if (square > num) { - right = middle - 1; - } else if (square < num) { - left = middle + 1; - } else { - return true; - } - } - return false; - } -}; - - -void checkSolution() { - assert(Solution::isPerfectSquare(1)); - assert(!Solution::isPerfectSquare(8)); - assert(Solution::isPerfectSquare(9)); - assert(!Solution::isPerfectSquare(10)); - assert(!Solution::isPerfectSquare(14)); - assert(Solution::isPerfectSquare(16)); - assert(!Solution::isPerfectSquare(INT_MAX)); -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0414.third-maximum-number/CMakeLists.txt b/src/leetcode/cpp/0414.third-maximum-number/CMakeLists.txt deleted file mode 100644 index 51998cf5..00000000 --- a/src/leetcode/cpp/0414.third-maximum-number/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0414-third-maximum-number - main.cpp - ) diff --git a/src/leetcode/cpp/0414.third-maximum-number/main.cpp b/src/leetcode/cpp/0414.third-maximum-number/main.cpp deleted file mode 100644 index f9e031cd..00000000 --- a/src/leetcode/cpp/0414.third-maximum-number/main.cpp +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include - -class Solution { - public: - static - int thirdMax(std::vector& nums) { - assert(!nums.empty()); - - std::sort(nums.rbegin(), nums.rend()); - auto iter = std::unique(nums.begin(), nums.end()); - nums.erase(iter, nums.end()); - - if (nums.size() < 3) { - return nums.at(0); - } - return nums.at(2); - } -}; - - -void checkSolution() { - { - std::vector nums = {3, 2, 1}; - int target = Solution::thirdMax(nums); - assert(target == 1); - } - - { - std::vector nums = {1, 2}; - int target = Solution::thirdMax(nums); - assert(target == 2); - } - - { - std::vector nums = {2, 2, 3, 1}; - int target = Solution::thirdMax(nums); - assert(target == 1); - } - - { - std::vector nums = {1, 1, 2}; - int target = Solution::thirdMax(nums); - assert(target == 2); - } -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0443.string-compression/CMakeLists.txt b/src/leetcode/cpp/0443.string-compression/CMakeLists.txt deleted file mode 100644 index 86e586b6..00000000 --- a/src/leetcode/cpp/0443.string-compression/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0443-string-compression - main.c - ) diff --git a/src/leetcode/cpp/0445.add-two-numbers-ii/CMakeLists.txt b/src/leetcode/cpp/0445.add-two-numbers-ii/CMakeLists.txt deleted file mode 100644 index 19d85a1c..00000000 --- a/src/leetcode/cpp/0445.add-two-numbers-ii/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0445-add-two-numbers-ii - main.c - ) diff --git a/src/leetcode/cpp/0445.add-two-numbers-ii/main.c b/src/leetcode/cpp/0445.add-two-numbers-ii/main.c deleted file mode 100644 index bc0c7913..00000000 --- a/src/leetcode/cpp/0445.add-two-numbers-ii/main.c +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include -#include - -struct ListNode { - int val; - struct ListNode *next; -}; - -struct ListNode* listFromArray(int arr[], int len) { - struct ListNode* list = NULL; - for (int i = len - 1; i >= 0; --i) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - assert(node != NULL); - node->val = arr[i]; - node->next = list; - list = node; - } - return list; -} - -static -struct ListNode* listReverse(struct ListNode* list) { - struct ListNode* previous = NULL; - struct ListNode* next = NULL; - - while (list != NULL) { - next = list->next; - list->next = previous; - previous = list; - list = next; - } - return previous; -} - -struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { - assert(l1 != NULL); - assert(l2 != NULL); - l1 = listReverse(l1); - l2 = listReverse(l2); - - struct ListNode* l3 = NULL; - struct ListNode* tail_node = NULL; - int carry = 0; - - while (l1 != NULL || l2 != NULL || carry != 0) { - int sum = carry; - if (l1 != NULL) { - sum += l1->val; - l1 = l1->next; - } - if (l2 != NULL) { - sum += l2->val; - l2 = l2->next; - } - carry = sum / 10; - struct ListNode* new_node = malloc(sizeof(struct ListNode)); - assert(new_node != NULL); - new_node->val = sum % 10; - new_node->next = NULL; - if (tail_node == NULL) { - l3 = new_node; - tail_node = new_node; - } else { - tail_node->next = new_node; - tail_node = new_node; - } - } - return listReverse(l3); -} - -int main() { - int arr1[] = {7, 2, 4, 3}; - int arr2[] = {5, 6, 4}; - struct ListNode* l1 = listFromArray(arr1, 4); - struct ListNode* l2 = listFromArray(arr2, 3); - struct ListNode* l3 = addTwoNumbers(l1, l2); - while (l3 != NULL) { - printf("%d, ", l3->val); - l3 = l3->next; - } - printf("\n"); - - return 0; -} diff --git a/src/leetcode/cpp/0485.max-consecutive-ones/CMakeLists.txt b/src/leetcode/cpp/0485.max-consecutive-ones/CMakeLists.txt deleted file mode 100644 index eb666220..00000000 --- a/src/leetcode/cpp/0485.max-consecutive-ones/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0485-max-consecutive-ones - main.cpp - ) diff --git a/src/leetcode/cpp/0485.max-consecutive-ones/main.cpp b/src/leetcode/cpp/0485.max-consecutive-ones/main.cpp deleted file mode 100644 index 5cceefe0..00000000 --- a/src/leetcode/cpp/0485.max-consecutive-ones/main.cpp +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include - -class Solution { - public: - static int findMaxConsecutiveOnes(std::vector& nums) { - int max_consecutive = 0; - - int count_of_1 = 0; - for (int num : nums) { - if (num == 1) { - count_of_1 += 1; - } else { - max_consecutive = std::max(max_consecutive, count_of_1); - count_of_1 = 0; - } - } - - return std::max(max_consecutive, count_of_1); - } -}; - - -void checkSolution() { - std::vector nums = {1,1,0,1,1,1}; - assert(Solution::findMaxConsecutiveOnes(nums) == 3); - - std::vector nums2 = {1,0,1,1,0,1}; - assert(Solution::findMaxConsecutiveOnes(nums2) == 2); -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0605.can-place-flowers/CMakeLists.txt b/src/leetcode/cpp/0605.can-place-flowers/CMakeLists.txt deleted file mode 100644 index f45f803c..00000000 --- a/src/leetcode/cpp/0605.can-place-flowers/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0605-can-place-flowers - main.cpp - ) diff --git a/src/leetcode/cpp/0605.can-place-flowers/main.cpp b/src/leetcode/cpp/0605.can-place-flowers/main.cpp deleted file mode 100644 index 094bdd24..00000000 --- a/src/leetcode/cpp/0605.can-place-flowers/main.cpp +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include - -class Solution { - public: - static - bool canPlaceFlowers(std::vector& flowerbed, int n) { - if (n == 0) { - return true; - } - - flowerbed.insert(flowerbed.begin(), 0); - flowerbed.push_back(0); - int i = 1; - for (i = 1; i < flowerbed.size() - 1; ++i) { - if (flowerbed.at(i) == 1) { - i += 1; - } else { - if (flowerbed.at(i - 1) == 0 && flowerbed.at(i + 1) == 0) { - flowerbed[i] = 1; - n -= 1; - if (n == 0) { - break; - } - } - } - } - - return n == 0; - } -}; - - -void checkSolution() { - { - std::vector flowerbed = {1,0,0,0,1}; - assert(Solution::canPlaceFlowers(flowerbed, 1)); - } - - { - std::vector flowerbed = {1,0,0,0,1}; - assert(!Solution::canPlaceFlowers(flowerbed, 2)); - } - - { - std::vector flowerbed = {1,0,0,0,0,1}; - assert(!Solution::canPlaceFlowers(flowerbed, 2)); - } - - { - std::vector flowerbed = {0,1,0,1,0,1,0,0}; - assert(Solution::canPlaceFlowers(flowerbed, 1)); - } - - { - std::vector flowerbed = {1,0,0,1}; - assert(!Solution::canPlaceFlowers(flowerbed, 1)); - } - - { - std::vector flowerbed = {0,0,1,0,0}; - assert(Solution::canPlaceFlowers(flowerbed, 1)); - } -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0628.maximum-product-of-three-numbers/CMakeLists.txt b/src/leetcode/cpp/0628.maximum-product-of-three-numbers/CMakeLists.txt deleted file mode 100644 index fd8e8b46..00000000 --- a/src/leetcode/cpp/0628.maximum-product-of-three-numbers/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0628-maximum-product-of-three-numbers - main.cpp - ) diff --git a/src/leetcode/cpp/0628.maximum-product-of-three-numbers/main.cpp b/src/leetcode/cpp/0628.maximum-product-of-three-numbers/main.cpp deleted file mode 100644 index 8757dd2a..00000000 --- a/src/leetcode/cpp/0628.maximum-product-of-three-numbers/main.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include - -class Solution { - public: - static - int maximumProduct(std::vector& nums) { - assert(nums.size() >= 3); - - std::sort(nums.begin(), nums.end()); - const auto len = nums.size(); - std::vector max_vals = { - // -3, -2, -1 - nums.at(len - 3) * nums.at(len - 2) * nums.at(len - 1), - // 0, -2, -1 - nums.at(0) * nums.at(len - 2) * nums.at(len - 1), - // 0, 1, -1 - nums.at(0) * nums.at(1) * nums.at(len - 1), - }; - std::sort(max_vals.begin(), max_vals.end()); - return max_vals.at(max_vals.size() - 1); - } -}; - - -void checkSolution() { - { - std::vector nums = {-100,-98,-1,2,3,4}; - int target = Solution::maximumProduct(nums); - assert(target == 39200); - } -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0665.non-decreasing-array/CMakeLists.txt b/src/leetcode/cpp/0665.non-decreasing-array/CMakeLists.txt deleted file mode 100644 index d27c5dc5..00000000 --- a/src/leetcode/cpp/0665.non-decreasing-array/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0665-non-decreasing-array - main.cpp - ) diff --git a/src/leetcode/cpp/0665.non-decreasing-array/main.cpp b/src/leetcode/cpp/0665.non-decreasing-array/main.cpp deleted file mode 100644 index f7ef87c5..00000000 --- a/src/leetcode/cpp/0665.non-decreasing-array/main.cpp +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include - -class Solution { - public: - static - bool checkPossibility(std::vector& nums) { - if (nums.size() <= 2) { - return true; - } - - int swap_count = 0; - for (int i = 1; i < nums.size() && swap_count < 2; ++i) { - if (nums[i - 1] >= nums[i]) { - // swap value - swap_count += 1; - if (i > 2 && nums[i - 2] > nums[i]) { - nums[i] = nums[i - 1]; - } else { - nums[i - 1] = nums[i]; - } - } - } - - return swap_count <= 1; - } -}; - -void checkSolution() { - { - std::vector nums = {4, 2, 3}; - assert(Solution::checkPossibility(nums)); - } - - { - std::vector nums = {4, 2, 1}; - assert(!Solution::checkPossibility(nums)); - } - - { - std::vector nums = {3,4,2,3}; - assert(!Solution::checkPossibility(nums)); - } - - { - std::vector nums = {5,7,1,8}; - assert(Solution::checkPossibility(nums)); - } -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0695.max-area-of-island/CMakeLists.txt b/src/leetcode/cpp/0695.max-area-of-island/CMakeLists.txt deleted file mode 100644 index 6d9ebb08..00000000 --- a/src/leetcode/cpp/0695.max-area-of-island/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0695-max-area-of-island - main.c - ) diff --git a/src/leetcode/cpp/0704.binary-search/CMakeLists.txt b/src/leetcode/cpp/0704.binary-search/CMakeLists.txt deleted file mode 100644 index e674b68a..00000000 --- a/src/leetcode/cpp/0704.binary-search/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0704-binary-search - main.c - ) diff --git a/src/leetcode/cpp/0704.binary-search/main.c b/src/leetcode/cpp/0704.binary-search/main.c deleted file mode 100644 index 921b41c4..00000000 --- a/src/leetcode/cpp/0704.binary-search/main.c +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -int search(int* nums, int numsSize, int target) { - if (numsSize == 0) { - return -1; - } - - int left = 0; - int right = numsSize - 1; - int middle; - - while (left <= right) { - middle = left + (right - left) / 2; - if (nums[middle] == target) { - return middle; - } - if (nums[middle] > target) { - right = middle - 1; - } else { - left = middle + 1; - } - } - return -1; -} - -int main() { - return 0; -} diff --git a/src/leetcode/cpp/0925.long-pressed-name/CMakeLists.txt b/src/leetcode/cpp/0925.long-pressed-name/CMakeLists.txt deleted file mode 100644 index 45b4e8be..00000000 --- a/src/leetcode/cpp/0925.long-pressed-name/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0925-long-pressed-name - main.cpp - ) diff --git a/src/leetcode/cpp/0925.long-pressed-name/main.cpp b/src/leetcode/cpp/0925.long-pressed-name/main.cpp deleted file mode 100644 index 34908d94..00000000 --- a/src/leetcode/cpp/0925.long-pressed-name/main.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include - -class Solution { - public: - static bool isLongPressedName(std::string name, std::string typed) { - assert(!name.empty() && !typed.empty()); - - const size_t m = name.size(); - const size_t n = typed.size(); - size_t i = 0; - size_t j = 0; - while (i < m && j < n) { - if (name[i] == typed[j]) { - i += 1; - j += 1; - } else if (i > 0 && name[i - 1] == typed[j]) { - j += 1; - } else { - return false; - } - } - - while (j < n && name[m - 1] == typed[j]) { - j += 1; - } - - return i == m && j == n; - } -}; - - -void checkSolution() { -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/0994.rotting-oranges/CMakeLists.txt b/src/leetcode/cpp/0994.rotting-oranges/CMakeLists.txt deleted file mode 100644 index e091f9f6..00000000 --- a/src/leetcode/cpp/0994.rotting-oranges/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0994-rotting-oranges - main.c - ) diff --git a/src/leetcode/cpp/1090.largest-values-from-labels/CMakeLists.txt b/src/leetcode/cpp/1090.largest-values-from-labels/CMakeLists.txt deleted file mode 100644 index 5e43dc78..00000000 --- a/src/leetcode/cpp/1090.largest-values-from-labels/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-1090-largest-values-from-labels - main.c - ) diff --git a/src/leetcode/cpp/1090.largest-values-from-labels/main.c b/src/leetcode/cpp/1090.largest-values-from-labels/main.c deleted file mode 100644 index e69de29b..00000000 diff --git a/src/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/CMakeLists.txt b/src/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/CMakeLists.txt deleted file mode 100644 index b344be06..00000000 --- a/src/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-1423-maximum-points-you-can-obtain-from-cards - main.c - ) diff --git a/src/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/main.c b/src/leetcode/cpp/1423.maximum-points-you-can-obtain-from-cards/main.c deleted file mode 100644 index e69de29b..00000000 diff --git a/src/leetcode/cpp/1446.consecutive-characters/CMakeLists.txt b/src/leetcode/cpp/1446.consecutive-characters/CMakeLists.txt deleted file mode 100644 index 97337042..00000000 --- a/src/leetcode/cpp/1446.consecutive-characters/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-1446-consecutive-characters - main.cpp - ) diff --git a/src/leetcode/cpp/1446.consecutive-characters/main.cpp b/src/leetcode/cpp/1446.consecutive-characters/main.cpp deleted file mode 100644 index f7d590df..00000000 --- a/src/leetcode/cpp/1446.consecutive-characters/main.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -#include -#include - -class Solution { - public: - static int maxPower(std::string s) { - int max_chars = 0; - char c; - int char_count = 0; - for (int i = 0; i < s.size(); ++i) { - if (s[i] == c) { - char_count += 1; - } else { - c = s[i]; - max_chars = std::max(max_chars, char_count); - char_count = 1; - } - } - - max_chars = std::max(max_chars, char_count); - return max_chars; - } -}; - - -void checkSolution() { - std::string s = "leetcode"; - assert(Solution::maxPower(s) == 2); - - std::string s2 = "abbcccddddeeeeedcba"; - assert(Solution::maxPower(s2) == 5); -} - -int main() { - checkSolution(); - return 0; -} diff --git a/src/leetcode/cpp/CMakeLists.txt b/src/leetcode/cpp/CMakeLists.txt deleted file mode 100644 index 493a518e..00000000 --- a/src/leetcode/cpp/CMakeLists.txt +++ /dev/null @@ -1,75 +0,0 @@ - -cmake_minimum_required(VERSION 3.10) -project(leetcode-cpp) - -if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer" - ) -endif() - -add_subdirectory(0003.longest-substring-without-repeating-characters) -add_subdirectory(0005.longest-palindromic-substring) -add_subdirectory(0008.string-to-integer-atoi) -add_subdirectory(0014.longest-common-prefix) -add_subdirectory(0016.3sum-closest) -add_subdirectory(0018.4sum) -add_subdirectory(0019.remove-nth-node-from-end-of-list) -add_subdirectory(0020.valid-parentheses) -add_subdirectory(0021.merge-two-sorted-lists) -#add_subdirectory(0025.reverse-nodes-in-k-group) -add_subdirectory(0027.remove-element) -#add_subdirectory(0031.next-permutation) -add_subdirectory(0034.find-first-and-last-position-of-element-in-sorted-array) -#add_subdirectory(0035.search-insert-position) -#add_subdirectory(0053.maximum-subarray) -add_subdirectory(0061.rotate-list) -#add_subdirectory(0062.unique-paths) -add_subdirectory(0066.plus-one) -add_subdirectory(0069.sqrtx) -add_subdirectory(0070.climbing-stairs) -add_subdirectory(0075.sort-colors) -add_subdirectory(0082.remove-duplicates-from-sorted-list-ii) -add_subdirectory(0083.remove-duplicates-from-sorted-list) -add_subdirectory(0086.partition-list) -add_subdirectory(0092.reverse-linked-list-ii) -add_subdirectory(0093.restore-ip-addresses) -add_subdirectory(0098.validate-binary-search-tree) -add_subdirectory(0101.symmetric-tree) -add_subdirectory(0102.binary-tree-level-order-traversal) -add_subdirectory(0103.binary-tree-zigzag-level-order-traversal) -add_subdirectory(0107.binary-tree-level-order-traversal-ii) -add_subdirectory(0108.convert-sorted-array-to-binary-search-tree) -add_subdirectory(0114.flatten-binary-tree-to-linked-list) -add_subdirectory(0137.single-number-ii) -add_subdirectory(0139.word-break) -add_subdirectory(0141.linked-list-cycle) -add_subdirectory(0142.linked-list-cycle-ii) -add_subdirectory(0144.binary-tree-preorder-traversal) -add_subdirectory(0146.lru-cache) -add_subdirectory(0160.intersection-of-two-linked-lists) -#add_subdirectory(0162.find-peak-element) -#add_subdirectory(0199.binary-tree-right-side-view) -#add_subdirectory(0200.number-of-islands) -add_subdirectory(0203.remove-linked-list-elements) -add_subdirectory(0206.reverse-linked-list) -#add_subdirectory(0215.kth-largest-element-in-an-array) -add_subdirectory(0234.palindrome-linked-list) -add_subdirectory(0237.delete-node-in-a-linked-list) -add_subdirectory(0268.missing-number) -add_subdirectory(0308.odd-even-linked-list) -add_subdirectory(0367.valid-perfect-square) -add_subdirectory(0414.third-maximum-number) -#add_subdirectory(0443.string-compression) -add_subdirectory(0445.add-two-numbers-ii) -add_subdirectory(0485.max-consecutive-ones) -add_subdirectory(0605.can-place-flowers) -add_subdirectory(0628.maximum-product-of-three-numbers) -add_subdirectory(0665.non-decreasing-array) -#add_subdirectory(0695.max-area-of-island) -add_subdirectory(0704.binary-search) -add_subdirectory(0925.long-pressed-name) -#add_subdirectory(0994.rotting-oranges) -#add_subdirectory(1090.largest-values-from-labels) -#add_subdirectory(1423.maximum-points-you-can-obtain-from-cards) -add_subdirectory(1446.consecutive-characters) diff --git a/src/leetcode/cpp/template/CMakeLists.txt b/src/leetcode/cpp/template/CMakeLists.txt deleted file mode 100644 index 0b0066fe..00000000 --- a/src/leetcode/cpp/template/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ - -add_executable(lc-0000-template - main.cpp - ) diff --git a/src/leetcode/cpp/template/main.cpp b/src/leetcode/cpp/template/main.cpp deleted file mode 100644 index 382aa2f7..00000000 --- a/src/leetcode/cpp/template/main.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2024 Xu Shaohua . All rights reserved. -<<<<<<<< HEAD:tree/src/binary_tree/mod.rs -// Use of this source is governed by General Public License that can be found -// in the LICENSE file. -======== -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include -#include - -class Solution { - public: -}; - - -void checkSolution() { -} - -int main() { - checkSolution(); - return 0; -} ->>>>>>>> leetcode/main:leetcode/cpp/template/main.cpp