Leetcode Length of Last Word
2016-07-20 03:50
330 查看
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
Difficulty: Easy
public class Solution {
public int lengthOfLastWord(String s) {
s = s.trim();
int count = 0;
for(int i = s.length() - 1; i >=0 ; i--){
if(s.charAt(i) == ' '){
return count;
}
else{
count++;
}
}
return count;
}
}
' ', 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.
Difficulty: Easy
public class Solution {
public int lengthOfLastWord(String s) {
s = s.trim();
int count = 0;
for(int i = s.length() - 1; i >=0 ; i--){
if(s.charAt(i) == ' '){
return count;
}
else{
count++;
}
}
return count;
}
}
相关文章推荐
- Leetcode Populating Next Right Pointers in Each Node
- Leetcode 3Sum Closest
- Leetcode Maximal Rectangle
- IOS控件总是看不见。
- Leetcode Binary Tree Maximum Path Sum
- Leetcode Odd Even Linked List
- 重建控制文件后,各文件(datafile、control file、redo log)中scn的关系
- selenium 多线程
- Reorder List
- Leetcode Remove Duplicates from Sorted List
- Leetcode Path Sum
- Leetcode Power of Two/Three/Four
- Leetcode Rotate Image
- 最小边割集
- 云服务器上安装配置Filezilla Server的坑!
- 在安装Ubuntu的MacBook上更改Apple 键盘模式
- 算法日记(Java实现)第20160719(5)期——POJ1008
- 在MacBook Pro 12-1 with ubuntu 15.10 上安装风扇控制器(mbpfan)
- HDU5723 2016 Multi-University Training Contest 1 (最小生成树+dfs)
- 百度地图API 地理位置获取和大头针显示当前位置