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,因为如果使用String的话,会多次建立对象,会很慢。

public class Solution {
    public String reverseWords(String s) {
        int len = s.length();
        if( len == 0)
            return s;
        StringBuffer result = new StringBuffer() ;
        int end = len-1,start = 0;
        while( end >= 0 && s.charAt(end) == ‘ ‘)
            end--;
        if( end == -1)
            return result.toString();
        while( start < len && s.charAt(start) == ‘ ‘)
            start++;
        int pos = end+1;
        for( int i = end ; i >= start ; i-- ){
            if( s.charAt(i) == ‘ ‘) {
                result.append(s.substring(i+1,pos));
                result.append(" ");
                while (i < end && s.charAt(i) == ‘ ‘)
                    i--;
                i++;
                pos = i;

            }
        }
        result.append(s.substring(start,pos));
        return result.toString();
    }
}
时间: 2024-07-31 14:33:42

leetcode 151. Reverse Words in a String --------- java的相关文章

[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 翻转字符串中的单词

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 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: 在字符串分割的时候

Java for 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". 解题思路: 本题方法多多,最简单的方式直接按“ ” spilt即可,JAVA实现如下: public String reverseWords(String s) { if (s == null || s.length()

【Leetcode】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". 二.分析 要注意几点:1.当字符串的头部或者尾部存在空格时,最后都将被消除 2.当两个子字符串之间的空格的个数大于1时,只要保留一个 解题思路:1.首先,将整个字符串进行反转 2.然后,使用split函数对字符串

Leetcode 151. Reverse Words in a String 解题报告

[Problem] 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. [Idea] 从左往右遍历一次,每当遇到空格暂停

LeetCode 151 reverse word 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. 这题遇到的困难主要是细节地方处理的不好:(1)空格的处理,(2

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". 1 public class Solution { 2 public String reverseWords(String s) { 3 if(s.equals(""))return s; 4 String

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();