https://leetcode.com/problems/length-of-last-word/description/
int lengthOfLastWord(string s) {
if(s.size() == 0) return 0;
int i = s.size()-1;
while(i>=0 && s[i] == ' ') i--;
if(i == -1) return 0;
int j;
for(j=i; j>=0; j--) {
if(s[j] == ' ') break;
}
return i-j;
}
No comments:
Post a Comment