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 11#include <stdio.h>
22#include <stdlib.h>
3+ #include <string.h>
34
45int lengthOfLastWord (char * s )
56{
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 -- ;
1415 }
15- return len ;
16+
17+ return word_len ;
1618}
1719
1820int main (int argc , char * * argv )
Original file line number Diff line number Diff line change 44
55static int dfs (int n , int * count )
66{
7- if (n == 0 ) {
8- return 0 ;
7+ if (n == 1 ) {
8+ return 1 ;
9+ } else if (n == 2 ) {
10+ return 2 ;
911 } else if (count [n ] > 0 ) {
1012 return count [n ];
1113 } 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 );
1816 return count [n ];
1917 }
2018}
2119
2220static int climbStairs (int n )
2321{
24- #if 0
22+ #if 1
23+ if (n < 1 ) return 0 ;
2524 int * count = malloc ((n + 1 ) * sizeof (int ));
2625 memset (count , 0 , (n + 1 ) * sizeof (int ));
27- count [1 ] = 1 ;
28- count [2 ] = 2 ;
2926 return dfs (n , count );
3027#else
3128 int i , a = 1 , b = 2 , c ;
Original file line number Diff line number Diff line change 1+ #include <limits.h>
12#include <stdio.h>
23#include <stdlib.h>
34
@@ -9,16 +10,19 @@ struct ListNode {
910
1011struct ListNode * deleteDuplicates (struct ListNode * head )
1112{
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 ;
1721 }
18- p -> next = q ;
19- p = q ;
22+ head = head -> next ;
2023 }
21- return head ;
24+ prev -> next = head ;
25+ return dummy .next ;
2226}
2327
2428int main (int argc , char * * argv )
You can’t perform that action at this time.
0 commit comments