155. Min Stack
선택한 UI 언어에 맞게 문제 텍스트를 러시아어에서 번역합니다. 코드는 변경하지 않습니다.
Разработайте стек, который поддерживает операции добавления elementа, удаления elementа, получения верхнего elementа и извлечения минимального elementа за постоянное время.
Реализуйте класс MinStack:
MinStack() инициализирует объект стека.
void push(int val) добавляет element val в стек.
void pop() удаляет element на вершине стека.
int top() returns верхний element стека.
int getMin() извлекает minimum element в стеке.
C# 해법
매칭됨/원본public 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++ 해법
자동 초안, 제출 전 검토#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 해법
매칭됨/원본class 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 해법
매칭됨/원본function 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 해법
매칭됨/원본class 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 해법
매칭됨/원본type 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
}
Vacancies for this task
활성 채용 with overlapping task tags are 표시됨.
아직 활성 채용이 없습니다.