From 9de9abad07b98902ab6ee5edd7fc2cbf337335c6 Mon Sep 17 00:00:00 2001 From: pranjal030404 Date: Fri, 23 May 2025 15:03:53 +0530 Subject: [PATCH 01/19] olution and Readme file updated with all the terms --- .../0000-0099/0002.Add Two Numbers/README.md | 49 +++++++++++++++++++ .../0002.Add Two Numbers/README_EN.md | 48 ++++++++++++++++++ .../0000-0099/0002.Add Two Numbers/Solution.c | 40 +++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 solution/0000-0099/0002.Add Two Numbers/Solution.c diff --git a/solution/0000-0099/0002.Add Two Numbers/README.md b/solution/0000-0099/0002.Add Two Numbers/README.md index 3806f64b53d17..9e9f0a440e250 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README.md +++ b/solution/0000-0099/0002.Add Two Numbers/README.md @@ -494,6 +494,55 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi result = aggregate ``` +#### C + ``` c + + /** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) +{ + struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); + dummy->val = 0; + dummy->next = NULL; + struct ListNode *curr = dummy; + 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; + int val = sum % 10; + + struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode)); + newNode->val = val; + newNode->next = NULL; + curr->next = newNode; + curr = curr->next; + } + + struct ListNode *result = dummy->next; + free(dummy); + return result; +} + ``` + diff --git a/solution/0000-0099/0002.Add Two Numbers/README_EN.md b/solution/0000-0099/0002.Add Two Numbers/README_EN.md index f6878f0937322..37dbc7e135713 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README_EN.md +++ b/solution/0000-0099/0002.Add Two Numbers/README_EN.md @@ -490,6 +490,54 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi result = aggregate ``` +#### C +```c +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) +{ + struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); + dummy->val = 0; + dummy->next = NULL; + struct ListNode *curr = dummy; + 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; + int val = sum % 10; + + struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode)); + newNode->val = val; + newNode->next = NULL; + curr->next = newNode; + curr = curr->next; + } + + struct ListNode *result = dummy->next; + free(dummy); + return result; +} +``` + diff --git a/solution/0000-0099/0002.Add Two Numbers/Solution.c b/solution/0000-0099/0002.Add Two Numbers/Solution.c new file mode 100644 index 0000000000000..08fd89cf9ded0 --- /dev/null +++ b/solution/0000-0099/0002.Add Two Numbers/Solution.c @@ -0,0 +1,40 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { + struct ListNode* dummy = (struct ListNode*) malloc(sizeof(struct ListNode)); + dummy->val = 0; + dummy->next = NULL; + struct ListNode* curr = dummy; + 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; + int val = sum % 10; + + struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode)); + newNode->val = val; + newNode->next = NULL; + curr->next = newNode; + curr = curr->next; + } + + struct ListNode* result = dummy->next; + free(dummy); + return result; +} \ No newline at end of file From 2e3bd2ffe029e81fb10d9cceca9dfb75e6b5b5e5 Mon Sep 17 00:00:00 2001 From: pranjal030404 <182807087+pranjal030404@users.noreply.github.com> Date: Fri, 23 May 2025 09:57:57 +0000 Subject: [PATCH 02/19] style: format code and docs with prettier --- .../0000-0099/0002.Add Two Numbers/README.md | 85 ++++++++++--------- .../0002.Add Two Numbers/README_EN.md | 7 +- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/solution/0000-0099/0002.Add Two Numbers/README.md b/solution/0000-0099/0002.Add Two Numbers/README.md index 9e9f0a440e250..8f46cb7b1e210 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README.md +++ b/solution/0000-0099/0002.Add Two Numbers/README.md @@ -495,53 +495,54 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi ``` #### C - ``` c - /** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * struct ListNode *next; - * }; - */ +```c + +/** +* Definition for singly-linked list. +* struct ListNode { +* int val; +* struct ListNode *next; +* }; +*/ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) { - struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); - dummy->val = 0; - dummy->next = NULL; - struct ListNode *curr = dummy; - 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; - int val = sum % 10; - - struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode)); - newNode->val = val; - newNode->next = NULL; - curr->next = newNode; - curr = curr->next; - } - - struct ListNode *result = dummy->next; - free(dummy); - return result; + struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); + dummy->val = 0; + dummy->next = NULL; + struct ListNode *curr = dummy; + 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; + int val = sum % 10; + + struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode)); + newNode->val = val; + newNode->next = NULL; + curr->next = newNode; + curr = curr->next; + } + + struct ListNode *result = dummy->next; + free(dummy); + return result; } - ``` +``` diff --git a/solution/0000-0099/0002.Add Two Numbers/README_EN.md b/solution/0000-0099/0002.Add Two Numbers/README_EN.md index 37dbc7e135713..e9fc9a9bbbc8a 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README_EN.md +++ b/solution/0000-0099/0002.Add Two Numbers/README_EN.md @@ -490,7 +490,8 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi result = aggregate ``` -#### C +#### C + ```c /** * Definition for singly-linked list. @@ -533,9 +534,9 @@ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) } struct ListNode *result = dummy->next; - free(dummy); + free(dummy); return result; -} +} ``` From 4e9aab5c4311ec80adcfc3789175e9d9683655af Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 23 May 2025 20:16:01 +0800 Subject: [PATCH 03/19] Update README.md --- .../0000-0099/0002.Add Two Numbers/README.md | 80 +++++++++---------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/solution/0000-0099/0002.Add Two Numbers/README.md b/solution/0000-0099/0002.Add Two Numbers/README.md index 8f46cb7b1e210..2b908b24a55a7 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README.md +++ b/solution/0000-0099/0002.Add Two Numbers/README.md @@ -499,48 +499,44 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi ```c /** -* Definition for singly-linked list. -* struct ListNode { -* int val; -* struct ListNode *next; -* }; -*/ - -struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) -{ - struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); - dummy->val = 0; - dummy->next = NULL; - struct ListNode *curr = dummy; - 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; - int val = sum % 10; - - struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode)); - newNode->val = val; - newNode->next = NULL; - curr->next = newNode; - curr = curr->next; - } - - struct ListNode *result = dummy->next; - free(dummy); - return result; + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { + struct ListNode* dummy = (struct ListNode*) malloc(sizeof(struct ListNode)); + dummy->val = 0; + dummy->next = NULL; + struct ListNode* curr = dummy; + 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; + int val = sum % 10; + + struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode)); + newNode->val = val; + newNode->next = NULL; + curr->next = newNode; + curr = curr->next; + } + + struct ListNode* result = dummy->next; + free(dummy); + return result; } ``` From 6b22bb92025770a5dc3faee76932b9fc9956669d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 23 May 2025 20:16:29 +0800 Subject: [PATCH 04/19] Update README_EN.md --- .../0002.Add Two Numbers/README_EN.md | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/solution/0000-0099/0002.Add Two Numbers/README_EN.md b/solution/0000-0099/0002.Add Two Numbers/README_EN.md index e9fc9a9bbbc8a..6274e8c2ad5e6 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README_EN.md +++ b/solution/0000-0099/0002.Add Two Numbers/README_EN.md @@ -493,6 +493,7 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi #### C ```c + /** * Definition for singly-linked list. * struct ListNode { @@ -501,24 +502,20 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi * }; */ -struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) -{ - struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); +struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { + struct ListNode* dummy = (struct ListNode*) malloc(sizeof(struct ListNode)); dummy->val = 0; dummy->next = NULL; - struct ListNode *curr = dummy; + struct ListNode* curr = dummy; int carry = 0; - while (l1 != NULL || l2 != NULL || carry != 0) - { + while (l1 != NULL || l2 != NULL || carry != 0) { int sum = carry; - if (l1 != NULL) - { + if (l1 != NULL) { sum += l1->val; l1 = l1->next; } - if (l2 != NULL) - { + if (l2 != NULL) { sum += l2->val; l2 = l2->next; } @@ -526,14 +523,14 @@ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) carry = sum / 10; int val = sum % 10; - struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode)); + struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode)); newNode->val = val; newNode->next = NULL; curr->next = newNode; curr = curr->next; } - struct ListNode *result = dummy->next; + struct ListNode* result = dummy->next; free(dummy); return result; } From bb9981e7b4757a58f0d5c035ec769a467ab3bfb1 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 23 May 2025 20:16:53 +0800 Subject: [PATCH 05/19] Update Solution.c --- solution/0000-0099/0002.Add Two Numbers/Solution.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/solution/0000-0099/0002.Add Two Numbers/Solution.c b/solution/0000-0099/0002.Add Two Numbers/Solution.c index 08fd89cf9ded0..0ca00b4bd6752 100644 --- a/solution/0000-0099/0002.Add Two Numbers/Solution.c +++ b/solution/0000-0099/0002.Add Two Numbers/Solution.c @@ -1,3 +1,4 @@ + /** * Definition for singly-linked list. * struct ListNode { @@ -37,4 +38,4 @@ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode* result = dummy->next; free(dummy); return result; -} \ No newline at end of file +} From c05b7a5f5d666c2bf8618e610fd393c23d263f66 Mon Sep 17 00:00:00 2001 From: pranjal030404 Date: Fri, 23 May 2025 21:30:10 +0530 Subject: [PATCH 06/19] solution of 0003 done in c --- .../README.md | 28 ++++++++++++++++++ .../README_EN.md | 29 +++++++++++++++++++ .../Solution.c | 22 ++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.c diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index f502d2ad9b027..c2315cb3acd0d 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -300,6 +300,34 @@ class Solution { } ``` +#### C + +``` c +int lengthOfLongestSubstring(char *s) { + int freq[256] = {0}; + int l = 0, r = 0; + int ans = 0; + int len = strlen(s); + + for (r = 0; r < len; r++) { + char c = s[r]; + freq[(unsigned char)c]++; + + while (freq[(unsigned char)c] > 1) { + freq[(unsigned char)s[l]]--; + l++; + } + + if (ans < r - l + 1) { + ans = r - l + 1; + } + } + + return ans; +} + +``` + diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md index 69d92379be2a3..655fb8009f571 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md @@ -298,6 +298,35 @@ class Solution { } ``` +#### C + +```c + +int lengthOfLongestSubstring(char *s) { + int freq[256] = {0}; + int l = 0, r = 0; + int ans = 0; + int len = strlen(s); + + for (r = 0; r < len; r++) { + char c = s[r]; + freq[(unsigned char)c]++; + + while (freq[(unsigned char)c] > 1) { + freq[(unsigned char)s[l]]--; + l++; + } + + if (ans < r - l + 1) { + ans = r - l + 1; + } + } + + return ans; +} + +``` + diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.c b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.c new file mode 100644 index 0000000000000..5024b4aa63f85 --- /dev/null +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.c @@ -0,0 +1,22 @@ +int lengthOfLongestSubstring(char *s) { + int freq[256] = {0}; + int l = 0, r = 0; + int ans = 0; + int len = strlen(s); + + for (r = 0; r < len; r++) { + char c = s[r]; + freq[(unsigned char)c]++; + + while (freq[(unsigned char)c] > 1) { + freq[(unsigned char)s[l]]--; + l++; + } + + if (ans < r - l + 1) { + ans = r - l + 1; + } + } + + return ans; +} From bc3ef008c0b790f1726f6351f2cdb5408c54500c Mon Sep 17 00:00:00 2001 From: pranjal030404 Date: Fri, 23 May 2025 21:35:13 +0530 Subject: [PATCH 07/19] 0004 done in c language --- .../README.md | 30 +++++++++++++++++++ .../README_EN.md | 30 +++++++++++++++++++ .../Solution.c | 25 ++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.c diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md index a0c5af52f0caa..e6cd803ed7bc3 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md @@ -350,6 +350,36 @@ proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = # echo medianOfTwoSortedArrays(arrA, arrB) ``` +#### C + +```c +int findKth(int *nums1, int m, int i, int *nums2, int n, int j, int k) { + if (i >= m) + return nums2[j + k - 1]; + if (j >= n) + return nums1[i + k - 1]; + if (k == 1) + return nums1[i] < nums2[j] ? nums1[i] : nums2[j]; + + int p = k / 2; + + int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX; + int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX; + + if (x < y) + return findKth(nums1, m, i + p, nums2, n, j, k - p); + else + return findKth(nums1, m, i, nums2, n, j + p, k - p); +} + +double findMedianSortedArrays(int *nums1, int m, int *nums2, int n) { + int total = m + n; + int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2); + int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2); + return (a + b) / 2.0; +} +``` + diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md index 15b73ee6ff6f0..dbdb35f892f2d 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md @@ -346,6 +346,36 @@ proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = # echo medianOfTwoSortedArrays(arrA, arrB) ``` +#### C + +``` c +int findKth(int *nums1, int m, int i, int *nums2, int n, int j, int k) { + if (i >= m) + return nums2[j + k - 1]; + if (j >= n) + return nums1[i + k - 1]; + if (k == 1) + return nums1[i] < nums2[j] ? nums1[i] : nums2[j]; + + int p = k / 2; + + int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX; + int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX; + + if (x < y) + return findKth(nums1, m, i + p, nums2, n, j, k - p); + else + return findKth(nums1, m, i, nums2, n, j + p, k - p); +} + +double findMedianSortedArrays(int *nums1, int m, int *nums2, int n) { + int total = m + n; + int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2); + int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2); + return (a + b) / 2.0; +} +``` + diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.c b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.c new file mode 100644 index 0000000000000..4995edf2c1be7 --- /dev/null +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.c @@ -0,0 +1,25 @@ +int findKth(int *nums1, int m, int i, int *nums2, int n, int j, int k) { + if (i >= m) + return nums2[j + k - 1]; + if (j >= n) + return nums1[i + k - 1]; + if (k == 1) + return nums1[i] < nums2[j] ? nums1[i] : nums2[j]; + + int p = k / 2; + + int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX; + int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX; + + if (x < y) + return findKth(nums1, m, i + p, nums2, n, j, k - p); + else + return findKth(nums1, m, i, nums2, n, j + p, k - p); +} + +double findMedianSortedArrays(int *nums1, int m, int *nums2, int n) { + int total = m + n; + int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2); + int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2); + return (a + b) / 2.0; +} \ No newline at end of file From c154596d5daa9a95eb7d7c3c961f767b434dd963 Mon Sep 17 00:00:00 2001 From: pranjal030404 <182807087+pranjal030404@users.noreply.github.com> Date: Fri, 23 May 2025 16:16:08 +0000 Subject: [PATCH 08/19] style: format code and docs with prettier --- .../README.md | 2 +- .../README_EN.md | 2 +- .../0000-0099/0004.Median of Two Sorted Arrays/README_EN.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index c2315cb3acd0d..d0e50aebd708f 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -302,7 +302,7 @@ class Solution { #### C -``` c +```c int lengthOfLongestSubstring(char *s) { int freq[256] = {0}; int l = 0, r = 0; diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md index 655fb8009f571..7e14e65d4f0bc 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md @@ -300,7 +300,7 @@ class Solution { #### C -```c +```c int lengthOfLongestSubstring(char *s) { int freq[256] = {0}; diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md index dbdb35f892f2d..484169b1434df 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md @@ -348,7 +348,7 @@ proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = #### C -``` c +```c int findKth(int *nums1, int m, int i, int *nums2, int n, int j, int k) { if (i >= m) return nums2[j + k - 1]; From f6095308c565be9ec716eb897e0474723da7f70d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 26 May 2025 08:50:56 +0800 Subject: [PATCH 09/19] Update README.md --- .../README.md | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index d0e50aebd708f..d95ea8a5d77a9 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -303,29 +303,28 @@ class Solution { #### C ```c -int lengthOfLongestSubstring(char *s) { - int freq[256] = {0}; - int l = 0, r = 0; - int ans = 0; - int len = strlen(s); - - for (r = 0; r < len; r++) { - char c = s[r]; - freq[(unsigned char)c]++; - - while (freq[(unsigned char)c] > 1) { - freq[(unsigned char)s[l]]--; - l++; - } +int lengthOfLongestSubstring(char* s) { + int freq[256] = {0}; + int l = 0, r = 0; + int ans = 0; + int len = strlen(s); + + for (r = 0; r < len; r++) { + char c = s[r]; + freq[(unsigned char) c]++; + + while (freq[(unsigned char) c] > 1) { + freq[(unsigned char) s[l]]--; + l++; + } - if (ans < r - l + 1) { - ans = r - l + 1; + if (ans < r - l + 1) { + ans = r - l + 1; + } } - } - return ans; + return ans; } - ``` From 377d9dd00ed86b7e217770d5b4c11608e0145881 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 26 May 2025 08:51:12 +0800 Subject: [PATCH 10/19] Update README_EN.md --- .../README_EN.md | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md index 7e14e65d4f0bc..16624fcc16b80 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md @@ -301,30 +301,28 @@ class Solution { #### C ```c +int lengthOfLongestSubstring(char* s) { + int freq[256] = {0}; + int l = 0, r = 0; + int ans = 0; + int len = strlen(s); + + for (r = 0; r < len; r++) { + char c = s[r]; + freq[(unsigned char) c]++; + + while (freq[(unsigned char) c] > 1) { + freq[(unsigned char) s[l]]--; + l++; + } -int lengthOfLongestSubstring(char *s) { - int freq[256] = {0}; - int l = 0, r = 0; - int ans = 0; - int len = strlen(s); - - for (r = 0; r < len; r++) { - char c = s[r]; - freq[(unsigned char)c]++; - - while (freq[(unsigned char)c] > 1) { - freq[(unsigned char)s[l]]--; - l++; - } - - if (ans < r - l + 1) { - ans = r - l + 1; + if (ans < r - l + 1) { + ans = r - l + 1; + } } - } - return ans; + return ans; } - ``` From 60538706faa6815ae547d057aca8caaff4547ead Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 26 May 2025 08:51:27 +0800 Subject: [PATCH 11/19] Update Solution.c --- .../Solution.c | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.c b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.c index 5024b4aa63f85..673e098af92ac 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.c +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.c @@ -1,22 +1,22 @@ -int lengthOfLongestSubstring(char *s) { - int freq[256] = {0}; - int l = 0, r = 0; - int ans = 0; - int len = strlen(s); +int lengthOfLongestSubstring(char* s) { + int freq[256] = {0}; + int l = 0, r = 0; + int ans = 0; + int len = strlen(s); - for (r = 0; r < len; r++) { - char c = s[r]; - freq[(unsigned char)c]++; + for (r = 0; r < len; r++) { + char c = s[r]; + freq[(unsigned char) c]++; - while (freq[(unsigned char)c] > 1) { - freq[(unsigned char)s[l]]--; - l++; - } + while (freq[(unsigned char) c] > 1) { + freq[(unsigned char) s[l]]--; + l++; + } - if (ans < r - l + 1) { - ans = r - l + 1; + if (ans < r - l + 1) { + ans = r - l + 1; + } } - } - return ans; + return ans; } From 77e281bc01dd71919bd15f89700df77e588e7b0c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 26 May 2025 08:52:56 +0800 Subject: [PATCH 12/19] Update README.md --- .../README.md | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md index e6cd803ed7bc3..b6a21c5b7e751 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md @@ -353,30 +353,30 @@ proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = #### C ```c -int findKth(int *nums1, int m, int i, int *nums2, int n, int j, int k) { - if (i >= m) - return nums2[j + k - 1]; - if (j >= n) - return nums1[i + k - 1]; - if (k == 1) - return nums1[i] < nums2[j] ? nums1[i] : nums2[j]; - - int p = k / 2; - - int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX; - int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX; - - if (x < y) - return findKth(nums1, m, i + p, nums2, n, j, k - p); - else - return findKth(nums1, m, i, nums2, n, j + p, k - p); +int findKth(int* nums1, int m, int i, int* nums2, int n, int j, int k) { + if (i >= m) + return nums2[j + k - 1]; + if (j >= n) + return nums1[i + k - 1]; + if (k == 1) + return nums1[i] < nums2[j] ? nums1[i] : nums2[j]; + + int p = k / 2; + + int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX; + int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX; + + if (x < y) + return findKth(nums1, m, i + p, nums2, n, j, k - p); + else + return findKth(nums1, m, i, nums2, n, j + p, k - p); } -double findMedianSortedArrays(int *nums1, int m, int *nums2, int n) { - int total = m + n; - int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2); - int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2); - return (a + b) / 2.0; +double findMedianSortedArrays(int* nums1, int m, int* nums2, int n) { + int total = m + n; + int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2); + int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2); + return (a + b) / 2.0; } ``` From 90bb3044ce3c3f7346fdc1216f5f6255fbc27e9a Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 26 May 2025 08:53:12 +0800 Subject: [PATCH 13/19] Update README_EN.md --- .../README_EN.md | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md index 484169b1434df..2896e96ddc3af 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md @@ -349,30 +349,30 @@ proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = #### C ```c -int findKth(int *nums1, int m, int i, int *nums2, int n, int j, int k) { - if (i >= m) - return nums2[j + k - 1]; - if (j >= n) - return nums1[i + k - 1]; - if (k == 1) - return nums1[i] < nums2[j] ? nums1[i] : nums2[j]; - - int p = k / 2; - - int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX; - int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX; - - if (x < y) - return findKth(nums1, m, i + p, nums2, n, j, k - p); - else - return findKth(nums1, m, i, nums2, n, j + p, k - p); +int findKth(int* nums1, int m, int i, int* nums2, int n, int j, int k) { + if (i >= m) + return nums2[j + k - 1]; + if (j >= n) + return nums1[i + k - 1]; + if (k == 1) + return nums1[i] < nums2[j] ? nums1[i] : nums2[j]; + + int p = k / 2; + + int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX; + int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX; + + if (x < y) + return findKth(nums1, m, i + p, nums2, n, j, k - p); + else + return findKth(nums1, m, i, nums2, n, j + p, k - p); } -double findMedianSortedArrays(int *nums1, int m, int *nums2, int n) { - int total = m + n; - int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2); - int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2); - return (a + b) / 2.0; +double findMedianSortedArrays(int* nums1, int m, int* nums2, int n) { + int total = m + n; + int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2); + int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2); + return (a + b) / 2.0; } ``` From 63a213f06e5db54c84a76bbebd55691fd30e2574 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 26 May 2025 08:53:28 +0800 Subject: [PATCH 14/19] Update Solution.c --- .../Solution.c | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.c b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.c index 4995edf2c1be7..2786c7ef9bfd8 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.c +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.c @@ -1,25 +1,25 @@ -int findKth(int *nums1, int m, int i, int *nums2, int n, int j, int k) { - if (i >= m) - return nums2[j + k - 1]; - if (j >= n) - return nums1[i + k - 1]; - if (k == 1) - return nums1[i] < nums2[j] ? nums1[i] : nums2[j]; +int findKth(int* nums1, int m, int i, int* nums2, int n, int j, int k) { + if (i >= m) + return nums2[j + k - 1]; + if (j >= n) + return nums1[i + k - 1]; + if (k == 1) + return nums1[i] < nums2[j] ? nums1[i] : nums2[j]; - int p = k / 2; + int p = k / 2; - int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX; - int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX; + int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX; + int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX; - if (x < y) - return findKth(nums1, m, i + p, nums2, n, j, k - p); - else - return findKth(nums1, m, i, nums2, n, j + p, k - p); + if (x < y) + return findKth(nums1, m, i + p, nums2, n, j, k - p); + else + return findKth(nums1, m, i, nums2, n, j + p, k - p); } -double findMedianSortedArrays(int *nums1, int m, int *nums2, int n) { - int total = m + n; - int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2); - int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2); - return (a + b) / 2.0; -} \ No newline at end of file +double findMedianSortedArrays(int* nums1, int m, int* nums2, int n) { + int total = m + n; + int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2); + int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2); + return (a + b) / 2.0; +} From c20743725136b467890b64f01052fba4a84345a2 Mon Sep 17 00:00:00 2001 From: pranjal030404 Date: Tue, 3 Jun 2025 12:35:22 +0530 Subject: [PATCH 15/19] 0005 and 0006 done in c language --- .../README.md | 50 +++++++++++++++++++ .../README_EN.md | 49 ++++++++++++++++++ .../Solution.c | 45 +++++++++++++++++ .../0006.Zigzag Conversion/README.md | 34 +++++++++++++ .../0006.Zigzag Conversion/README_EN.md | 34 +++++++++++++ .../0006.Zigzag Conversion/Solution.c | 29 +++++++++++ 6 files changed, 241 insertions(+) create mode 100644 solution/0000-0099/0005.Longest Palindromic Substring/Solution.c create mode 100644 solution/0000-0099/0006.Zigzag Conversion/Solution.c diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README.md b/solution/0000-0099/0005.Longest Palindromic Substring/README.md index 11a6b7df01a67..be99158e336bb 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README.md @@ -527,6 +527,56 @@ class Solution { } ``` +#### c + +```c +char *longestPalindrome(char *s) { + int n = strlen(s); + if (n == 0) { + char *result = malloc(1); + result[0] = '\0'; + return result; + } + + bool dp[n][n]; + memset(dp, 0, sizeof(dp)); + + int start = 0, max_len = 1; + + for (int i = 0; i < n; i++) { + dp[i][i] = true; + } + + for (int i = 0; i < n - 1; i++) { + if (s[i] == s[i + 1]) { + dp[i][i + 1] = true; + start = i; + max_len = 2; + } + } + + // Check for lengths > 2 + for (int len = 3; len <= n; len++) { + for (int i = 0; i < n - len + 1; i++) { + int j = i + len - 1; + if (s[i] == s[j] && dp[i + 1][j - 1]) { + dp[i][j] = true; + if (len > max_len) { + start = i; + max_len = len; + } + } + } + } + char *result = malloc(max_len + 1); + strncpy(result, s + start, max_len); + result[max_len] = '\0'; + return result; +} + +``` + + diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md index 4ac2610fb4e8e..d18a7eac62fde 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md @@ -525,6 +525,55 @@ class Solution { } ``` +#### C + +``` c +char *longestPalindrome(char *s) { + int n = strlen(s); + if (n == 0) { + char *result = malloc(1); + result[0] = '\0'; + return result; + } + + bool dp[n][n]; + memset(dp, 0, sizeof(dp)); + + int start = 0, max_len = 1; + + for (int i = 0; i < n; i++) { + dp[i][i] = true; + } + + for (int i = 0; i < n - 1; i++) { + if (s[i] == s[i + 1]) { + dp[i][i + 1] = true; + start = i; + max_len = 2; + } + } + + // Check for lengths > 2 + for (int len = 3; len <= n; len++) { + for (int i = 0; i < n - len + 1; i++) { + int j = i + len - 1; + if (s[i] == s[j] && dp[i + 1][j - 1]) { + dp[i][j] = true; + if (len > max_len) { + start = i; + max_len = len; + } + } + } + } + char *result = malloc(max_len + 1); + strncpy(result, s + start, max_len); + result[max_len] = '\0'; + return result; +} + +``` + diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.c b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.c new file mode 100644 index 0000000000000..bd3296f1505a5 --- /dev/null +++ b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.c @@ -0,0 +1,45 @@ + + +char *longestPalindrome(char *s) { + int n = strlen(s); + if (n == 0) { + char *result = malloc(1); + result[0] = '\0'; + return result; + } + + bool dp[n][n]; + memset(dp, 0, sizeof(dp)); + + int start = 0, max_len = 1; + + for (int i = 0; i < n; i++) { + dp[i][i] = true; + } + + for (int i = 0; i < n - 1; i++) { + if (s[i] == s[i + 1]) { + dp[i][i + 1] = true; + start = i; + max_len = 2; + } + } + + // Check for lengths > 2 + for (int len = 3; len <= n; len++) { + for (int i = 0; i < n - len + 1; i++) { + int j = i + len - 1; + if (s[i] == s[j] && dp[i + 1][j - 1]) { + dp[i][j] = true; + if (len > max_len) { + start = i; + max_len = len; + } + } + } + } + char *result = malloc(max_len + 1); + strncpy(result, s + start, max_len); + result[max_len] = '\0'; + return result; +} diff --git a/solution/0000-0099/0006.Zigzag Conversion/README.md b/solution/0000-0099/0006.Zigzag Conversion/README.md index c12bad03a22aa..7b6d40a253480 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README.md @@ -493,6 +493,40 @@ class Solution { } ``` +#### C + +``` C +char *convert(char *s, int numRows) { + if (numRows == 1 || numRows >= strlen(s)) { + char *result = malloc(strlen(s) + 1); + strcpy(result, s); + return result; + } + char **rows = malloc(numRows * sizeof(char *)); + for (int i = 0; i < numRows; i++) { + rows[i] = malloc(strlen(s) + 1); + rows[i][0] = '\0'; + } + int currentRow = 0; + int goingDown = 0; + for (int i = 0; s[i] != '\0'; i++) { + strncat(rows[currentRow], &s[i], 1); + if (currentRow == 0 || currentRow == numRows - 1) { + goingDown = !goingDown; + } + currentRow += goingDown ? 1 : -1; + } + char *result = malloc(strlen(s) + 1); + result[0] = '\0'; + for (int i = 0; i < numRows; i++) { + strcat(result, rows[i]); + free(rows[i]); + } + free(rows); + return result; +} +``` + diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md index 04390da8f24e7..915a60092e575 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md @@ -491,6 +491,40 @@ class Solution { } ``` +#### + +```c +char *convert(char *s, int numRows) { + if (numRows == 1 || numRows >= strlen(s)) { + char *result = malloc(strlen(s) + 1); + strcpy(result, s); + return result; + } + char **rows = malloc(numRows * sizeof(char *)); + for (int i = 0; i < numRows; i++) { + rows[i] = malloc(strlen(s) + 1); + rows[i][0] = '\0'; + } + int currentRow = 0; + int goingDown = 0; + for (int i = 0; s[i] != '\0'; i++) { + strncat(rows[currentRow], &s[i], 1); + if (currentRow == 0 || currentRow == numRows - 1) { + goingDown = !goingDown; + } + currentRow += goingDown ? 1 : -1; + } + char *result = malloc(strlen(s) + 1); + result[0] = '\0'; + for (int i = 0; i < numRows; i++) { + strcat(result, rows[i]); + free(rows[i]); + } + free(rows); + return result; +} +``` + diff --git a/solution/0000-0099/0006.Zigzag Conversion/Solution.c b/solution/0000-0099/0006.Zigzag Conversion/Solution.c new file mode 100644 index 0000000000000..0c23f5da5b410 --- /dev/null +++ b/solution/0000-0099/0006.Zigzag Conversion/Solution.c @@ -0,0 +1,29 @@ +char *convert(char *s, int numRows) { + if (numRows == 1 || numRows >= strlen(s)) { + char *result = malloc(strlen(s) + 1); + strcpy(result, s); + return result; + } + char **rows = malloc(numRows * sizeof(char *)); + for (int i = 0; i < numRows; i++) { + rows[i] = malloc(strlen(s) + 1); + rows[i][0] = '\0'; + } + int currentRow = 0; + int goingDown = 0; + for (int i = 0; s[i] != '\0'; i++) { + strncat(rows[currentRow], &s[i], 1); + if (currentRow == 0 || currentRow == numRows - 1) { + goingDown = !goingDown; + } + currentRow += goingDown ? 1 : -1; + } + char *result = malloc(strlen(s) + 1); + result[0] = '\0'; + for (int i = 0; i < numRows; i++) { + strcat(result, rows[i]); + free(rows[i]); + } + free(rows); + return result; +} \ No newline at end of file From db6e8f15bec24b21dd3fdba4123c85543137c0e5 Mon Sep 17 00:00:00 2001 From: pranjal030404 <182807087+pranjal030404@users.noreply.github.com> Date: Tue, 3 Jun 2025 07:09:34 +0000 Subject: [PATCH 16/19] style: format code and docs with prettier --- solution/0000-0099/0005.Longest Palindromic Substring/README.md | 1 - .../0000-0099/0005.Longest Palindromic Substring/README_EN.md | 2 +- solution/0000-0099/0006.Zigzag Conversion/README.md | 2 +- solution/0000-0099/0006.Zigzag Conversion/README_EN.md | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README.md b/solution/0000-0099/0005.Longest Palindromic Substring/README.md index be99158e336bb..e4a000d7aa164 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README.md @@ -576,7 +576,6 @@ char *longestPalindrome(char *s) { ``` - diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md index d18a7eac62fde..8b39b068c345c 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md @@ -527,7 +527,7 @@ class Solution { #### C -``` c +```c char *longestPalindrome(char *s) { int n = strlen(s); if (n == 0) { diff --git a/solution/0000-0099/0006.Zigzag Conversion/README.md b/solution/0000-0099/0006.Zigzag Conversion/README.md index 7b6d40a253480..172268afc5ed9 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README.md @@ -495,7 +495,7 @@ class Solution { #### C -``` C +```C char *convert(char *s, int numRows) { if (numRows == 1 || numRows >= strlen(s)) { char *result = malloc(strlen(s) + 1); diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md index 915a60092e575..45bce03f21a1e 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md @@ -491,7 +491,7 @@ class Solution { } ``` -#### +#### ```c char *convert(char *s, int numRows) { From 7c546ef8fea10585e82ea71d3b5b7eeb39d2af2b Mon Sep 17 00:00:00 2001 From: pranjal030404 Date: Tue, 3 Jun 2025 16:01:54 +0530 Subject: [PATCH 17/19] c added --- solution/0000-0099/0006.Zigzag Conversion/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md index 915a60092e575..9e86d8fd52ce2 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md @@ -491,7 +491,7 @@ class Solution { } ``` -#### +#### c ```c char *convert(char *s, int numRows) { From f4cc2cb725310890bc56403c768d4f0a0a990eb1 Mon Sep 17 00:00:00 2001 From: pranjal030404 <182807087+pranjal030404@users.noreply.github.com> Date: Tue, 3 Jun 2025 10:43:41 +0000 Subject: [PATCH 18/19] style: format code and docs with prettier --- solution/0000-0099/0006.Zigzag Conversion/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md index 915a60092e575..45bce03f21a1e 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md @@ -491,7 +491,7 @@ class Solution { } ``` -#### +#### ```c char *convert(char *s, int numRows) { From 7cbf92b8adb29b6e71377df9e73b0e8eeb469602 Mon Sep 17 00:00:00 2001 From: pranjal030404 Date: Tue, 3 Jun 2025 16:26:28 +0530 Subject: [PATCH 19/19] formatted --- .../Solution.c | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.c b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.c index bd3296f1505a5..26592c3a40353 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.c +++ b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.c @@ -1,45 +1,45 @@ -char *longestPalindrome(char *s) { - int n = strlen(s); - if (n == 0) { - char *result = malloc(1); - result[0] = '\0'; - return result; - } +char* longestPalindrome(char* s) { + int n = strlen(s); + if (n == 0) { + char* result = malloc(1); + result[0] = '\0'; + return result; + } - bool dp[n][n]; - memset(dp, 0, sizeof(dp)); + bool dp[n][n]; + memset(dp, 0, sizeof(dp)); - int start = 0, max_len = 1; + int start = 0, max_len = 1; - for (int i = 0; i < n; i++) { - dp[i][i] = true; - } + for (int i = 0; i < n; i++) { + dp[i][i] = true; + } - for (int i = 0; i < n - 1; i++) { - if (s[i] == s[i + 1]) { - dp[i][i + 1] = true; - start = i; - max_len = 2; + for (int i = 0; i < n - 1; i++) { + if (s[i] == s[i + 1]) { + dp[i][i + 1] = true; + start = i; + max_len = 2; + } } - } - // Check for lengths > 2 - for (int len = 3; len <= n; len++) { - for (int i = 0; i < n - len + 1; i++) { - int j = i + len - 1; - if (s[i] == s[j] && dp[i + 1][j - 1]) { - dp[i][j] = true; - if (len > max_len) { - start = i; - max_len = len; + // Check for lengths > 2 + for (int len = 3; len <= n; len++) { + for (int i = 0; i < n - len + 1; i++) { + int j = i + len - 1; + if (s[i] == s[j] && dp[i + 1][j - 1]) { + dp[i][j] = true; + if (len > max_len) { + start = i; + max_len = len; + } + } } - } } - } - char *result = malloc(max_len + 1); - strncpy(result, s + start, max_len); - result[max_len] = '\0'; - return result; + char* result = malloc(max_len + 1); + strncpy(result, s + start, max_len); + result[max_len] = '\0'; + return result; }