LeetCode刷题(C++)——Length of Last Word(Easy)
2017-05-14 17:05
423 查看
Given a string s consists of upper/lower-case alphabets and empty space characters
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s =
return
class Solution {
public:
int lengthOfLastWord(string s) {
if (s.empty())
return 0;
int len = 0;
int k = s.size() - 1;
while (k >= 0 && s[k] == ' ')
k--;
for (int i =k;i >= 0;i--)
{
if (s[i] == ' ')
return len;
len++;
}
return len;
}
};
' ', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s =
"Hello World",
return
5.
class Solution {
public:
int lengthOfLastWord(string s) {
if (s.empty())
return 0;
int len = 0;
int k = s.size() - 1;
while (k >= 0 && s[k] == ' ')
k--;
for (int i =k;i >= 0;i--)
{
if (s[i] == ' ')
return len;
len++;
}
return len;
}
};
相关文章推荐
- LeetCode 58 — Length of Last Word(C++ Java Python)
- LeetCode-58-Length of Last Word(字符串)-Easy
- leetcode_length of last word_easy
- leetcode_c++:Length of Last Word(058)
- 【LeetCode】(58)Length of Last Word(Easy)
- leetcode:58 Length of Last Word-每日编程第三十四题
- [leetcode-58]Length of Last Word(c)
- LeetCode-Easy部分中标签为String 58. Length of Last Word
- leetcode---Length of Last Word---字符串
- [LeetCode] Length of Last Word 最后一个单词的长度
- [LeetCode] 041: Length of Last Word
- LeetCode-58-Length of Last Word-E
- [Leetcode] Length of last word 最后一个单词的长度
- LeetCode:Length of Last Word
- Leetcode 58. Length of Last Word (Easy) (cpp)
- leetcode之Length of Last Word
- LeetCode:Length of Last Word
- leetcode [Length of Last Word]
- 【LeetCode】Length of Last Word 解题报告
- Leetcode 58 Length of Last Word 字符串