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..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 @@ -300,6 +300,33 @@ 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..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 @@ -298,6 +298,33 @@ 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..673e098af92ac --- /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; +} 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..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 @@ -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..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 @@ -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..2786c7ef9bfd8 --- /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; +}