58. Length of Last Word
Last updated
Last updated
class Solution {
public:
int lengthOfLastWord(string s) {
int size = s.length();
int cnt = 0, lastCnt = 0;
for (int i = 0; i < size; ++i) {
if (s[i] != ' ') {
++cnt;
} else {
if (cnt != 0) {
lastCnt = cnt;
}
cnt = 0;
}
}
if (s[size - 1] != ' ') {
return cnt;
}
return lastCnt;
}
};