Skip to content

Commit 302ab59

Browse files
committed
leetcode stack
1 parent 1a3915d commit 302ab59

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

.gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/
25+
26+
###
27+
28+
elasticsearch.keystore
29+
30+
logs

pom.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>LeetCode-Java</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
</properties>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.geekidentity.leetcode.offer.stack;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 剑指 Offer 30. 包含min函数的栈
7+
* https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
8+
*/
9+
public class CQueue {
10+
private final Stack<Integer> inStack = new Stack<>();
11+
private final Stack<Integer> outStack = new Stack<>();
12+
13+
public CQueue() {
14+
15+
}
16+
17+
public void appendTail(int value) {
18+
inStack.push(value);
19+
}
20+
21+
public int deleteHead() {
22+
if (outStack.isEmpty()) {
23+
while (!inStack.isEmpty()) {
24+
outStack.push(inStack.pop());
25+
}
26+
}
27+
if (outStack.isEmpty()) {
28+
return -1;
29+
}
30+
return outStack.pop();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.geekidentity.leetcode.offer.stack;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 剑指 Offer 30. 包含min函数的栈
7+
* https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
8+
*/
9+
public class MinStack {
10+
11+
private final Stack<Integer> stack = new Stack<>();
12+
private final Stack<Integer> minStack = new Stack<>();
13+
14+
15+
public MinStack() {
16+
17+
}
18+
19+
public void push(int x) {
20+
stack.push(x);
21+
if (!minStack.isEmpty()) {
22+
int min = minStack.peek();
23+
if (x < min) {
24+
min = x;
25+
}
26+
minStack.push(min);
27+
} else {
28+
minStack.push(x);
29+
}
30+
}
31+
32+
public void pop() {
33+
if (!minStack.isEmpty()) {
34+
minStack.pop();
35+
stack.pop();
36+
}
37+
38+
}
39+
40+
public int top() {
41+
if (minStack.isEmpty()) {
42+
return -1;
43+
}
44+
return stack.peek();
45+
}
46+
47+
public int min() {
48+
if (minStack.isEmpty()) {
49+
return -1;
50+
}
51+
return minStack.peek();
52+
}
53+
54+
public static void main(String[] args) {
55+
MinStack stack = new MinStack();
56+
stack.push(1);
57+
stack.push(2);
58+
System.out.println(stack.top());
59+
System.out.println(stack.min());
60+
stack.pop();
61+
System.out.println(stack.min());
62+
System.out.println(stack.top());
63+
}
64+
}

0 commit comments

Comments
 (0)