[LintCode] 最后一个单词的长度

 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 };

 

posted @ 2015-06-28 15:57  jianchao-li  阅读(170)  评论(0编辑  收藏  举报