LeetCode 557. Reverse Words in a String III

时间:2021-03-05 15:47:36

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.

Example 1:

Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

 

first:

import java.util.Stack;

class Solution {
    public String reverseWords(String s) {
        Stack<Character> stack = new Stack<Character>();
        char[] array = s.toCharArray();
        //char[] output = new char[array.length];
        //ArrayList<Character> output = new ArrayList<Character>();
        StringBuilder sb = new StringBuilder();
        for(char c : array){
            if(c!=' '){
                stack.push(c);
            }else{
                while(!stack.empty()){
                    sb.append(stack.pop());
                }
                sb.append(' ');
            }
        }
        
        //Character[] outputArray = output.toArray(new Character[output.size()]);
        return sb.toString();
    }
}

 

result:

Submission Result: Wrong Answer 
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL "
Expected: "s'teL ekat edoCteeL tsetnoc"

 

 

second:

import java.util.Stack;

class Solution {
    public String reverseWords(String s) {
        Stack<Character> stack = new Stack<Character>();
        char[] array = s.toCharArray();
        //char[] output = new char[array.length];
        //ArrayList<Character> output = new ArrayList<Character>();
        StringBuilder sb = new StringBuilder();
        for(char c : array){
            if(c!=' '){
                stack.push(c);
            }else{
                while(!stack.empty()){
                    sb.append(stack.pop());
                }
                sb.append(' ');
            }
        }
        
        while(!stack.empty()){
            sb.append(stack.pop());
        }

        
        //Character[] outputArray = output.toArray(new Character[output.size()]);
        return sb.toString();
    }
}

 

result:

LeetCode 557. Reverse Words in a String III

re-try:

import java.util.Stack;
//import java.util.Vector;

class Solution {
    public String reverseWords(String s) {
        Stack<Character> stack = new Stack<Character>();
        char[] array = s.toCharArray();
        char[] output = new char[array.length];
        
        int endPointer = 0;
        for(char c : array){
            if(c!=' '){
                stack.push(c);
            }else{
                int size =stack.size();
                for(int i=0;i<size;i++){
                    output[endPointer+i]=stack.pop();
                }
                output[endPointer+stack.size()+1] = ' ';
                endPointer +=stack.size()+1;
                
            }
        }
        
        while(!stack.empty()){
            output[endPointer]=stack.pop();
            endPointer++;
        }

        
        //Character[] outputArray = output.toArray(new Character[output.size()]);
        return new String(output);
    }
}

 

result:

试着从StringBuilder和数组的角度去优化,但造成代码太复杂,不是一个好的选择。

solution里的第1种方法:

public class Solution {
    public String reverseWords(String s) {
        String words[] = s.split(" ");
        StringBuilder res=new StringBuilder();
        for (String word: words)
            res.append(new StringBuffer(word).reverse().toString() + " ");
        return res.toString().trim();
    }
}

 result:

LeetCode 557. Reverse Words in a String III

solution里的第2种第3种方法

比第一种慢。

conclusion:

很奇怪参考答案里的方法,最快的也才是50%+!另外感觉Stack比较慢!

还需要注意的是,要熟悉String和StringBuffer的方法,比如String.split和StringBuffer.reverse。