Skip to content

Commit a01d82f

Browse files
Improvement
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 5af6c04 commit a01d82f

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

0026_remove_duplicates_from_sorted_array/rm_dup.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4+
45
static int removeDuplicates(int* nums, int numsSize)
56
{
6-
if (numsSize <= 1) {
7-
return numsSize;
8-
}
9-
10-
int i, count = 1;
7+
int i, size = 0;
118
for (i = 1; i < numsSize; i++) {
12-
if (nums[i - 1] != nums[i]) {
13-
nums[count++] = nums[i];
9+
if (nums[size] != nums[i]) {
10+
nums[++size] = nums[i];
1411
}
1512
}
1613

17-
return count;
14+
return size + 1;
1815
}
1916

2017
int main(int argc, char **argv)

0026_remove_duplicates_from_sorted_array/rm_dup.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ using namespace std;
55
class Solution {
66
public:
77
int removeDuplicates(vector<int>& nums) {
8-
if (nums.size() == 0) {
9-
return 0;
10-
}
11-
12-
int count = 1;
8+
int size = 0;
139
for (int i = 1; i < nums.size(); i++) {
14-
if (nums[i - 1] != nums[i]) {
15-
nums[count++] = nums[i];
10+
if (nums[size] != nums[i]) {
11+
nums[++size] = nums[i];
1612
}
1713
}
18-
return count;
14+
15+
return size + 1;
1916
}
2017
};

0070_climbing_stairs/climb_stairs.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5-
static int dfs(int n, int *count)
5+
static int dfs(int n, int *steps)
66
{
77
if (n == 1) {
88
return 1;
99
} else if (n == 2) {
1010
return 2;
11-
} else if (count[n] > 0) {
12-
return count[n];
11+
} else if (steps[n] > 0) {
12+
return steps[n];
1313
} else {
14-
count[n] += dfs(n - 1, count);
15-
count[n] += dfs(n - 2, count);
16-
return count[n];
14+
steps[n] += dfs(n - 1, steps);
15+
steps[n] += dfs(n - 2, steps);
16+
return steps[n];
1717
}
1818
}
1919

2020
static int climbStairs(int n)
2121
{
2222
#if 1
2323
if (n < 1) return 0;
24-
int *count = malloc((n + 1) * sizeof(int));
25-
memset(count, 0, (n + 1) * sizeof(int));
26-
return dfs(n, count);
24+
int *steps = malloc((n + 1) * sizeof(int));
25+
memset(steps, 0, (n + 1) * sizeof(int));
26+
return dfs(n, steps);
2727
#else
2828
int i, a = 1, b = 2, c;
2929
for (i = 3; i <= n; i++) {

0076_minimum_window_substring/window_substring.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static char *minWindow(char *s, char *t)
2222
int l = 0, r = 0;
2323
int min_len = slen + 1;
2424
int start = 0;
25-
int chars_to_meet = 0;
25+
int len = 0;
2626

2727
for (i = 0; i < tlen; i++) {
2828
count[t[i]]++;
@@ -31,18 +31,18 @@ static char *minWindow(char *s, char *t)
3131
while (r < slen) {
3232
if (--count[s[r++]] >= 0) {
3333
/* pattern found */
34-
chars_to_meet++;
34+
len++;
3535
}
3636

37-
while (chars_to_meet == tlen) {
37+
while (len >= tlen) {
3838
if (r - l < min_len) {
3939
min_len = r - l;
4040
start = l;
4141
}
4242

4343
/* Chars with negative count are not included in the pattern string */
4444
if (++count[s[l++]] > 0) {
45-
chars_to_meet--;
45+
len--;
4646
}
4747
}
4848
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy