关闭

LeetCode:Reverse Words in a String

2615人阅读 评论(0) 收藏 举报
分类:

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

Hide Tags
 String
Hide Similar Problems
 (M) Reverse Words in a String II






















思路:

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;
        
    }
}


0
0
查看评论

LeetCode 186. Reverse Words in a String II(反转单词)

原题网址:https://leetcode.com/problems/reverse-words-in-a-string-ii/ Given an input string, reverse the string word by word. A word is defined as a sequ...
  • jmspan
  • jmspan
  • 2016-05-25 01:36
  • 805

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

题目链接:https://leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reverse the string word by word. For example, Given s = "...
  • qq508618087
  • qq508618087
  • 2016-01-18 14:40
  • 1562

LeetCode(151)Reverse Words in a String

题目如下: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". ...
  • feliciafay
  • feliciafay
  • 2014-03-10 02:47
  • 3772

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

【LeetCode】Reverse Words in a String III 解题报告标签(空格分隔): LeetCode题目地址:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description题目描述:Given...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017-04-12 11:48
  • 1113

Leetcode 557. Reverse Words in a String III

题目:Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order...
  • jessir
  • jessir
  • 2017-04-10 19:48
  • 1007

(LeetCode) Reverse Words in a String

problem :Reverse Words in a String Given an input string, reverse the string word by word. For example, Given s = "the sky is ...
  • zhangyuehuan
  • zhangyuehuan
  • 2014-04-13 09:26
  • 8222

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

557. Reverse Words in a String IIIGiven a string, you need to reverse the order of characters in each word within a sentence while still preserving wh...
  • whl_program
  • whl_program
  • 2017-04-10 00:46
  • 958

【LeetCode刷题Java版】Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". clic...
  • bruce_6
  • bruce_6
  • 2014-10-13 14:21
  • 2118

LeetCode 557 Reverse Words in a String III(逆转字符串)

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. ...
  • qq_26658823
  • qq_26658823
  • 2017-04-14 19:45
  • 449

【Leetcode】557. Reverse Words in a String III

思路: 用空格分割字符串,对每个子串,逆序,添加到结果数组。 public class Solution { public String reverseWords(String s) { String[] strs = s.split(" "); ...
  • zhouziyu2011
  • zhouziyu2011
  • 2017-04-09 13:18
  • 1792
    个人资料
    • 访问:391564次
    • 积分:6806
    • 等级:
    • 排名:第4121名
    • 原创:321篇
    • 转载:1篇
    • 译文:0篇
    • 评论:34条
    博客专栏
    最新评论