【leetcode】:Length of the LastWord
来源:互联网 发布:大数据量表设计 编辑:程序博客网 时间:2024/06/05 17:04
前言
这个题目非常之简单,就是求出一行字符串其中最后一个单词的长度
思路
用s.size()求出一行长度,然后从最后开始遍历,找到第一个不是空格的地方记录,为最后一个字符串的末位置
继续遍历,找到最后一个字符的首位置,然后两位置一减得最后结果
class Solution {public: int lengthOfLastWord(string s) { int len = s.size(); if(len==0) return 0; while(len>0 &&s[len-1] == ' '){ len--; //len就代表了最后的空格位置 } int end = len; while(len>0 &&s[len-1]!=' '){ len--; } len =end -len; return len; }};
0 0
- 【leetcode】:Length of the LastWord
- 【LeetCode】 Length Of LastWord
- LeetCode的LastWord
- leetcode-Length of the longest substring without repeating characters
- the length of the string
- calculate the length of char*
- 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]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] Length of Last Word
- leetcode length of last word
- Golang初级系列教程-手把手编写 Hello World
- php 多重继承的替代方案trait
- 最近的状态不好
- 一步一步学习Angular2(03.示例 MASTER/DETAIL)
- GitLab/Git在AndroidStudio上的使用
- 【leetcode】:Length of the LastWord
- Android 开发过程中遇见的奇葩的bug
- 怎么样在svn根目录添加一个工程
- xilinx 音频技术参考
- C语言常用数学函数
- 设计模式
- Android定制化显示toast
- git克隆;上传;合并
- setFixedSize引起video画面闪烁