Skip to content

Solution of 0003 and 0004 in c language and readme done #4427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 30 additions & 0 deletions solution/0000-0099/0004.Median of Two Sorted Arrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
30 changes: 30 additions & 0 deletions solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
25 changes: 25 additions & 0 deletions solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.c
Original file line number Diff line number Diff line change
@@ -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;
}