250x250
반응형
Notice
Recent Posts
Recent Comments
Link
N
(Leet Code c++)Length of Last Word 본문
728x90
반응형
58. Length of Last Word
Given a string s consists of some words separated by spaces, return the length of the last word in the string. If the last word does not exist, return 0.
A word is a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World" Output: 5
Example 2:
Input: s = " " Output: 0
Constraints:
- 1 <= s.length <= 104
- s consists of only English letters and spaces ' '.
class Solution {
public:
int lengthOfLastWord(string s) {
string token;
stringstream ss(s);
vector<string> str;
while(ss >> token){
str.push_back(token);
}
if(str.empty()){
return 0;
}
else{
return str.back().size();
}
}
};
728x90
반응형
'Leet Code 알고리즘' 카테고리의 다른 글
(Leet Code c++)Add Binary (0) | 2021.07.09 |
---|---|
(Leet Code c++)Plus One (0) | 2021.07.09 |
(Leet Code c++)Container With Most Water (0) | 2021.07.08 |
(Leet Code c++)Maximum Subarray (0) | 2021.07.08 |
(Leet Code c++)Search Insert Position (0) | 2021.07.07 |