Leetcode58-Length of Last Word
来源:互联网 发布:医院网络拓扑结构图 编辑:程序博客网 时间:2024/06/03 11:17
题目:难度(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.
Tags:String
分析:最后一个单词的理解:最后一个单词的后面和前面均允许有空白符;如果串中只有一个单词也是最后一个单词;全是空白符就一个单词也没有,也就没有最后一个单词之说了。
思路:开始打算用for程序遍历整个string,然后找到所有的空格键,在找到空格键之间的word,后来发现情况太复杂,主要是空格键若在string的首和尾,会有各种可能,借鉴别人的方法,用strip()函数除去首位的空格键,再从后往前找到所有在中间的空格键
str.strip()---除去左右的空格键,
str.lstrip()---除去左边的空格键
str.rstrip()---除去右边的空格键
阅读全文
0 0
- LeetCode58:Length of Last Word
- Leetcode58 Length of Last Word
- LeetCode58----Length of Last Word
- leetcode58---Length of Last Word
- leetcode58:Length of last word
- Leetcode58: Length of Last Word
- leetcode58: Length of Last Word
- Leetcode58.Length of Last Word
- LeetCode58. Length of Last Word
- LeetCode58 Length of Last Word
- Leetcode58. Length of Last Word
- leetcode58. Length of Last Word
- leetcode58. Length of Last Word
- LeetCode58. Length of Last Word
- Leetcode58-Length of Last Word
- LeetCode58——Length of Last Word
- LeetCode58——Length of Last Word
- Length of Last Word
- vue学习之路(二)--vue-awesome-swiper
- bootstrap日期控件
- Spring IOC经典案例
- spring cloud day05【turbine】
- 黑客帝国
- Leetcode58-Length of Last Word
- 今天开通了博客
- 丫丫头-玲珑塔设计教程
- Bean注入
- 欢迎使用CSDN-markdown编辑器
- Grunt学习1_Grunt插件之LiveReload 实现页面自动刷新,所见即所得编辑
- SpringMVC之分析HandlerMethodReturnValueHandler(二)
- String类的常用方法
- 怎么让LED灯延时1秒