Leetcode 58 Length of Last Word 字符串

找出最后一个词的长度

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(string s) {
 4         int a = 0, l = 0, b = 0;
 5         while((b = s.find(" ", a)) != string::npos){
 6             if(b - a > 0) l = b - a;
 7             a = b + 1;
 8         }
 9         if(a == s.size()) return l;
10         else return s.size() - a;
11     }
12 };

 

posted @ 2016-03-13 19:51  Breeze0806  阅读(102)  评论(0编辑  收藏  举报