-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
42 lines (36 loc) · 838 Bytes
/
main.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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <sstream>
using namespace std;
class Solution {
public:
int lengthOfLastWord(string s) {
int len = 0, tail = s.length() - 1;
while(tail >=0 && s[tail] == ' ') tail--; //去除前导空格
while(tail >=0 && s[tail] != ' '){ //倒序遍历最后一个单词
len++;
tail--;
}
return len;
}
/*
int lengthOfLastWord(string s) {
string buf;
stringstream str(s); //将字符串以空格分开
int len = 0;
while(str >> buf){
len = buf.length(); //最后一个字符串的长度
}
return len;
}*/
};
int main()
{
Solution s;
cout << s.lengthOfLastWord("b a ") <<endl;
return 0;
}