LeetCode 58 - Length of Last Word
来源:互联网 发布:2017省市区数据库sql 编辑:程序博客网 时间:2024/06/12 02:14
Length of 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
.
My Code
class Solution {public: int lengthOfLastWord(string s) { char c; int end = s.size() - 1; while (end >= 0 && s[end] == ' ') end--; int start = end - 1; while (start >= 0 && s[start] != ' ') start--; if (end == -1) return 0; return end - start; }};Runtime: 4 ms
0 0
- [leetcode 58] Length of Last Word
- leetcode-58 Length of Last Word
- Leetcode NO.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, easy] Length of Last Word
- Length of Last Word (leetCode 58)
- leetcode[58]:Length of Last Word
- 【leetcode c++】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(c)
- leetcode 58: Length of Last Word
- leetCode #58 Length of Last Word
- NSRunLoop
- 嵌入式系统课程开始
- [leetcode] 236. Lowest Common Ancestor of a Binary Tree
- 有符号和无符号数表示
- nyoj-686-An Interesting Sequence【分解质因数】
- LeetCode 58 - Length of Last Word
- java cookie全解析
- 7 行代码集成所有移动支付,“Ping++” 试图打造中国版 Stripe
- Java 多线程(五)——线程通信(共享内存、管道流、wait()、notify()等)
- 自定义内容提供者getType(Uri uri) 方法详解
- Spring MVC 4
- 线性表的查找
- Web Payment (在线支付/网上支付)
- 莫比乌斯反演学习笔记