-
Notifications
You must be signed in to change notification settings - Fork 2
/
58.LengthOfLastWord.cpp
53 lines (48 loc) · 1.08 KB
/
58.LengthOfLastWord.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* @lc app=leetcode id=58 lang=cpp
*
* [58] Length of Last Word
*
* https://leetcode.com/problems/length-of-last-word/description/
*
* algorithms
* Easy (32.56%)
* Likes: 714
* Dislikes: 2569
* Total Accepted: 395K
* Total Submissions: 1.2M
* Testcase Example: '"Hello World"'
*
* Given a string s consists of upper/lower-case alphabets and empty space
* characters ' ', return the length of last word (last word means the last
* appearing word if we loop from left to right) in the string.
*
* If the last word does not exist, return 0.
*
* Note: A word is defined as a maximal substring consisting of non-space
* characters only.
*
* Example:
*
*
* Input: "Hello World"
* Output: 5
*
*
*
*
*/
// @lc code=start
#include <string>
class Solution {
public:
int lengthOfLastWord(std::string s) {
int ptr = s.length() - 1;
int len = 0;
while (ptr >= 0 && s[ptr] == ' ') ptr--;
if (ptr < 0) return len;
while (ptr >= 0 && s[ptr--] != ' ') len++;
return len;
}
};
// @lc code=end