zoukankan      html  css  js  c++  java
  • 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:

    试着从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:

    solution里的第2种第3种方法

    比第一种慢。

    conclusion:

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

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

  • 相关阅读:
    如何避免JavaScript的内存泄露及内存管理技巧
    【跟我一起学python吧】python chr()、unichr()和ord()
    阿里云,实力与担当并存!
    首届阿里白帽大会成功举办,用技术“肩天下”
    DataV数据可视化年度峰会——唤醒数据,看见未来
    支付宝移动端 Hybrid 解决方案探索与实践
    大数据上云第一课:(1)MaxCompute授权和外表操作躲坑指南
    函数计算支持应用中心功能
    Serverless 解惑——函数计算如何访问 MySQL 数据库
    开发函数计算的正确姿势——使用交互模式安装依赖
  • 原文地址:https://www.cnblogs.com/hzg1981/p/8947949.html
Copyright © 2011-2022 走看看