LeetCode58----Length of Last Word
2015-09-07 17:00
411 查看
LeetCode敲的第一题,小菜鸟一只,纪念一下~~
Given a string s consists of upper/lower-case alphabets and empty space characters
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 =
return
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.
class Solution { public: int lengthOfLastWord(string s) { const char *p=s.c_str(); if(*p=='\0')<span style="white-space:pre"> </span>//判断字符串是否为空 { return 0; } int num=0; while(*p!='\0')<span style="white-space:pre"> </span>//判断是否为字符串尾 { if(*p!=' ')<span style="white-space:pre"> </span>//不是空格时计数 { num++; p++; } else { if(*(++p)!='\0'&&*p!=' ')<span style="white-space:pre"> </span>//空格时,判断下一字符是否为串尾,并是否为连续空格, num=0;<span style="white-space:pre"> </span>//不是串尾&&不是空格,表明为字符,重新计数;否则,进行新一轮判断 } } return num; } };
相关文章推荐
- C# 面向对象的三大特性
- sqlite or svn 错误 The database disk image is malformed 可解决
- 第一周 项目0 宣告“主权”
- 第一次博文
- 宣告主权
- Mac 上配置Python开发环境 Pydev + Eclipse
- js打印对象的方法
- 第2周SHH数据结构—【项目1 - C/C++语言中函数参数传递的三种方式】
- hibernate中一对多关系的映射
- 1232 -- 找模式串
- 1231 -- 金子上的友情
- apns 官方文档
- Java中枚举的7种用法
- 《IOS_OC》继承、多态、初始化方法、便利构造器
- IOS_UI_UITableView 应用
- 蓝桥杯:翻硬币
- python中的高阶函数
- Sql数据库批量清理日志
- mysql中insert和replace的区别
- 第一周 项目1 C++语言中函数参数传递的三种方式