File tree 4 files changed +23
-29
lines changed
0026_remove_duplicates_from_sorted_array
0076_minimum_window_substring
4 files changed +23
-29
lines changed Original file line number Diff line number Diff line change 1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
4
+
4
5
static int removeDuplicates (int * nums , int numsSize )
5
6
{
6
- if (numsSize <= 1 ) {
7
- return numsSize ;
8
- }
9
-
10
- int i , count = 1 ;
7
+ int i , size = 0 ;
11
8
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 ];
14
11
}
15
12
}
16
13
17
- return count ;
14
+ return size + 1 ;
18
15
}
19
16
20
17
int main (int argc , char * * argv )
Original file line number Diff line number Diff line change @@ -5,16 +5,13 @@ using namespace std;
5
5
class Solution {
6
6
public:
7
7
int removeDuplicates (vector<int >& nums) {
8
- if (nums.size () == 0 ) {
9
- return 0 ;
10
- }
11
-
12
- int count = 1 ;
8
+ int size = 0 ;
13
9
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];
16
12
}
17
13
}
18
- return count;
14
+
15
+ return size + 1 ;
19
16
}
20
17
};
Original file line number Diff line number Diff line change 2
2
#include <stdlib.h>
3
3
#include <string.h>
4
4
5
- static int dfs (int n , int * count )
5
+ static int dfs (int n , int * steps )
6
6
{
7
7
if (n == 1 ) {
8
8
return 1 ;
9
9
} else if (n == 2 ) {
10
10
return 2 ;
11
- } else if (count [n ] > 0 ) {
12
- return count [n ];
11
+ } else if (steps [n ] > 0 ) {
12
+ return steps [n ];
13
13
} 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 ];
17
17
}
18
18
}
19
19
20
20
static int climbStairs (int n )
21
21
{
22
22
#if 1
23
23
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 );
27
27
#else
28
28
int i , a = 1 , b = 2 , c ;
29
29
for (i = 3 ; i <= n ; i ++ ) {
Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ static char *minWindow(char *s, char *t)
22
22
int l = 0 , r = 0 ;
23
23
int min_len = slen + 1 ;
24
24
int start = 0 ;
25
- int chars_to_meet = 0 ;
25
+ int len = 0 ;
26
26
27
27
for (i = 0 ; i < tlen ; i ++ ) {
28
28
count [t [i ]]++ ;
@@ -31,18 +31,18 @@ static char *minWindow(char *s, char *t)
31
31
while (r < slen ) {
32
32
if (-- count [s [r ++ ]] >= 0 ) {
33
33
/* pattern found */
34
- chars_to_meet ++ ;
34
+ len ++ ;
35
35
}
36
36
37
- while (chars_to_meet = = tlen ) {
37
+ while (len > = tlen ) {
38
38
if (r - l < min_len ) {
39
39
min_len = r - l ;
40
40
start = l ;
41
41
}
42
42
43
43
/* Chars with negative count are not included in the pattern string */
44
44
if (++ count [s [l ++ ]] > 0 ) {
45
- chars_to_meet -- ;
45
+ len -- ;
46
46
}
47
47
}
48
48
}
You can’t perform that action at this time.
0 commit comments