2012-02-27 69 views
4

我必須檢查堆棧中的兩個值的和等於100,並打印出indecis和數字。我已經使用數組實現了這種可能性,但是我無法使用堆棧使其工作。請幫幫我。直到現在我寫下了以下內容,但沒有給出正確的結果。Java - 堆棧 - 檢查堆棧的兩個數字是否相等100

import java.util.Stack; 

public class 2 { 

public static void main(String[] args) { 

    int x = 100; 
    Stack stack=new Stack(); 
    Stack tempStack=new Stack(); 
    stack.push(new Integer(20)); 
    stack.push(new Integer(53)); 
    stack.push(new Integer(41)); 
    stack.push(new Integer(38)); 
    stack.push(new Integer(28)); 
    stack.push(new Integer(47)); 
    stack.push(new Integer(70)); 
    stack.push(new Integer(30)); 
    stack.push(new Integer(80)); 
    stack.push(new Integer(400)); 
    stack.push(new Integer(3)); 
    stack.push(new Integer(20)); 

    tempStack = (Stack) stack.clone(); 
    for (int i=0; i<stack.size(); i++) { 
     tempStack = (Stack) stack.clone(); 
     int value = (Integer) stack.pop(); 
     if (!stack.isEmpty()) { 
      for (int k=0; k<tempStack.size(); k++) { 
       int tmp = (Integer) tempStack.pop(); 
       if ((value + tmp) == x) { 
        System.out.println("Indices " + i + " & " + k + " with values " 
          + value + " & " + tmp); 
       } 
      } 
     } 
    } 
} 
} 

以下是我的基於陣列的解決方案:

public class 1 { 

public static void main(String[] args) { 

    int x = 100; 
    int [] array = {20,3,400,80,30,70,20,47,28,38,41,53,20}; 
    for (int i=0; i<array.length; i++){ 
     int temp1 = array[i]; 
     for (int k=1; k<array.length; k++) { 
      int temp2 = array[k]; 
      if ((temp1+temp2)==x) 
       System.out.println("Indices " + i + " & " + k + " with values " 
         + temp1 + " & " + temp2); 
     } 
    } 
} 
} 
+0

基本堆棧用於編程語言的語法檢查(在編譯器中)和一些服務策略實現,如LIFO。在你的情況堆棧不是最好的數據結構。 – 2012-02-27 06:27:12

回答

4

作爲StackCollection它實現the toArray(T[]) method,所以你可以用它來你的籌碼轉換成數組,並使用你的工作陣列的解決方案。

但是,您將遇到沒有數組自動裝箱的問題。自動自動裝箱原始類型和對象,這意味着,例如,您可以向Stack直接添加int值,而無需創建Integer對象之間進行轉換,因爲編譯器可以實現這個要求:

Stack<Integer> stack = new Stack<Integer>(); 
stack.push(20); 
stack.push(53); 

然而,編譯器將不int[]Integer[]之間進行轉換,所以你不得不做的事:

Integer[] array = stack.toArray(new Integer[stack.size()]); 

而且使用Integer[]將是一個苦差事。

所以最容易做的事情是這樣的:

int[] array = new int[stack.size()]; 

for (int i = 0; i < array.length; i++) { 
    array[i] = stack.get(i); 
} 

創建數組一次會比多次克隆和排空堆棧更有效。

(雖然如果這是打算教你如何使用堆棧,這可能不是最好的方法!一門功課的問題)

+0

乾淨,簡單和高效 – RAY 2012-02-27 06:15:05

1
在旅遊邏輯

變化不大,不參加循環stack.size(),它遞減在每個循環迭代所以ü迭代僅半環狀

int stackSize = stack.size(); 
    for (int i=0; i<stackSize; i++) { 
     tempStack = (Stack) stack.clone(); 
     int value = (Integer) stack.pop(); 
     if (!stack.isEmpty()) { 
      int tempSize = tempStack.size(); 
      for (int k=0; k<tempSize; k++) { 
       int tmp = (Integer) tempStack.pop(); 
       if ((value + tmp) == x) { 
        System.out.println("Indices " + i + " & " + k + " with values " 
          + value + " & " + tmp); 
       } 
      } 
     } 
    } 
1

第二堆棧的索引可能不是當你克隆循環內的初始堆棧是正確的,它是較小的每次迭代。

stack = {25,50} 
stack.clone => {25,50} 
stack.pop => 25 
stack.clone => {50} 
thus, if 50+50== 100 the indicies found would be i=1, k=0 instead of 1,1... 
1

您的代碼似乎給數字的正確組合,但未能給予指標的正確組合。

這是因爲您打電話給pop函數,該函數會從堆棧中移除一個項目,從而將其大小減少1個。因此,您得到的索引是與該時刻的堆棧大小相比的索引。

相反,我會建議,使用peek()get(int index)函數來讀取值。我已經更新了get(index)你的榜樣,而不克隆堆棧... HV看看...

import java.util.Stack; 

public class Class2 { 

public static void main(String[] args) { 

    int x = 100; 
    Stack stack=new Stack(); 
    Stack tempStack=new Stack(); 
    stack.push(new Integer(20)); 
    stack.push(new Integer(53)); 
    stack.push(new Integer(41)); 
    stack.push(new Integer(38)); 
    stack.push(new Integer(28)); 
    stack.push(new Integer(47)); 
    stack.push(new Integer(70)); 
    stack.push(new Integer(30)); 
    stack.push(new Integer(80)); 
    stack.push(new Integer(400)); 
    stack.push(new Integer(3)); 
    stack.push(new Integer(20)); 

    // tempStack = (Stack) stack.clone(); 
    for (int i=0; i<stack.size(); i++) { 
     // tempStack = (Stack) stack.clone(); 
     int value = (Integer) stack.get(i); 
     if (!stack.isEmpty()) { 
      for (int k=i+1; k<stack.size()-1; k++) { 
       int tmp = (Integer) stack.get(k); 
       System.out.println("Value"+value+" tmp "+tmp+"Stack size"+stack.size()); 
       if ((value + tmp) == x) { 
        System.out.println("Indices " + i + " & " + k + " with values " 
          + value + " & " + tmp); 
       } 
      } 
     } 
    } 
} 
} 
+0

我試過你的方式,但我不知道我只得到3個輸出。使用該數組,我獲得更多的值作爲輸出。 – user1234905 2012-02-27 15:19:27

1

這是一種不自然的使用的堆垛(想知道怎麼你會在「面向堆棧做到這一點'第四?你會使用一個數組。),所以你只是在努力掙扎。這就是說,只要用你的陣列的解決方案實現這些堆棧運營後:

  1. 深度,這將返回堆棧上的元素的數量。

  2. PICK,它將索引處的元素返回到堆棧中。

如果你允許使用它們,java.util.Stack中繼承了:.size().elementAt()

1

問題與你的籌碼基地的解決方案是在這條線。 int value =(Integer)stack.pop(); 一旦你彈出第一個元素,它將從堆棧中消失