# LeetCode:Reverse Words in a String

### Reverse Words in a String

Total Accepted: 102953 Total Submissions: 655369 Difficulty: Medium

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.

click to show clarification.

Subscribe to see which companies asked this question

s中两边可能有空格，单词之间可能有多个空格，这些都需要去掉。

java code:

public class Solution {
public String reverseWords(String s) {

String[] strs = s.trim().split("\\s+");

String ans = "";
for(int i=strs.length-1;i>=0;i--) {
if(i!=0)
ans += strs[i]+" ";
else
ans += strs[i];
}

return ans;

}
}

#### [leetcode] 151. Reverse Words in a String 解题报告

2016-01-18 14:40:11

#### [LeetCode]557. Reverse Words in a String III(反转字符串 III)

2017-04-10 00:46:36

#### LeetCode(151)Reverse Words in a String

2014-03-10 02:47:53

#### leetcode 151 Reverse Words in a String java 算法

2016-07-20 19:37:08

#### 【LeetCode】Reverse Words in a String III 解题报告

2017-04-12 11:48:48

#### [leetcode: Python]557. Reverse Words in a String III

2017-05-19 16:17:34

#### python：leetcode之reverse words in a string

2016-02-21 17:30:27

#### LeetCode 186. Reverse Words in a String II（反转单词）

2016-05-25 01:36:04

#### 【Leetcode】557. Reverse Words in a String III

2017-04-09 13:18:08

#### leetcode151-Reverse Words in a String(翻转字符串单词的位置)

2016-04-03 11:21:01