File tree Expand file tree Collapse file tree 3 files changed +72
-0
lines changed 
project/LeetCode/leetcode/src/main/java/com/blankj/easy/_058 Expand file tree Collapse file tree 3 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 1717| 27| [ Remove Element] [ 027 ] | Array, Two Pointers| 
1818| 28| [ Implement strStr()] [ 028 ] | Two Pointers, String| 
1919| 38| [ Count and Say] [ 028 ] | String| 
20+ | 58| [ Length of Last Word] [ 058 ] | String| 
2021
2122
2223## Medium  
5253[ 027 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/027/README.md 
5354[ 028 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/028/README.md 
5455[ 038 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/038/README.md 
56+ [ 058 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/058/README.md 
Original file line number Diff line number Diff line change 1+ # [ Length of Last Word] [ title ]  
2+ 
3+ ## Description  
4+ 
5+ Given a string * s*  consists of upper/lower-case alphabets and empty space characters ` ' ' ` , return the length of last word in the string.
6+ 
7+ If the last word does not exist, return 0.
8+ 
9+ ** Note:**  A word is defined as a character sequence consists of non-space characters only.
10+ 
11+ For example,
12+ 
13+ Given * s*  = ` "Hello World" ` ,
14+ 
15+ return ` 5 ` .
16+ 
17+ ** Tags:**  String
18+ 
19+ 
20+ ## 思路  
21+ 
22+ 题意是让你从一个只包含大小字母和空格字符的字符串中得到最后一个单词的长度,很简单,我们倒序遍历,先得到最后一个非空格字符的索引,然后再得到它前面的空格字符索引,两者相减即可。当然,我们使用API来完成这件事更加方便,只需一行代码` return s.trim().length()-s.trim().lastIndexOf(" ")-1; ` ,但我相信作者出这道题的目的肯定不是考你API的使用,所以我们还是用自己的思路来实现。
23+ 
24+ ```  java 
25+ public  class  Solution  {
26+  public  int  lengthOfLastWord (String  s ) {
27+  int  p =  s. length() -  1 ;
28+  while  (p >=  0  &&  s. charAt(p) ==  '  ' -- ;
29+  int  end =  p;
30+  while  (p >=  0  &&  s. charAt(p) !=  '  ' -- ;
31+  return  end -  p;
32+  }
33+ }
34+ ``` 
35+ 
36+ 
37+ ## 结语  
38+ 
39+ 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[ awesome-java-leetcode] [ ajl ] 
40+ 
41+ 
42+ 
43+ [ title ] : https://leetcode.com/problems/length-of-last-word 
44+ [ ajl ] : https://github.com/Blankj/awesome-java-leetcode 
Original file line number Diff line number Diff line change 1+ package  com .blankj .easy ._058 ;
2+ 
3+ /** 
4+  * <pre> 
5+  * author: Blankj 
6+  * blog : http://blankj.com 
7+  * time : 2017/04/21 
8+  * desc : 
9+  * </pre> 
10+  */ 
11+ 
12+ public  class  Solution  {
13+  public  int  lengthOfLastWord (String  s ) {
14+  int  p  = s .length () - 1 ;
15+  while  (p  >= 0  && s .charAt (p ) == ' ' ) p --;
16+  int  end  = p ;
17+  while  (p  >= 0  && s .charAt (p ) != ' ' ) p --;
18+  return  end  - p ;
19+  }
20+ 
21+  public  static  void  main (String [] args ) {
22+  Solution  solution  = new  Solution ();
23+  System .out .println (solution .lengthOfLastWord ("word " ));
24+  System .out .println (solution .lengthOfLastWord ("hello world" ));
25+  }
26+ }
                         You can’t perform that action at this time. 
           
                  
0 commit comments