8000 1 · javasharper/leetcode@b34e422 · GitHub
[go: up one dir, main page]

Skip to content

Commit b34e422

Browse files
committed
1
1 parent f1e3ee2 commit b34e422

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

LengthofLastWord/LengthofLastWord.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
class Solution {
22
public:
33
int lengthOfLastWord(const char *s) {
4-
// Start typing your C/C++ solution below
5-
// DO NOT write int main() function
6-
7-
int last_word_length = 0;
8-
int word_length = 0;
9-
while (*s != '\0') {
10-
while (*s == ' ')
11-
s++;
12-
13-
int word_length = 0;
14-
while (*s != ' ' && *s != '\0') {
15-
s++;
16-
word_length += 1;
4+
int curr = 0;
5+
const char* p = s;
6+
while (*p != '\0') {
7+
if (*p == ' ') {
8+
p++;
9+
} else {
10+
curr = 0;
11+
while (*p != '\0' && *p != ' ') {
12+
p++;
13+
curr++;
14+
}
1715
}
18-
if (word_length)
19-
last_word_length = word_length;
2016
}
21-
return last_word_length;
17+
return curr;
2218
}
23-
};
19+
};

0 commit comments

Comments
 (0)
0