58. 最后一个单词的长度

 1 class Solution 
 2 {
 3     void spilt(string s,char c,vector<string> &res)
 4     {
 5         istringstream iss(s);
 6         string temp;
 7         while(getline(iss,temp,c))
 8         {
 9             //如果temp不为空,才可以添加进去
10             if(!temp.empty()) res.push_back(temp);
11         }
12     }
13 public:
14     int lengthOfLastWord(string s) 
15     {
16         vector<string> res; 
17         if(s.empty()) return 0;
18 
19         spilt(s,' ',res);
20         if(res.empty()) return 0;
21         return res.back().size();
22     }
23 };

 

posted @ 2020-03-19 19:20  Jinxiaobo0509  阅读(111)  评论(0)    收藏  举报