File tree Expand file tree Collapse file tree 3 files changed +31
-28
lines changed
0083_remove_duplicates_from_sorted_list Expand file tree Collapse file tree 3 files changed +31
-28
lines changed Original file line number Diff line number Diff line change 1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
+ #include <string.h>
3
4
4
5
int lengthOfLastWord (char * s )
5
6
{
6
- int len = 0 ;
7
- while ( * s != '\0' ) {
8
- if ( s [ -1 ] == ' ' && s [ 0 ] != ' ' ) {
9
- len = 1 ;
10
- } else if ( * s != ' ' ) {
11
- len ++ ;
12
- }
13
- s ++ ;
7
+ int word_len = 0 ;
8
+ int len = strlen ( s );
9
+
10
+ while ( len > 0 && s [ -- len ] == ' ' ) {}
11
+
12
+ while ( len >= 0 && s [ len ] != ' ' ) {
13
+ word_len ++ ;
14
+ len -- ;
14
15
}
15
- return len ;
16
+
17
+ return word_len ;
16
18
}
17
19
18
20
int main (int argc , char * * argv )
Original file line number Diff line number Diff line change 4
4
5
5
static int dfs (int n , int * count )
6
6
{
7
- if (n == 0 ) {
8
- return 0 ;
7
+ if (n == 1 ) {
8
+ return 1 ;
9
+ } else if (n == 2 ) {
10
+ return 2 ;
9
11
} else if (count [n ] > 0 ) {
10
12
return count [n ];
11
13
} else {
12
- if (n >= 1 ) {
13
- count [n ] += dfs (n - 1 , count );
14
- }
15
- if (n >= 2 ) {
16
- count [n ] += dfs (n - 2 , count );
17
- }
14
+ count [n ] += dfs (n - 1 , count );
15
+ count [n ] += dfs (n - 2 , count );
18
16
return count [n ];
19
17
}
20
18
}
21
19
22
20
static int climbStairs (int n )
23
21
{
24
- #if 0
22
+ #if 1
23
+ if (n < 1 ) return 0 ;
25
24
int * count = malloc ((n + 1 ) * sizeof (int ));
26
25
memset (count , 0 , (n + 1 ) * sizeof (int ));
27
- count [1 ] = 1 ;
28
- count [2 ] = 2 ;
29
26
return dfs (n , count );
30
27
#else
31
28
int i , a = 1 , b = 2 , c ;
Original file line number Diff line number Diff line change
1
+ #include <limits.h>
1
2
#include <stdio.h>
2
3
#include <stdlib.h>
3
4
@@ -9,16 +10,19 @@ struct ListNode {
9
10
10
11
struct ListNode * deleteDuplicates (struct ListNode * head )
11
12
{
12
- struct ListNode * p , * q ;
13
- p = q = head ;
14
- while (p != NULL ) {
15
- while (q != NULL && q -> val == p -> val ) {
16
- q = q -> next ;
13
+ struct ListNode dummy ;
14
+ struct ListNode * prev = & dummy ;
15
+ dummy .val = INT_MIN ;
16
+
17
+ while (head != NULL ) {
18
+ if (prev -> val != head -> val ) {
19
+ prev -> next = head ;
20
+ prev = head ;
17
21
}
18
- p -> next = q ;
19
- p = q ;
22
+ head = head -> next ;
20
23
}
21
- return head ;
24
+ prev -> next = head ;
25
+ return dummy .next ;
22
26
}
23
27
24
28
int main (int argc , char * * argv )
You can’t perform that action at this time.
0 commit comments