Skip to content

Commit b2a6267

Browse files
feat: add c solutions to lc problems: No.0003,0004 (#4427)
1 parent 3e3d06c commit b2a6267

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,33 @@ class Solution {
300300
}
301301
```
302302

303+
#### C
304+
305+
```c
306+
int lengthOfLongestSubstring(char* s) {
307+
int freq[256] = {0};
308+
int l = 0, r = 0;
309+
int ans = 0;
310+
int len = strlen(s);
311+
312+
for (r = 0; r < len; r++) {
313+
char c = s[r];
314+
freq[(unsigned char) c]++;
315+
316+
while (freq[(unsigned char) c] > 1) {
317+
freq[(unsigned char) s[l]]--;
318+
l++;
319+
}
320+
321+
if (ans < r - l + 1) {
322+
ans = r - l + 1;
323+
}
324+
}
325+
326+
return ans;
327+
}
328+
```
329+
303330
<!-- tabs:end -->
304331
305332
<!-- solution:end -->

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,33 @@ class Solution {
298298
}
299299
```
300300

301+
#### C
302+
303+
```c
304+
int lengthOfLongestSubstring(char* s) {
305+
int freq[256] = {0};
306+
int l = 0, r = 0;
307+
int ans = 0;
308+
int len = strlen(s);
309+
310+
for (r = 0; r < len; r++) {
311+
char c = s[r];
312+
freq[(unsigned char) c]++;
313+
314+
while (freq[(unsigned char) c] > 1) {
315+
freq[(unsigned char) s[l]]--;
316+
l++;
317+
}
318+
319+
if (ans < r - l + 1) {
320+
ans = r - l + 1;
321+
}
322+
}
323+
324+
return ans;
325+
}
326+
```
327+
301328
<!-- tabs:end -->
302329
303330
<!-- solution:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
int lengthOfLongestSubstring(char* s) {
2+
int freq[256] = {0};
3+
int l = 0, r = 0;
4+
int ans = 0;
5+
int len = strlen(s);
6+
7+
for (r = 0; r < len; r++) {
8+
char c = s[r];
9+
freq[(unsigned char) c]++;
10+
11+
while (freq[(unsigned char) c] > 1) {
12+
freq[(unsigned char) s[l]]--;
13+
l++;
14+
}
15+
16+
if (ans < r - l + 1) {
17+
ans = r - l + 1;
18+
}
19+
}
20+
21+
return ans;
22+
}

solution/0000-0099/0004.Median of Two Sorted Arrays/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,36 @@ proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float =
350350
# echo medianOfTwoSortedArrays(arrA, arrB)
351351
```
352352

353+
#### C
354+
355+
```c
356+
int findKth(int* nums1, int m, int i, int* nums2, int n, int j, int k) {
357+
if (i >= m)
358+
return nums2[j + k - 1];
359+
if (j >= n)
360+
return nums1[i + k - 1];
361+
if (k == 1)
362+
return nums1[i] < nums2[j] ? nums1[i] : nums2[j];
363+
364+
int p = k / 2;
365+
366+
int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX;
367+
int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX;
368+
369+
if (x < y)
370+
return findKth(nums1, m, i + p, nums2, n, j, k - p);
371+
else
372+
return findKth(nums1, m, i, nums2, n, j + p, k - p);
373+
}
374+
375+
double findMedianSortedArrays(int* nums1, int m, int* nums2, int n) {
376+
int total = m + n;
377+
int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2);
378+
int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2);
379+
return (a + b) / 2.0;
380+
}
381+
```
382+
353383
<!-- tabs:end -->
354384
355385
<!-- solution:end -->

solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,36 @@ proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float =
346346
# echo medianOfTwoSortedArrays(arrA, arrB)
347347
```
348348

349+
#### C
350+
351+
```c
352+
int findKth(int* nums1, int m, int i, int* nums2, int n, int j, int k) {
353+
if (i >= m)
354+
return nums2[j + k - 1];
355+
if (j >= n)
356+
return nums1[i + k - 1];
357+
if (k == 1)
358+
return nums1[i] < nums2[j] ? nums1[i] : nums2[j];
359+
360+
int p = k / 2;
361+
362+
int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX;
363+
int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX;
364+
365+
if (x < y)
366+
return findKth(nums1, m, i + p, nums2, n, j, k - p);
367+
else
368+
return findKth(nums1, m, i, nums2, n, j + p, k - p);
369+
}
370+
371+
double findMedianSortedArrays(int* nums1, int m, int* nums2, int n) {
372+
int total = m + n;
373+
int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2);
374+
int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2);
375+
return (a + b) / 2.0;
376+
}
377+
```
378+
349379
<!-- tabs:end -->
350380
351381
<!-- solution:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
int findKth(int* nums1, int m, int i, int* nums2, int n, int j, int k) {
2+
if (i >= m)
3+
return nums2[j + k - 1];
4+
if (j >= n)
5+
return nums1[i + k - 1];
6+
if (k == 1)
7+
return nums1[i] < nums2[j] ? nums1[i] : nums2[j];
8+
9+
int p = k / 2;
10+
11+
int x = (i + p - 1 < m) ? nums1[i + p - 1] : INT_MAX;
12+
int y = (j + p - 1 < n) ? nums2[j + p - 1] : INT_MAX;
13+
14+
if (x < y)
15+
return findKth(nums1, m, i + p, nums2, n, j, k - p);
16+
else
17+
return findKth(nums1, m, i, nums2, n, j + p, k - p);
18+
}
19+
20+
double findMedianSortedArrays(int* nums1, int m, int* nums2, int n) {
21+
int total = m + n;
22+
int a = findKth(nums1, m, 0, nums2, n, 0, (total + 1) / 2);
23+
int b = findKth(nums1, m, 0, nums2, n, 0, (total + 2) / 2);
24+
return (a + b) / 2.0;
25+
}

0 commit comments

Comments
 (0)