leetcode-58. Length of Last Word
来源:互联网 发布:达芬奇恶魔知乎 编辑:程序博客网 时间:2024/05/15 12:42
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.
思路:从后到前遍历得到最后一个word长度。注意最后可能有一系列空格,先清除
class Solution {public: int lengthOfLastWord(string s) { int length = 0; int i = s.size()-1; while(s[i] == ' ') { i--; } for(;i >= 0;i--) { if(s[i] == ' ') { return length; } length++; } return length; }};
0 0
- 58. Length of Last Word Leetcode Python
- [LeetCode]58.Length of Last Word
- LeetCode --- 58. Length of Last Word
- [Leetcode] 58. Length of Last Word
- [leetcode] 58.Length of Last Word
- LeetCode - 58. Length of Last Word
- <LeetCode OJ> 58. Length of Last Word
- 58. Length of Last Word LeetCode
- 【LeetCode】58. Length of Last Word
- leetcode 58. Length of Last Word
- LeetCode *** 58. Length of Last Word
- LeetCode 58. Length of Last Word
- leetcode-58. Length of Last Word
- LeetCode-58.Length of Last Word
- [leetcode] 58. Length of Last Word
- LeetCode 58. Length of Last Word
- Leetcode-58. Length of Last Word
- leetcode-java-58. Length of Last Word
- Fragment知识总结
- Spark-000讲:What is Spark ?
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(七)
- poj 2632Crashing Robots
- vi/vim编辑器必知必会
- leetcode-58. Length of Last Word
- 自定义控件三部曲之绘图篇(十)——Paint之setXfermode(一)
- Python分析网页神器pyquery
- 第五章 Python数据结构
- UUID生成器
- JavaScript与HTML_DOM
- leetcode-203. Remove Linked List Elements
- 数据挖掘-数据探索
- 最短排序