[leetcode] 58. Length of Last Word 解题报告
来源:互联网 发布:时时彩合买软件 编辑:程序博客网 时间:2024/06/06 01:04
题目连接:https://leetcode.com/problems/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
.
思路:本题有一个需要注意的就是末尾可能会有空格,所以把空格先去掉。
代码如下:
class Solution {public: int lengthOfLastWord(string s) { while(s[s.size()-1] == ' ') s.erase(s.size()-1); return s.size() - s.rfind(' ') -1; }};
0 0
- [Leetcode] Length of Last Word 解题报告
- 【LeetCode】Length of Last Word 解题报告
- [leetcode] 58. Length of Last Word 解题报告
- [Leetcode] 58. Length of Last Word 解题报告
- [LeetCode 解题报告]058. Length of Last Word
- Leetcode #58. Length of Last Word 末尾词长 解题报告
- 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
- 网络层—广播路由选择算法
- requestCode与resultCode不等以及编程中遇到的其它问题
- CentOS 6.5 Git源码安装
- Hive中的三种不同的数据导出方式介绍
- ios中ARC模式下的内存管理
- [leetcode] 58. Length of Last Word 解题报告
- DP---源点到目的点经过最多的1点
- 关于CommandType.StoredProcedure输出参数访问问题
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
- 第六届福建省大学生程序设计竞赛 Problem J RunningMan【数学】
- Hadoop编码解码【压缩解压缩】机制详解(1)
- xcode command+左键不能跳转的解决方法
- LeetCode之String题目汇总
- print_r、echo、var_dump三者的区别