[LeetCode刷题记录]Length of Last Word
来源:互联网 发布:怎么给淘宝客服评价 编辑:程序博客网 时间:2024/04/30 11:01
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
.
思路:指针扫描一次,遇到空格置零。要特别注意多个空格的情况。要判断空格后面是否为空格,或者是指到字符串尾部。
对char *操作时注意一些细节问题。
class Solution {public: int lengthOfLastWord(const char *s) { if(s==NULL) return 0; int ret=0; int flag=0; while(*s){ if(*s!=' ') ++ret; if(*s==' '&&*(s+1)&&*(s+1)!=' ') ret=0; ++s; } return ret; }};
0 0
- [LeetCode刷题记录]Length of Last Word
- Leetcode刷题记——58. 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
- 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
- 【BZOJ3669】NOI2014-魔法森林(神奇的解法)
- 电脑使用一段时间自动休眠
- ftp put 到指定的远程目录
- Python实现计数排序
- Oracle 数据库11g 最佳培训教程PPT合集
- [LeetCode刷题记录]Length of Last Word
- 第二章作业
- PHP 学习之--第一个PHP脚本
- Gitlab的使用总结
- B. Party
- javascript中对象的深度克隆
- 根据参数提示错误信息
- 如何判定a,b,c,d四个值是否相等
- 树莓派的系统安装