首页 > 其他 > 详细

58. Length of Last Word

时间:2018-04-16 17:51:09      阅读:135      评论:0      收藏:0      [点我收藏+]
 1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     int lengthOfLastWord(string s) 
12     {
13         int len=s.length();
14         if(len==0)
15             return 0;
16         int i=len-1;
17         while(!isalpha(s[i]))
18             --i;
19         if(i<0)
20             return 0;
21         int count=0;
22         while(i>=0)
23         {
24             if(!isalpha(s[i]))
25                 break;
26             ++count;
27             --i;
28         }
29         return count;
30     }
31 };

从后向前扫描,直到第一个字母,开始计数,遇到非字母或者到了字符串头,停止计数,输出结果。

58. Length of Last Word

原文:https://www.cnblogs.com/zhuangbijingdeboke/p/8856380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!