设为首页 加入收藏

TOP

leetcode - Length of Last Word
2015-07-20 17:27:00 来源: 作者: 【 】 浏览:3
Tags:leetcode Length Last Word

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.

class Solution {
public:
    int lengthOfLastWord(const char *s) {
		int cnt = 0,result = 0;
		while (*s != '\0')
		{
			cnt = *s == ' ' ? 0 : cnt + 1;
			result = cnt > 0 ? cnt : result;
			s++;
		}
		return result;
    }
};


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA10972 - RevolC FaeLoN(双连通.. 下一篇HDU 4027 Can you answer these q..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)
·Redis - The Real-ti (2025-12-26 08:20:50)
·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)