Amazon10月10日網上筆試題2 – JAVA編程語言程序開發技術文章

題目簡介:指定入棧順序和出棧順序,打印入棧出棧的次序
例子:入棧123,出棧213,則打印push1|push2|pop2|pop1|push3|pop3
具體代碼如下:(當時寫的代碼,未優化)
[java] 
import java.util.Scanner; 
import java.util.Stack; 
import java.util.StringTokenizer; 
 
public class Solution2  

    private String calculateOperationSequence(int[] originalArray, int[] resultArray)  
    { 
        // your code is here 
        Stack s = new Stack(); 
        String temp=""; 
        int temp_r = resultArray[0]; 
        int temp_r_index = 0; 
        int count = 0; 
        if(originalArray.length==resultArray.length){ 
            for(int i=0;i<originalArray.length;i++){ 
                s.push(originalArray[i]); 
                temp = temp+"|"+"push"+String.valueOf(i+1); 
                count++; 
                while(true){ 
                    if(s.empty()){ 
                        break; 
                    }else{ 
                        if(Integer.parseInt(s.peek().toString())==temp_r){ 
                            temp = temp+"|"+"pop"+s.peek().toString(); 
                            count++; 
                            if(temp_r_index==resultArray.length-1){ 
                                 
                            }else{ 
                                temp_r_index++; 
                                temp_r = resultArray[temp_r_index]; 
                            } 
                            s.pop(); 
                        }else{ 
                            break; 
                        } 
                    } 
                } 
            } 
            temp = temp.substring(1, temp.length()); 
 
            if(count==originalArray.length + resultArray.length){ 
                return temp; 
            }else{ 
                return "None"; 
            } 
        }else{ 
            return "None"; 
        } 
         
    } 
     
    public static void main(String[] args)  
    { 
        Solution2 solution = new Solution2(); 
        Scanner scanner = new Scanner(System.in); 
         
        while (scanner.hasNextLine())  
        { 
            String strLine1 = scanner.nextLine(); 
            StringTokenizer stringTokenizer1 = new StringTokenizer(strLine1); 
             
            //Initialize the original array 
            int arrayLength = stringTokenizer1.countTokens(); 
            int[] originalArray = new int[arrayLength]; 
            for(int i = 0; i < arrayLength; i++) 
            { 
                originalArray[i] = Integer.parseInt(stringTokenizer1.nextToken()); 
            } 
             
            //Initialize the result array 
            String strLine2 = scanner.nextLine(); 
            StringTokenizer stringTokenizer2 = new StringTokenizer(strLine2); 
            arrayLength = stringTokenizer2.countTokens(); 
            int[] resultArray = new int[arrayLength]; 
            for(int j = 0; j < arrayLength; j++) 
            { 
                resultArray[j] = Integer.parseInt(stringTokenizer2.nextToken()); 
            } 
             
            String operationSequence = solution.calculateOperationSequence(originalArray, resultArray); 
            System.out.println(operationSequence); 
        } 
    } 

總結:最開始用數組做的,很麻煩。然後靈光一閃,隻需要模擬出入棧和出棧的過程順便打印就可以瞭,之後就是用瞭stack幾分鐘就完成瞭。
[java] 
<pre></pre> 
<pre></pre> 
<pre></pre> 

發佈留言