[Leetcode] Length of Last Word 最后一个单词长度 - SegmentFault
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
http://algobox.org/length-of-last-word/
http://algorithm.yuanbin.me/zh-cn/string/length_of_last_word.html
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 5http://algobox.org/length-of-last-word/
Start from the last character, count the non-space character until we meet the space. The trailing space is skipped by query if current count of non-space character is zero.
public int lengthOfLastWord(final String a) {
int length = 0;
for (int i = a.length() - 1; i > -1; --i)
if (a.charAt(i) != ' ')
length++;
else if (length != 0)
break;
return length;
}
http://n00tc0d3r.blogspot.com/2013/03/length-of-last-word.html
We first trim the while spaces from the end of the string and then go backwards to count the length of the last word (if exists).
public int lengthOfLastWord(String s) {
int len = 0, last = s.length()-1;
while (last>=0 && s.charAt(last) == ' ') last--;
while (last>=0 && s.charAt(last--) != ' ') ++len;
return len;
}
http://algorithm.yuanbin.me/zh-cn/string/length_of_last_word.html
public int lengthOfLastWord(String s) {
if (s == null | s.isEmpty()) return 0;
// trim right space
int begin = 0, end = s.length();
while (end > 0 && s.charAt(end - 1) == ' ') {
end--;
}
// find the last space
for (int i = 0; i < end; i++) {
if (s.charAt(i) == ' ') {
begin = i + 1;
}
}
return end - begin;
}
http://blog.csdn.net/linhuanmars/article/details/21858067public int lengthOfLastWord(String s) { if(s==null || s.length()==0) return 0; int idx = s.length()-1; while(idx>=0 && s.charAt(idx)==' ') idx--; int idx2 = idx; while(idx2>=0 && s.charAt(idx2)!=' ') idx2--; return idx-idx2; }X. http://www.programcreek.com/2014/05/leetcode-length-of-last-word-java/
We just need a flag to mark the start of letters from the end. If a letter starts and the next character is not a letter, return the length.
http://www.cnblogs.com/springfor/p/3872326.html
这道题主要是考虑一下最后是不是空格,方法是倒着找不是空格的字符并计数,如果遇到空格且计数不是0,说明最后一个单词已经被计数了,所以可以返回了。
Read full article from [Leetcode] Length of Last Word 最后一个单词长度 - SegmentFault
1 public int lengthOfLastWord(String s) {
2 if (s == null || s.length() == 0)
3 return 0;
4
5 int len = s.length();
6 int count = 0;
7 for (int i = len - 1; i >= 0; i--) {
8 if (s.charAt(i) != ' ') {
9 count++;
10 }
11 if(s.charAt(i)==' '&&count != 0){
12 return count;
13 }
14 }
15 return count;
16 }
2 if (s == null || s.length() == 0)
3 return 0;
4
5 int len = s.length();
6 int count = 0;
7 for (int i = len - 1; i >= 0; i--) {
8 if (s.charAt(i) != ' ') {
9 count++;
10 }
11 if(s.charAt(i)==' '&&count != 0){
12 return count;
13 }
14 }
15 return count;
16 }
public int lengthOfLastWord(String s) {
return s.trim().split(" +")[s.trim().split(" +").length - 1].length();
}
从后往前看字符串,跳过所有空格后,记下该结束位置,再到下一个空格,再记录一个开始位置,则长度就是结束位置减去开始位置。在跳过空格的循环后,要判断是否已经超界,如果超界则返回0 public int lengthOfLastWord(String s) {
int idx = s.length() - 1;
// 跳过末尾的空格
while(idx >= 0){
if(s.charAt(idx) != ' ') break;
idx--;
}
// 记录结束位置
int end = idx;
// 如果已经超界返回0
if(idx < 0) return 0;
// 找到开始位置
while(idx >= 0){
if(s.charAt(idx) == ' ') break;
idx--;
}
return end - idx;
}
https://gist.github.com/Jeffwan/8856782Read full article from [Leetcode] Length of Last Word 最后一个单词长度 - SegmentFault