Skip to content

Commit 69b90ab

Browse files
author
applewjg
committed
LengthofLastWord
Change-Id: I3db3c8b5e2ffbfcb06f1c74d9ff6020362b93041
1 parent bf0ced5 commit 69b90ab

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

LengthofLastWord.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 25, 2014
4+
Problem: Length of Last Word
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/length-of-last-word/
7+
Notes:
8+
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
9+
If the last word does not exist, return 0.
10+
Note: A word is defined as a character sequence consists of non-space characters only.
11+
For example,
12+
Given s = "Hello World",
13+
return 5.
14+
15+
Solution: ...
16+
*/
17+
public class Solution {
18+
public int lengthOfLastWord(String s) {
19+
int res = 0, i = s.length() - 1;
20+
while (i >= 0 && s.charAt(i) == ' ') --i;
21+
while (i >= 0 && s.charAt(i) != ' ') {
22+
--i; ++res;
23+
}
24+
return res;
25+
}
26+
}

0 commit comments

Comments
 (0)