File tree Expand file tree Collapse file tree 4 files changed +120
-0
lines changed
src/main/java/leetcode/_58_ Expand file tree Collapse file tree 4 files changed +120
-0
lines changed Original file line number Diff line number Diff line change
1
+ ### [ 58\. Length of Last Word] ( https://leetcode.com/problems/length-of-last-word/ )
2
+
3
+ Difficulty: ** Easy**
4
+
5
+
6
+ Given a string _ s_ consists of upper/lower-case alphabets and empty space characters ` ' ' ` , return the length of last word in the string.
7
+
8
+ If the last word does not exist, return 0.
9
+
10
+ ** Note:** A word is defined as a character sequence consists of non-space characters only.
11
+
12
+ ** Example:**
13
+
14
+ ```
15
+ Input: "Hello World"
16
+ Output: 5
17
+ ```
18
+
19
+
20
+ #### Solution
21
+
22
+ Language: ** Java**
23
+
24
+ ``` java
25
+ class Solution {
26
+ public int lengthOfLastWord (String s ) {
27
+ if (s == null ) {
28
+ return 0 ;
29
+ }
30
+ s = s. trim();
31
+ char [] chars = s. toCharArray();
32
+ int length = 0 ;
33
+ for (int i = chars. length - 1 ; i >= 0 ; i-- ) {
34
+ if (chars[i] != ' ' ) {
35
+ length++ ;
36
+ } else {
37
+ break ;
38
+ }
39
+ }
40
+ return length;
41
+ }
42
+ }
43
+ ```
44
+ ![ ] ( http://ww1.sinaimg.cn/large/006tNc79ly1g4xg3lcobsj31bk0s2gqt.jpg )
Original file line number Diff line number Diff line change
1
+ package leetcode ._58_ ;
2
+
3
+ /**
4
+ * Created by zhangbo54 on 2019-03-04.
5
+ */
6
+ public class Main {
7
+ public static void main (String [] args ) {
8
+ Solution solution = new Solution ();
9
+ System .out .println (solution .lengthOfLastWord ("a" ));
10
+ }
11
+ }
12
+
Original file line number Diff line number Diff line change
1
+ package leetcode ._58_ ;
2
+
3
+ class Solution {
4
+ public int lengthOfLastWord (String s ) {
5
+ if (s == null ) {
6
+ return 0 ;
7
+ }
8
+ s = s .trim ();
9
+ char [] chars = s .toCharArray ();
10
+ int length = 0 ;
11
+ for (int i = chars .length - 1 ; i >= 0 ; i --) {
12
+ if (chars [i ] != ' ' ) {
13
+ length ++;
14
+ } else {
15
+ break ;
16
+ }
17
+ }
18
+ return length ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ ### [ 58\. Length of Last Word] ( https://leetcode.com/problems/length-of-last-word/ )
2
+
3
+ Difficulty: ** Easy**
4
+
5
+
6
+ Given a string _ s_ consists of upper/lower-case alphabets and empty space characters ` ' ' ` , return the length of last word in the string.
7
+
8
+ If the last word does not exist, return 0.
9
+
10
+ ** Note:** A word is defined as a character sequence consists of non-space characters only.
11
+
12
+ ** Example:**
13
+
14
+ ```
15
+ Input: "Hello World"
16
+ Output: 5
17
+ ```
18
+
19
+
20
+ #### Solution
21
+
22
+ Language: ** Java**
23
+
24
+ ``` java
25
+ class Solution {
26
+ public int lengthOfLastWord (String s ) {
27
+ if (s == null ) {
28
+ return 0 ;
29
+ }
30
+ s = s. trim();
31
+ char [] chars = s. toCharArray();
32
+ int length = 0 ;
33
+ for (int i = chars. length - 1 ; i >= 0 ; i-- ) {
34
+ if (chars[i] != ' ' ) {
35
+ length++ ;
36
+ } else {
37
+ break ;
38
+ }
39
+ }
40
+ return length;
41
+ }
42
+ }
43
+ ```
44
+ ![ ] ( http://ww1.sinaimg.cn/large/006tNc79ly1g4xg3lcobsj31bk0s2gqt.jpg )
You can’t perform that action at this time.
0 commit comments