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.

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:

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:

Trying to optimize from the perspective of StringBuilder and arrays, but the code is too complicated and not a good choice.

The first method in the solution:

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:

The second and third methods in the solution :

slower than the first.

conclusion:

It is very strange to refer to the method in the answer, the fastest is 50%+! In addition, I feel that Stack is slower!

It's also important to be familiar with String and StringBuffer methods, such as String.split and StringBuffer.reverse.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324958663&siteId=291194637