58.leetcode Length of Last Word(easy)[字符串 分割]
来源:互联网 发布:linux安装vnc客户端 编辑:程序博客网 时间:2024/04/27 11:50
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
.
返回字符串中最后一个单词的长度,因此这个题目可以采用下面的方式完成在字符串首部加上空格,那么每遍历到一个非空格字符那么前面的字符如果是空格那么step清零重新开始计算,否则增加step的值。也可以采用分割算法按照空格分隔字符串得到最后一个字符串,然后计算长度。
class Solution {public: int lengthOfLastWord(string s) { int step = 0; int flag = 0; string s1(" "); s = s1+s; for(int i=1;i<s.length();i++) { if(s[i] !=' ') { if(s[i-1]==' ') step = 0; ++step; } } return step; }};
0 0
- 58.leetcode Length of Last Word(easy)[字符串 分割]
- LeetCode-58-Length of Last Word(字符串)-Easy
- [leetcode] 【字符串】58. Length of Last Word
- [Leetcode 58, easy] Length of Last Word
- 【LeetCode】(58)Length of Last Word(Easy)
- <LeetCode><Easy> 58 Length of Last Word
- Leetcode 58. Length of Last Word (Easy) (cpp)
- leetcode 58. Length of Last Word(easy)
- 58. Length of Last Word [easy] (Python)
- 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 058 Length of Last Word(难易度:Easy)
- LeetCode-Easy刷题(12) Length of Last Word
- 58. Length of Last Word Leetcode Python
- [LeetCode]58.Length of Last Word
- Android自定义View:如何实现一个模拟时钟?
- hdu1013-字符相加
- HDU 1443 Joseph
- 简表报表配置以及ftl文件的一些功能
- ios-anchorPoint、position理解
- 58.leetcode Length of Last Word(easy)[字符串 分割]
- Hexo结合Github创建静态博客
- 浅析Java中的final关键字
- 二叉树深度优先遍历
- AppWidget的使用及原理分析
- 【入门介绍】机器学习之强化学习算法
- C++中的istringstream 的用法 HDU 2072单词数
- 二叉搜索树的习题 pta
- 基于树的查找(二叉排序树、平衡二叉树、B树、B+树、伸展树和红黑树)