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

LeetCode OJ:Length of Last Word(最后一个词的长度)

时间:2015-10-22 10:44:05      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example, 
Given s = "Hello World",
return 5.

求最后一个非空单词的长度而已,没什么注意点,直接看代码,理解题目意思就可以了:

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

 

LeetCode OJ:Length of Last Word(最后一个词的长度)

标签:

原文地址:http://www.cnblogs.com/-wang-cheng/p/4900010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!