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

Commit170380d

Browse files
author
applewjg
committed
Reverse Words in a String
1 parent5cb3977 commit170380d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎ReverseWordsInAString.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 13, 2014
4+
Problem: Reverse Words in a String
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/reverse-words-in-a-string/
7+
Notes:
8+
Given an input string, reverse the string word by word.
9+
10+
For example,
11+
Given s = "the sky is blue",
12+
return "blue is sky the".
13+
14+
Solution: 1. Reverse the raw string and reverse each word.
15+
2. Cannot do it In Place by Java. oops~.~
16+
*/
17+
18+
publicclassSolution {
19+
publicStringreverseWords(Strings) {
20+
StringBuffersb =newStringBuffer();
21+
for (inti =s.length() -1;i >=0;) {
22+
while (i >=0 &&s.charAt(i) ==' ') --i;
23+
StringBuffertemp =newStringBuffer();
24+
while (i >=0 &&s.charAt(i) !=' ') {
25+
temp.append(s.charAt(i--));
26+
}
27+
temp.reverse();
28+
if (sb.length() >0 &&temp.length() >0)sb.append(" ");
29+
sb.append(temp);
30+
}
31+
returnsb.toString();
32+
}
33+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp