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

Commitcb98925

Browse files
authored
Merge pull requestneetcode-gh#1331 from julienChemillier/patch-18
Add 58 in c language
2 parents57ef53d +b76f206 commitcb98925

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎c/58-Length-Of-The-Last-Word.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Given a string s consisting of words and spaces, return the length of the last word in the string.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
intlengthOfLastWord(char*s){
9+
intlast_space=-1;// Index of the last ' '
10+
intlast_word=0;// Length of the last word
11+
inti;
12+
for (i=0;s[i]!='\0';i++) {
13+
if (s[i]==' ') {
14+
if (last_space!= (i-1)) {
15+
last_word=i-last_space-1;
16+
}
17+
last_space=i;
18+
}
19+
}
20+
if (last_space== (i-1)) {// if the length wanted is already in last_word
21+
returnlast_word;
22+
}else {
23+
returni-last_space-1;
24+
}
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp