设为首页 加入收藏

TOP

leetcode - Length of Last Word
2015-07-20 17:27:00 来源: 作者: 【 】 浏览:4
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..

评论

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

·Python爬虫教程(从 (2025-12-26 16:49:14)
·【全269集】B站最详 (2025-12-26 16:49:11)
·Python爬虫详解:原 (2025-12-26 16:49:09)
·Spring Boot Java: (2025-12-26 16:20:19)
·Spring BootでHello (2025-12-26 16:20:15)