Leetcode 58. Length of Last Word

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Length of Last Word

2. Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int lengthOfLastWord(string s) {
int length = 0;
int index = s.find_last_not_of(" ");
if(index == -1) {
return length;
}
while(index >= 0 && s[index--] != ' ') {
length++;
}
return length;
}
};

Reference

  1. https://leetcode.com/problems/length-of-last-word/description/
如果有收获,可以请我喝杯咖啡!