首页 > 其他 > 详细

[LeetCode] Length of Last Word

时间:2015-06-28 16:54:44      阅读:155      评论:0      收藏:0      [点我收藏+]

Well, the basic idea is very simple. Start from the tail of s and move backwards to find the first non-space character. Then from this character, move backwards and count the number of non-space characters until we pass over the head of s or meet a space character. The count will then be the length of the last word.

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(string s) { 
 4         int len = 0, tail = s.length() - 1;
 5         while (tail >= 0 && s[tail] ==  ) tail--;
 6         while (tail >= 0 && s[tail] !=  ) {
 7             len++;
 8             tail--;
 9         }
10         return len;
11     }
12 };

 

[LeetCode] Length of Last Word

原文:http://www.cnblogs.com/jcliBlogger/p/4605703.html

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