Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit69b90ab

Browse files
author
applewjg
committed
LengthofLastWord
Change-Id: I3db3c8b5e2ffbfcb06f1c74d9ff6020362b93041
1 parentbf0ced5 commit69b90ab

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, wangjingui@outlook.com
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+
publicclassSolution {
18+
publicintlengthOfLastWord(Strings) {
19+
intres =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+
returnres;
25+
}
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp