LeetCode 0058 - Length of Last Word
# Hints
- 水题
# 题面
Difficulty | Time Complexity Limit | Extra-Memory Complexity Limit |
---|---|---|
Easy |
Given a string 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 .
Note:
A word is defined as a maximal substring consisting of non-space characters only.
Example:
Input: "Hello World"
Output: 5
# 题意
给定一个由大小写字母及空格构成的字符串 ,求串中的最后一个单词的长度。
# 题解
水题,用 stringstream
直接模拟即可。
# AC代码
class Solution {
public:
int lengthOfLastWord(const string & s) {
// 求解
stringstream ss(s);
string word;
int res = 0;
while (ss >> word) {
res = word.length();
}
// 返回
return res;
}
};
- 01
- Reading Papers - Kernel Concurrency06-01
- 02
- Linux Kernel - Source Code Overview05-01
- 03
- Linux Kernel - Per-CPU Storage05-01