File tree 1 file changed +19
-13
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +19
-13
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
- /**Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
3
-
4
- If the last word does not exist, return 0.
2
+ /**
3
+ * 58. Length of Last Word
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
+ * If the last word does not exist, return 0.
5
7
6
8
Note: A word is defined as a character sequence consists of non-space characters only.
7
9
8
10
For example,
9
11
Given s = "Hello World",
10
- return 5.*/
12
+ return 5.
13
+
14
+ */
11
15
public class _58 {
12
16
13
- public int lengthOfLastWord (String s ) {
14
- if (s == null || s .length () == 0 ) {
15
- return 0 ;
16
- }
17
- s = s .trim ();
18
- int n = s .length () - 1 ;
19
- while (n >= 0 && s .charAt (n ) != ' ' ) {
20
- n --;
17
+ public static class Solution1 {
18
+ public int lengthOfLastWord (String s ) {
19
+ if (s == null || s .length () == 0 ) {
20
+ return 0 ;
21
+ }
22
+ s = s .trim ();
23
+ int n = s .length () - 1 ;
24
+ while (n >= 0 && s .charAt (n ) != ' ' ) {
25
+ n --;
26
+ }
27
+ return s .length () - n - 1 ;
21
28
}
22
- return s .length () - n - 1 ;
23
29
}
24
30
25
31
}
You can’t perform that action at this time.
0 commit comments