Skip to content

Commit

Permalink
Merge remote-tracking branch 'leetcode/main' into tmp-leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Feb 20, 2024
2 parents 107ecdf + 84020ce commit a8a7ad3
Show file tree
Hide file tree
Showing 189 changed files with 6,205 additions and 0 deletions.
3 changes: 3 additions & 0 deletions leetcode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
/cmake-build-debug
/cmake-build-release
5 changes: 5 additions & 0 deletions leetcode/CMakeLists.txt
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)
127 changes: 127 additions & 0 deletions leetcode/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions leetcode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[workspace]
resolver = "2"

members = [
"rust/*.*"
]

exclude = [
"cpp",
"problems",
]
232 changes: 232 additions & 0 deletions leetcode/LICENSE

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions leetcode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# About

Leetcode problems and solutions

4 changes: 4 additions & 0 deletions leetcode/cpp/0002.add-two-numbers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

add_executable(lc-0002-add-two-numbers
main.c
)
69 changes: 69 additions & 0 deletions leetcode/cpp/0002.add-two-numbers/main.c
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;
}
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
)
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

add_executable(lc-0005-longest-palindromic-substring
main.c
)
11 changes: 11 additions & 0 deletions leetcode/cpp/0005.longest-palindromic-substring/main.c
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;
}
4 changes: 4 additions & 0 deletions leetcode/cpp/0007.reverse-integer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

add_executable(lc-0007-reverse-integer
main.c
)
39 changes: 39 additions & 0 deletions leetcode/cpp/0007.reverse-integer/main.c
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;
}
4 changes: 4 additions & 0 deletions leetcode/cpp/0008.string-to-integer-atoi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

add_executable(lc-0008-string-to-integer-atoi
main.c
)
Loading

0 comments on commit a8a7ad3

Please sign in to comment.