LeetCode 58. Length of Last Word
2016-07-22 01:49
525 查看
58. Length of Last Word
My Submissions QuestionEditorial Solution
Total Accepted: 90032 Total Submissions: 308380 Difficulty: Easy
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.
My Submissions QuestionEditorial Solution
Total Accepted: 90032 Total Submissions: 308380 Difficulty: Easy
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(string s) { int cnt = 0; int flag = 0; for(int i = s.length() - 1; i >= 0; i--) { if(flag == 0 && s[i] == ' ') { continue; } if(s[i] != ' ') { flag = 1; cnt++; } else { break; } } return cnt; } };
相关文章推荐
- 统计项目源码行数的Java代码
- LeetCode 203. Remove Linked List Elements
- LeetCode 38. Count and Say
- java实现鼠标和键盘动作后台监听(未测试)
- LeetCode 203. Remove Linked List Elements
- 利用aosv库实现各种滚动与组件的互动
- LeetCode 225. Implement Stack using Queues
- LeetCode 14. Longest Common Prefix
- CSS3实现10种Loading效果
- Android适配——采用Values-dpi-wSize X hSize 模式,并分析原理
- HDU 4768 Flyer(二分)
- LeetCode 234. Palindrome Linked List
- HDU 4768 Flyer(二分)
- js控制文本框只能输入中文、英文、数字与指定特殊符号
- LeetCode 67. Add Binary
- 常用正则表达式列表
- 在 mac 系统下播放.csf 视频的方法
- LeetCode 28. Implement strStr()
- 利用Gson实现Json串和Java Bean互转(未测试)
- LeetCode344. Reverse String