【问题标题】:Java - Stacks - checking if 2 numbers of the stack equal 100Java - 堆栈 - 检查堆栈的 2 个数字是否等于 100
【发布时间】:2012-03-16 16:02:28
【问题描述】:

我必须检查堆栈中哪两个值的总和等于 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);
        }
    }
}
}

【问题讨论】:

  • 基本上堆栈用于编程语言的语法检查(在编译器中)和一些服务策略实现,如 LIFO。在你的情况下,堆栈不是最好的数据结构。

标签: java arrays stack


【解决方案1】:

Stack 是 Collection,它实现了 the toArray(T[]) method,因此您可以使用它将堆栈转换为数组并使用您的工作数组解决方案。

但是,您会遇到数组没有自动装箱的问题。自动装箱会自动在原始类型和对象之间进行转换,这意味着,例如,您可以将 int 值直接添加到您的 Stack 而无需创建 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);
}

创建一次数组将比重复克隆和清空堆栈更有效。

(如果这是一个旨在教你如何使用堆栈的家庭作业问题,这可能不是最好的方法!)

【讨论】:

    【解决方案2】:

    第二个堆栈的索引可能不正确,因为当您在循环内克隆初始堆栈时,每次迭代它都会变小。

    即

    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...
    

    【讨论】:

      【解决方案3】:

      这是对堆栈的不自然使用(想知道在'面向堆栈'的 Forth 中如何做到这一点?你会使用数组。),所以你在挣扎是正确的。也就是说,只需在实现这些堆栈运算符后使用您的数组解决方案:

      1. DEPTH,返回栈中元素的个数。

      2. PICK,将索引处的元素返回到堆栈中。

      如果您被允许使用它们,java.util.Stack 会同时继承:.size() 和 .elementAt()

      【讨论】:

        【解决方案4】:

        游览逻辑变化不大,不要在循环中使用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);
                        }
                    }
                }
            }
        

        【讨论】:

          【解决方案5】:

          您的代码似乎给出了正确的数字组合,但未能给出正确的索引组合。

          这是因为您正在调用 pop 函数,该函数从堆栈中删除一个项目,从而将其大小减少 1。因此,您获得的索引是与该时刻堆栈大小相比的索引。

          相反,我建议使用peek() 或get(int index) 函数来读取值。我已经用get(index) 更新了您的示例,而没有克隆堆栈...看看...

          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);
                          }
                      }
                  }
              }
          }
          }
          

          【讨论】:

          • 我试过你的方法,但我不知道我只得到 3 个输出。使用数组我得到更多的值作为输出。
          【解决方案6】:

          您的堆栈基础解决方案的问题在于这一行。 int value = (Integer) stack.pop(); 一旦你弹出第一个元素,它将从堆栈中消失

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-12-19
            • 2011-09-01
            • 2021-04-28
            • 1970-01-01
            • 2013-12-06
            • 2011-01-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多