Length of Last Word
来源:互联网 发布:安云网建站系统源码 编辑:程序博客网 时间:2024/05/22 12:46
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
.
Solution:
class Solution {public: int lengthOfLastWord(string s) { if(s.length() == 0) return 0; int i = s.length()-1; while(s[i] == ' ') i--; if(i < 0) return 0; int j = 0; while(i >= 0 && s[i--] != ' ') j++; return j; }};
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
- 传智陈力 PHP培训.第二版PHP视频教程.陈力.大型门户网站核心技术-页面静态化.页面静态化教程简介
- linux下vi操作命令简介归纳
- c# 中的TimeSpan类
- 逆向工程破解软件的一些汇编命令
- poj 3083 Children of the Candy Corn 搜索
- Length of Last Word
- 常用的linux指令
- android 无标题栏的设置方法
- 多线程四种封装方式,外部类调用两种,匿名内部类两种
- tableView的刷新,cell重用失败的问题
- Java中匿名内部类总结
- Codeforces Round #311 (Div. 2) A,B,C,D,E
- 希尔排序算法
- Project Euler 46 solution optimized using C++ meta-programming