Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8054e5c

Browse files
author
applewjg
committed
Min Stack
Change-Id: I9e549f77593c9dc1690a2620a8de952d3e287cad
1 parenta9d24c0 commit8054e5c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎MinStack.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Nov 14, 2014
4+
Problem: Min Stack
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/min-stack/
7+
Notes:
8+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
9+
push(x) -- Push element x onto stack.
10+
pop() -- Removes the element on top of the stack.
11+
top() -- Get the top element.
12+
getMin() -- Retrieve the minimum element in the stack.
13+
*/
14+
15+
classMinStack {
16+
privateStack<Integer>stack =newStack<>();
17+
privateStack<Integer>minStack =newStack<>();
18+
publicvoidpush(intx) {
19+
stack.push(x);
20+
if (minStack.isEmpty() ||x <=minStack.peek()) {
21+
minStack.push(x);
22+
}
23+
}
24+
publicvoidpop() {
25+
if (stack.pop().equals(minStack.peek()))minStack.pop();
26+
}
27+
publicinttop() {
28+
returnstack.peek();
29+
}
30+
publicintgetMin() {
31+
returnminStack.peek();
32+
}
33+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp