Skip to content

Commit a3fe473

Browse files
[N-0] refactor 58
1 parent 616789b commit a3fe473

File tree

1 file changed

+19
-13
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+19
-13
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_58.java

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
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.
57
68
Note: A word is defined as a character sequence consists of non-space characters only.
79
810
For example,
911
Given s = "Hello World",
10-
return 5.*/
12+
return 5.
13+
14+
*/
1115
public class _58 {
1216

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;
2128
}
22-
return s.length() - n - 1;
2329
}
2430

2531
}

0 commit comments

Comments
 (0)