[leedcode 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".

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

public class Solution {
    public String reverseWords(String s) {
        //利用java中StringBuilder的insert(0,String)函数,注意第一个字符是空格的情况
        //因此需要检查word的长度
        if(s==null||s.length()<=0) return s;
        StringBuilder word=new StringBuilder();
        StringBuilder res=new StringBuilder();
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)!=‘ ‘) word.append(s.charAt(i));
            else if(s.charAt(i)==‘ ‘){
                if(word.length()>0){
                    res.insert(0,word+" ");
                    word=new StringBuilder();
                }else{
                    continue;
                }
            }
        }
        if(word.length()>0) res.insert(0,word+" ");
        return res.toString().trim();
    }
}
时间: 2024-12-15 06:23:22

[leedcode 151] Reverse Words in a String的相关文章

[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". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. click to show clarification.

leetCode 151. Reverse Words in a String 字符串反转 | Medium

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". 题目大意: 输入一个字符串,将单词序列反转. 思路1: 采用一个vector,来存放中间结果 将vector的结果倒序放入原字符串中. 思路2: 在字符串分割的时候

151. Reverse Words in a String &amp;&amp; 61. Rotate List &amp;&amp; 189. Rotate Array

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". What constitutes a word?A sequence of non-space characters constitutes a word. Cou

[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". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

Leetcode 448. Find All Numbers Disappeared in an Array JAVA语言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". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. 题意:反转字符串中的单词,注意空格的处理!!! public 

151. Reverse Words in a String (String)

思路: 本题考查的目的并不是使用字符串的函数.方法是两次reverse,先对每个单词先做一次翻转,然后对整个字符串做一次翻转. 需要注意的是去除extra space,并且对全space字符串.以及最后一个单词要做特殊处理. class Solution { public: void reverseWords(string &s) { int start = 0; int end=-1; int i = 0; int cur = 0; // point to the string with ex

leetcode 151. Reverse Words in a String --------- java

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. 倒序字符串. 注意使用BufferString,因为如果使用Stri

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". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. click to show clarification. Clari

(String)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". public class Solution { public String reverseWords(String s) { String afterTrim= s.trim(); String[] split=afterTr