-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path58.cpp
More file actions
executable file
·34 lines (34 loc) · 829 Bytes
/
58.cpp
File metadata and controls
executable file
·34 lines (34 loc) · 829 Bytes
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
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
class Solution {
public:
int lengthOfLastWord(string s) {
vector<string> cont;
char delim = ' ';
std::stringstream ss(s);
std::string token;
bool flag = false;
for (int i = 0; i< s.length();i++)
if (s[i] != ' '){
flag = true;
break;
}
if (flag == false)
return 0;
while (getline(ss, token, delim)) {
cont.push_back(token);
}
if (cont.size() == 0)
return 0;
string answer;
int i = cont.size()-1;
while (cont[i] == " " || cont[i] == "") {
i--;
}
answer = cont[i];
return answer.length();
}
};