155. Min Stack
El texto de la tarea se traduce del ruso para el idioma seleccionado. El código no cambia.
Разработайте стек, который поддерживает операции добавления elementа, удаления elementа, получения верхнего elementа и извлечения минимального elementа за постоянное время.
Реализуйте класс MinStack:
MinStack() инициализирует объект стека.
void push(int val) добавляет element val в стек.
void pop() удаляет element на вершине стека.
int top() returns верхний element стека.
int getMin() извлекает minimum element в стеке.
C# solución
coincidente/originalpublic class MinStack {
private Stack<int[]> stack = new Stack<int[]>();
public MinStack() {
}
public void Push(int x) {
if (stack.Count == 0) {
stack.Push(new int[] { x, x });
return;
}
int current_min = stack.Peek()[1];
stack.Push(new int[] { x, Math.Min(x, current_min) });
}
public void Pop() {
stack.Pop();
}
public int Top() {
return stack.Peek()[0];
}
public int GetMin() {
return stack.Peek()[1];
}
}
C++ solución
borrador automático, revisar antes de enviar#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
public class MinStack {
private stack<int[]> stack = new stack<int[]>();
public MinStack() {
}
public void Push(int x) {
if (stack.size() == 0) {
stack.push(new int[] { x, x });
return;
}
int current_min = stack.Peek()[1];
stack.push(new int[] { x, min(x, current_min) });
}
public void Pop() {
stack.pop();
}
public int Top() {
return stack.Peek()[0];
}
public int GetMin() {
return stack.Peek()[1];
}
}
Java solución
coincidente/originalclass MinStack {
private Stack<int[]> stack = new Stack<>();
public MinStack() {}
public void push(int x) {
if (stack.isEmpty()) {
stack.push(new int[] { x, x });
return;
}
int currentMin = stack.peek()[1];
stack.push(new int[] { x, Math.min(x, currentMin) });
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek()[0];
}
public int getMin() {
return stack.peek()[1];
}
}
JavaScript solución
coincidente/originalfunction last(arr) {
return arr[arr.length - 1];
}
class MinStack {
_stack = [];
push(x) {
if (this._stack.length === 0) {
this._stack.push([x, x]);
return;
}
const currentMin = last(this._stack)[1];
this._stack.push([x, Math.min(currentMin, x)]);
}
pop() {
this._stack.pop();
}
top() {
return last(this._stack)[0];
}
getMin() {
return last(this._stack)[1];
}
}
Python solución
coincidente/originalclass MinStack:
def __init__(self):
self.stack = []
def push(self, x: int) -> None:
if not self.stack:
self.stack.append((x, x))
return
current_min = self.stack[-1][1]
self.stack.append((x, min(x, current_min)))
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.stack[-1][1]
Go solución
coincidente/originaltype MinStack struct {
stack [][]int
}
func Constructor() MinStack {
return MinStack{stack: make([][]int, 0)}
}
func (this *MinStack) Push(val int) {
if len(this.stack) == 0 {
this.stack = append(this.stack, []int{val, val})
return
}
currentMin := this.stack[len(this.stack)-1][1]
this.stack = append(this.stack, []int{val, min(val, currentMin)})
}
func (this *MinStack) Pop() {
this.stack = this.stack[:len(this.stack)-1]
}
func (this *MinStack) Top() int {
return this.stack[len(this.stack)-1][0]
}
func (this *MinStack) GetMin() int {
return this.stack[len(this.stack)-1][1]
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
Vacantes para esta tarea
Se muestran vacantes activas con etiquetas coincidentes.
Todavía no hay vacantes activas.