We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see ourdocumentation.
There was an error while loading.Please reload this page.
1 parent5cb3977 commit170380dCopy full SHA for 170380d
ReverseWordsInAString.java
@@ -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
+}