We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f1e3ee2 commit b34e422Copy full SHA for b34e422
LengthofLastWord/LengthofLastWord.cpp
@@ -1,23 +1,19 @@
1
class Solution {
2
public:
3
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
14
- while (*s != ' ' && *s != '\0') {
15
16
- word_length += 1;
+ int curr = 0;
+ const char* p = s;
+ while (*p != '\0') {
+ if (*p == ' ') {
+ p++;
+ } else {
+ curr = 0;
+ while (*p != '\0' && *p != ' ') {
+ curr++;
+ }
17
}
18
- if (word_length)
19
- last_word_length = word_length;
20
21
- return last_word_length;
+ return curr;
22
23
-};
+};
0 commit comments