LeetCode Length of Last Word
来源:互联网 发布:网络上的芙蓉姐姐是谁 编辑:程序博客网 时间:2024/06/10 09:17
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 count = 0, last = 0; while (*s != '\0') { count = (*s == ' ') ? 0 : count+1; last = count > 0 ? count : last; s++; } return last; }};
public class Solution { public int lengthOfLastWord(String s) { int count = 0, ans = 0; int cur = 0; for (; cur < s.length(); cur++) { count = (s.charAt(cur) == ' ') ? 0 : count+1; ans = count > 0 ? count : ans; } return ans; }}
0 0
- LeetCode: Length of Last Word
- LeetCode Length of Last Word
- LeetCode : Length of Last Word
- [Leetcode] Length of Last Word
- [LeetCode] Length of Last Word
- [LeetCode]Length of Last Word
- [leetcode]Length of Last Word
- Leetcode:Length of Last Word
- LeetCode-Length of Last Word
- [LeetCode] Length of Last Word
- [leetcode] Length of Last Word
- leetcode length of last word
- LeetCode - Length of Last Word
- leetcode:Length of Last Word
- LeetCode:Length of Last Word
- LeetCode | Length of Last Word
- LeetCode - Length of Last Word
- Leetcode: Length of Last Word
- C++ 用户层下的用Push+Ret API HOOK
- leetcode Find Minimum in Rotated Array
- 关于提高UDP发送效率的方法
- 欢迎使用CSDN-markdown编辑器
- CSS 实现表格内容超出用省略号显示
- LeetCode Length of Last Word
- 第3题作业
- java文本区控件
- memcached优化方法
- 关于sql优化的一些小技巧
- mysql left( right ) join使用on 与where 筛选的差异
- 准备蓝桥杯--dyx--数列特征
- 从游戏设计到星际探险,微软HoloLens全息技术将改变什么?
- Python爬虫爬学校民主湖(4)------------模拟登录