[LintCode]最後の単語の長さ

2329 ワード

 1 class Solution {
 2 public:
 3     /**
 4      * @param s A string
 5      * @return the length of last word
 6      */
 7     int lengthOfLastWord(string s) {
 8         int len = 0, tail = s.length() - 1;
 9         while (tail >= 0 && s[tail] == ' ') tail--;
10         while (tail >= 0 && s[tail] != ' ') {
11             len++;
12             tail--;
13         }
14         return len;
15     }
16 };