JavaRush /Java 博客 /Random-ZH /喝咖啡休息#162。使用队列实现堆栈。Java Math 类方法

喝咖啡休息#162。使用队列实现堆栈。Java Math 类方法

已在 Random-ZH 群组中发布

使用队列实现堆栈

来源:Hackernoon 本文的内容致力于解决仅使用两个队列实现堆栈的问题。 喝咖啡休息#162。 使用队列实现堆栈。 Java Math 类方法 - 1我们在 Leetcode 中遇到了一个问题

仅使用两个队列实现后进先出 (LIFO) 堆栈。实现的堆栈必须支持常规堆栈的所有功能(pushtoppopempty)。

实现 MyStack 类:

  • void push(int x)将元素 x 推入栈顶。

  • int pop()删除栈顶元素并返回。

  • int top()返回栈顶元素。

  • booleanempty()如果堆栈为空则返回true ,否则返回 false

额外细节:

  • 您应该只使用标准队列操作,这意味着只允许push to backpeek/pop from frontsizeisempty操作。

  • 根据您的编程语言,队列可能不受本机支持。但是,如果您仅使用标准队列操作,则可以使用列表或双端队列(双队列)来模拟队列。

示例1:

输入

["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]

输出

[空、空、空、2、2、假]

解释

MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // возвращает 2
myStack.pop(); // возвращает 2
myStack.empty(); // возвращает False

问题的解决

第一个选项

在第一个解决方案中,我们将使用额外的内存:
static class MyStack {
  private Queue<Integer> current;
  private Queue<Integer> tmp;

  public MyStack() {
    current = new ArrayDeque<>();
    tmp = new ArrayDeque<>();
  }
对于这个解决方案,我们需要一个额外的队列。让我们从推送开始。对于推送操作,我们只需将一个新元素添加到当前队列中即可。
public void push(int x) {
  current.add(x);
}
这里的主要技巧是pop方法。这里我们将除最后一个之外的所有元素放入tmp队列中。 现在队列中的最后一个元素成为第一个发送的元素。
public int pop() {
  if (current.isEmpty()){
    return -1;
  }
  int size = current.size();
  for (int i = 0; i < size - 1; i ++){
    tmp.add(current.poll());
  }
  int ret = current.poll();
  current = tmp;
  return ret;
}
上面的方法 怎么样?此方法类似于pop
public int top() {
  if (current.isEmpty()){
    return -1;
  }
  int size = current.size();
  for (int i = 0; i < size - 1; i ++){
    tmp.add(current.poll());
  }
  int ret = current.peek();
  tmp.add(current.poll());
  current = tmp;
  return ret;
}
对于最后一个方法,我们应该简单地检查当前队列:
public boolean empty() {
  return current.isEmpty();
}

第二种解决方案

在此选项中,我们不会使用额外的内存:
static class MyStack {

  private Queue<Integer> current;

  public MyStack2() {
    current = new ArrayDeque<>();
  }
}
这里的主要思想是push方法。我们添加当前元素,然后将当前元素之前的元素移动到队列末尾。例如,如果我们有一个包含元素[2,1]的队列,我们​​使用push 3 - 并获取队列[3,2,1],然后是[1,3,2],[2,1,3]和。 .. 和所有。
public void push(int x) {
  current.add(x);
  int size = current.size();

  for (int i = 0; i < size-1; i ++){
    current.add(current.poll());
  }
}
在这里,我们已经拥有了所有其他方法的正确元素和队列的开头。
public int pop() {
  return current.poll();
}

public int top() {
  return current.peek();
}

public boolean empty() {
  return current.isEmpty();
}

Java Math 类方法

来源:Medium 在这篇文章中,您将了解 Java Math 类的功能和范围。 Java 语言中Java.lang.Math喝咖啡休息#162。 使用队列实现堆栈。 Java 数学类方法 - 2包的一部分是Math类。这个类为我们提供了一些预定义的方法,如minmaxsqrt等。以下是我们可以在日常工作中使用的数学课程中的一些方法的示例。1. max()方法返回参数中传递的较大数字:
System.out.println(Math.max(5,3));
OUTPUT: 5
2. sqrt()方法将向我们显示作为参数传递的数字的平方根:
System.out.println(Math.sqrt(4));
OUTPUT: 2.0
3. random()方法返回0到1范围内的随机数。
System.out.println(Math.random());
OUTPUT: 0.8685304957692445
4、abs()方法返回任何负数的绝对正值。例如,如果我们将其传递为 0,则结​​果将返回 0。
System.out.println(Math.abs(-4));
OUTPUT: 4
System.out.println(Math.abs(4));
OUTPUT: 4
System.out.println(Math.abs(0));
OUTPUT: 0
5.floor () 方法返回一个小于或等于参数且等于最接近的数学整数的双精度值。
System.out.println(Math.floor(-1.2));
OUTPUT: -2.0
System.out.println(Math.floor(1.2));
OUTPUT: 2.0
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION