-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'leetcode/main' into tmp-leetcode
- Loading branch information
Showing
189 changed files
with
6,205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target | ||
/cmake-build-debug | ||
/cmake-build-release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
cmake_minimum_required(VERSION 3.10) | ||
project(leetcode) | ||
|
||
add_subdirectory(cpp) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[workspace] | ||
resolver = "2" | ||
|
||
members = [ | ||
"rust/*.*" | ||
] | ||
|
||
exclude = [ | ||
"cpp", | ||
"problems", | ||
] |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
# About | ||
|
||
Leetcode problems and solutions | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
add_executable(lc-0002-add-two-numbers | ||
main.c | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) 2023 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be | ||
// found in the LICENSE file. | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
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* new_node = malloc(sizeof(struct ListNode)); | ||
assert(new_node != NULL); | ||
new_node->val = arr[i]; | ||
new_node->next = list; | ||
list = new_node; | ||
} | ||
return list; | ||
} | ||
|
||
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { | ||
struct ListNode* l3 = NULL; | ||
struct ListNode* tail_node = NULL; | ||
int carry = 0; | ||
while (l1 != NULL || l2 != NULL || carry != 0) { | ||
struct ListNode* new_node = malloc(sizeof(struct ListNode)); | ||
assert(new_node != NULL); | ||
int sum = carry; | ||
if (l1 != NULL) { | ||
sum += l1->val; | ||
l1 = l1->next; | ||
} | ||
if (l2 != NULL) { | ||
sum += l2->val; | ||
l2 = l2->next; | ||
} | ||
carry = sum / 10; | ||
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 l3; | ||
} | ||
|
||
int main() { | ||
int arr1[] = {2, 4, 3}; | ||
int arr2[] = {5, 6, 4}; | ||
struct ListNode* l1 = listFromArray(arr1, 3); | ||
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; | ||
} |
4 changes: 4 additions & 0 deletions
4
leetcode/cpp/0003.longest-substring-without-repeating-characters/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
add_executable(lc-0003-longest-substring-without-repeating-characters | ||
main.c | ||
) |
52 changes: 52 additions & 0 deletions
52
leetcode/cpp/0003.longest-substring-without-repeating-characters/main.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be | ||
// found in the LICENSE file. | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
|
||
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; | ||
} |
4 changes: 4 additions & 0 deletions
4
leetcode/cpp/0005.longest-palindromic-substring/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
add_executable(lc-0005-longest-palindromic-substring | ||
main.c | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be | ||
// found in the LICENSE file. | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main() { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
add_executable(lc-0007-reverse-integer | ||
main.c | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be | ||
// found in the LICENSE file. | ||
|
||
#include <assert.h> | ||
#include <limits.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
int reverse(int x) { | ||
int rev = 0; | ||
while (x != 0) { | ||
if (rev > INT_MAX / 10 || rev < INT_MIN / 10) { | ||
return 0; | ||
} | ||
rev = rev * 10 + x % 10; | ||
x /= 10; | ||
} | ||
|
||
return rev; | ||
} | ||
|
||
void checkSolution(int x, int expected_rev_x) { | ||
int rev_x = reverse(x); | ||
printf("x: %d, rev_x: %d, expected: %d\n", x, rev_x, expected_rev_x); | ||
assert(rev_x == expected_rev_x); | ||
} | ||
|
||
int main() { | ||
checkSolution(123, 321); | ||
checkSolution(-123, -321); | ||
checkSolution(120, 21); | ||
checkSolution(1534236469, 0); | ||
checkSolution(-2147483648, 0); | ||
checkSolution(-2147483412, -2143847412); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
add_executable(lc-0008-string-to-integer-atoi | ||
main.c | ||
) |
Oops, something went wrong.