码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Length of Last Word

时间:2015-06-28 16:57:20      阅读:108      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!