[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 @   jianchao-li  阅读(172)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示