Skip to content

Commit c40b4b8

Browse files
Improvement
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 72226ca commit c40b4b8

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

0058_length_of_last_word/word_length.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
#include <string.h>
34

45
int 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

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

0070_climbing_stairs/climb_stairs.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,25 @@
44

55
static 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

2220
static 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;

0083_remove_duplicates_from_sorted_list/rm_dup.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <limits.h>
12
#include <stdio.h>
23
#include <stdlib.h>
34

@@ -9,16 +10,19 @@ struct ListNode {
910

1011
struct 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

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

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