Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return
"blue is sky the".
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 |
public class Solution { public
String reverseWords(String s) { StringBuffer reverse = new
StringBuffer(); int
len = s.length(); if(len > 0){ int
end = 0; int
start = 0; int
length = 0; while(start < len){ if(s.charAt(start) == 32){ ++start; ++end; } else
{ if(end < len && s.charAt(end) != 32) ++end; else{ reverse.insert(0, " "+ s.substring(start, end)); start = end; length += (end - start + 1); } } } if(length > 0) reverse = reverse.deleteCharAt(0); } return
reverse.toString(); }} |
leetcode--Reverse Words in a String,布布扣,bubuko.com
leetcode--Reverse Words in a String
原文:http://www.cnblogs.com/averillzheng/p/3591481.html