leetcode:58 Length of Last Word-每日编程第三十四题
2015-12-26 19:35
405 查看
Length of Last Word
Total
Accepted: 76106 Total
Submissions: 269840 Difficulty: Easy
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
<span style="font-size:12px;">class Solution {
public:
int lengthOfLastWord(string s) {
int len=s.length();
//判断是否为空字符串
if(len==0){
return 0;
}
int index=len-1;
//去掉末尾可能出现的空格
while(index>=0&&s[index]==' '){
index--;
}
//ans为最后一个字符长度
int ans=0;
while(index>=0&&s[index]!=' '){
index--;
ans++;
}
return ans;
}
};</span>
Total
Accepted: 76106 Total
Submissions: 269840 Difficulty: Easy
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.
<span style="font-size:12px;">class Solution {
public:
int lengthOfLastWord(string s) {
int len=s.length();
//判断是否为空字符串
if(len==0){
return 0;
}
int index=len-1;
//去掉末尾可能出现的空格
while(index>=0&&s[index]==' '){
index--;
}
//ans为最后一个字符长度
int ans=0;
while(index>=0&&s[index]!=' '){
index--;
ans++;
}
return ans;
}
};</span>
相关文章推荐
- 如何创建和使用Python CGI脚本
- 整数的输出格式(C++程序设计第7周)
- 实数的输出格式(C++程序设计第7周)
- 服务相关编程
- java取出一组中文每个汉字的首字母 如 :中国 zg
- 对编程而言,学历再高,智商再高,也得一样一样来
- Python模块学习:threading 多线程控制和处理
- ACM StepByStep(一)基础知识与参考资料
- Java之关于Serialization
- 位操作在java和C/C++中的区别
- Django项目开发纪要
- 深刻理解Python中的元类(metaclass)
- GitHub简明使用指南
- 他山之石-PHP多线程的实现方法详解
- Spring加载resource时classpath*:与classpath:的区别
- spring注入properties属性配置
- C代码中如何调用C++ C++中如何调用C
- InfoQ中文站特供稿件:Rust编程语言的核心部件
- 实战c++中的智能指针unique_ptr系列-- 使用unique_ptr来避免if多层嵌套
- 实战c++中的智能指针unique_ptr系列-- 使用unique_ptr来避免if多层嵌套