Length of Last Word
来源:互联网 发布:剑三小和尚捏脸数据 编辑:程序博客网 时间:2024/05/17 18:01
从前往后扫描,遇到非空格字符则len+1,遇到空格不能直接len归0,需要判断下一个字符是否为非空格,如果非空格,则len归0,否则不能归0。
注意实现细节。
class Solution {public: int lengthOfLastWord(const char *s) { int len = 0; while(*s) { if(*s++ != ' ') len++; else if(*s && *s != ' ') len = 0; } return len; }};
9.1 最新代码。
class Solution {public: int lengthOfLastWord(const char *s) { const int len = strlen(s); int lastLength = 0; bool word = false; for(int i=0; i<len; ++i) { if(s[i] != ' ') { if(!word) { lastLength = 1; word = true; } else { lastLength++; } } else word = false; } return lastLength; }};
0 0
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- LeetCode 112 — Path Sum(C++ Java Python)
- 四种高效的SqlServer分页方式
- 移动网页开发
- Create Unit Test on VS
- nginx启动,重启,关闭命令
- Length of Last Word
- 由委托说明事件的由来2
- Linux设备驱动程序学习(21)-内存映射和DMA-数据结构
- mac下adb环境变量的配置以及command not found的解决
- Generate BrainF**k code
- ZOJ 3261 (并查集)
- GMT、UTC与24时区 等时间概念
- Serial Programming Guide for POSIX Operating Systems 5th Edition Michael R. Sweet Copyright 1994-
- HDFS HA 及解决方案