Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc40b4b8

Browse files
Improvement
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent72226ca commitc40b4b8

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
intlengthOfLastWord(char*s)
56
{
6-
intlen=0;
7-
while (*s!='\0') {
8-
if (s[-1]==' '&&s[0]!=' ') {
9-
len=1;
10-
}elseif (*s!=' ') {
11-
len++;
12-
}
13-
s++;
7+
intword_len=0;
8+
intlen=strlen(s);
9+
10+
while (len>0&&s[--len]==' ') {}
11+
12+
while (len >=0&&s[len]!=' ') {
13+
word_len++;
14+
len--;
1415
}
15-
returnlen;
16+
17+
returnword_len;
1618
}
1719

1820
intmain(intargc,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
staticintdfs(intn,int*count)
66
{
7-
if (n==0) {
8-
return0;
7+
if (n==1) {
8+
return1;
9+
}elseif (n==2) {
10+
return2;
911
}elseif (count[n]>0) {
1012
returncount[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
returncount[n];
1917
}
2018
}
2119

2220
staticintclimbStairs(intn)
2321
{
24-
#if0
22+
#if1
23+
if (n<1)return0;
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
returndfs(n,count);
3027
#else
3128
inti,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
structListNode*deleteDuplicates(structListNode*head)
1112
{
12-
structListNode*p,*q;
13-
p=q=head;
14-
while (p!=NULL) {
15-
while (q!=NULL&&q->val==p->val) {
16-
q=q->next;
13+
structListNodedummy;
14+
structListNode*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-
returnhead;
24+
prev->next=head;
25+
returndummy.next;
2226
}
2327

2428
intmain(intargc,char**argv)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp