class MinStack {
Deque<Integer> stack;
Deque<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
stack = new LinkedList<>();
minStack = new LinkedList<>();
}
public void push(int x) {
stack.offerLast(x);
if(minStack.isEmpty() || x <= minStack.peekLast()) {
minStack.offerLast(x);
}
}
public void pop() {
int i = stack.pollLast();
if(i == minStack.peekLast()) {
minStack.pollLast();
}
}
public int top() {
return stack.peekLast();
}
public int getMin() {
return minStack.peekLast();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
//定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,
class MinStack {
/** initialize your data structure here. */
Stack<Integer>A,B;
public MinStack() {
A = new Stack<>();
B = new Stack<>();
}
//将x压入栈
public void push(int x) {
A.add(x);
if(B.empty()||B.peek()>=x)
B.add(x);
}
public void pop() {
if(A.pop().equals(B.peek()))//使用.equals判断相等值
B.pop();
}
public int top() {
return A.peek();
}
public int getMin() {
return B.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
class MinStack {
Stack<Integer> stack1;
Stack<Integer> stack2;
/** initialize your data structure here. */
public MinStack() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
public void push(int x) {
stack1.push(x);
if (stack2.isEmpty() || stack2.peek() >= x) {
stack2.push(x);
}
}
public void pop() {
if (!stack1.isEmpty()) {
//将 Integer 对象转换为原始的 int 类型进行比较,这样就可以确保比较的是值而不是引用
if (stack1.peek().intValue() == stack2.peek().intValue()) {
stack2.pop();
}
stack1.pop();
}
}
public int top() {
return stack1.peek();
}
public int getMin() {
return stack2.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
Stack<Integer> stack1;
Stack<Integer> stack2;
/** initialize your data structure here. */
public MinStack() {
this.stack1 = new Stack();
this.stack2 = new Stack();
}
public void push(int x) {
stack1.push(x);
if(stack2.isEmpty() || x <= stack2.peek().intValue()){
stack2.push(x);
}
}
public void pop() {
if(!stack1.isEmpty()){
if(stack1.peek().intValue() == stack2.peek().intValue()){
stack2.pop();
}
stack1.pop();
}
}
public int top() {
return stack1.peek();
}
public int getMin() {
return stack2.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
stack.push(x);
if (minStack.empty() || x <= minStack.peek()) {
minStack.push(x);
}
}
public void pop() {
Integer v = stack.pop();
if (v.equals(minStack.peek())) {
minStack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
class MinStack {
Stack<Integer> A;
Stack<Integer> B;
/** initialize your data structure here. */
public MinStack() {
A = new Stack<Integer>();
B = new Stack<Integer>();
}
public void push(int x) {
//栈A存放所有数据,栈B只存放逐步递减的数据
A.add(x);
if(B.empty() || B.peek() >= x){
B.add(x);
}
}
public void pop() {
if(A.pop().equals(B.peek())){
B.pop();
}
}
public int top() {
return A.peek();
}
public int getMin() {
return B.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
java
class MinStack{
Stack A,B;
public MinStack(){
A = new Stack<>();
B = new Stack<>();
}
public void push(int x){
A.add(x);
if(B.empty() || B.peek() >= x)
B.add(x);
}
public void pop(){
if(A.pop().equals(B.peek()))
B.pop();
}
public int top(){
return A.peek();
}
public int getMin(){
return B.peek();
}
}
提交
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<Integer>();
minstack = new Stack<Integer>();
}
public void push(int x) {
stack.push(x);
if(minstack.isEmpty()){
minstack.push(x);
}else{
if(x<=minstack.peek()){
minstack.push(x);
}
}
}
public void pop() {
int p = stack.pop();
if(p == minstack.peek()){
minstack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return minstack.peek();
}
class MinStack {
Stack<Integer> A, B;
public MinStack() {
A = new Stack<>();
B = new Stack<>();
}
public void push(int x) {
A.add(x);
if(B.empty() || B.peek() >= x)
B.add(x);
}
public void pop() {
if(A.pop().equals(B.peek()))
B.pop();
}
public int top() {
return A.peek();
}
public int getMin() {
return B.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
StackA,B;
/** initialize your data structure here. */
public MinStack() {
A = new Stack();
B = new Stack();
}
public void push(int x) {
A.add(x);
if(B.empty() || B.peek()>= x){
B.add(x);
}
}
public void pop() {
if(A.pop().equals(B.peek())){
B.pop();
}
}
public int top() {
return A.peek();
}
public int getMin() {
return B.peek();
}
}
方法2:
class MinStack {
StackA;
private int min = Integer.MAX_VALUE;
/** initialize your data structure here. */
public MinStack() {
A = new Stack();
}
public void push(int x) {
if(x <= min){
A.add(min);
min = x;
}
A.add(x);
}
public void pop() {
if(A.pop().equals(min)){
min = A.pop();
}
}
public int top() {
return A.peek();
}
public int getMin() {
return min;
}
class MinStack {
Stack<Integer> stackA , stackB;
public MinStack() {
stackA = new Stack<>();
stackB = new Stack<>();
}
public void push(int x) {
stackA.add(x);
if (stackB.isEmpty() || stackB.peek() >= x) {
stackB.add(x);
}
}
public void pop() {
if (stackB.peek().equals(stackA.pop())) {
stackB.pop();
}
}
public int top() {
return stackA.peek();
}
public int getMin() {
return stackB.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
Stack<Integer> main ;
Stack<Integer> sup;
/** initialize your data structure here. */
public MinStack() {
main = new Stack<Integer>();
sup = new Stack<Integer>();
}
public void push(int x) {
if(sup.empty() || x <= sup.peek())
sup.push(x);
main.push(x);
}
public void pop() {
if(main.empty())
return ;
if( main.peek().equals(sup.peek()))
sup.pop();
main.pop();
}
public int top() {
int top = main.peek();
return top;
}
public int getMin() {
int min = sup.peek();
return min;
}
class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int val) {
stack.push(val);
if (minStack.isEmpty() || val <= minStack.peek().intValue()) {
minStack.push(val);
}
}
public void pop() {
if (!stack.isEmpty()) {
if (stack.pop().intValue() == minStack.peek().intValue()) {
minStack.pop();
}
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
Stack A, B;
/** initialize your data structure here. */
public MinStack() {
A = new Stack<>();
B = new Stack<>();
}
public void push(int x) {
A.add(x);
if(B.empty()||B.peek()>=x){
B.add(x);
}
}
public void pop() {
if(A.pop().equals(B.peek()))
B.pop();
}
public int top() {
return A.peek();
}
public int getMin() {
return B.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
思路:辅助栈存最小值
感觉这题比较简单,我感觉比那个链表的题好理解很多,可能原因在于这里没有那么多判断的条件吧,整体代码也比较好理解。
class MinStack {
Stack stack1 = new Stack();
Stack stack2 = new Stack();
/** initialize your data structure here. */
public MinStack() {
this.stack1 =stack1;
this.stack2 = stack2;
}
public void push(int x) {
stack1.push(x);
if(stack2.isEmpty() || x <= stack2.peek()){
stack2.push(x);
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
时间复杂度O(1),空间复杂度O(n)
/** initialize your data structure here. */
Deque<Integer> stack1;
Deque<Integer> stack2;
public MinStack() {
stack1 = new ArrayDeque<>();
stack2 = new ArrayDeque<>();
}
public void push(int x) {
stack1.addLast(x);
if(stack2.isEmpty()) stack2.addLast(x);
else{
int temp = stack2.getLast();
if(temp < x) stack2.addLast(temp);
else stack2.addLast(x);
}
}
public void pop() {
stack1.removeLast();
stack2.removeLast();
}
public int top() {
return stack1.getLast();
}
public int getMin() {
return stack2.getLast();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
Deque dataStack;
Deque minStack;
/** Integerlize your data structure here. */
public MinStack() {
this.dataStack = new ArrayDeque();
this.minStack = new ArrayDeque();
}
public void push(int x) {
dataStack.push(x);
if(minStack.isEmpty() || x <= minStack.peek().intValue()){
minStack.push(x);
}
}
public void pop() {
if(!dataStack.isEmpty()){
if(dataStack.peek().intValue() == minStack.peek().intValue()){
minStack.pop();
}
dataStack.pop();
}
}
public int top() {
return dataStack.peek();
}
public int getMin() {
return minStack.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
List<Integer> list;
int lowest = Integer.MAX_VALUE;//初始值为int的最大值
/** initialize your data structure here. */
public MinStack() {
list = new ArrayList<>();
}
public void push(int x) {
lowest = x<lowest?x:lowest;
list.add(x);
}
public void pop() {
int index = list.size()-1;
list.remove(index);
lowest = Min();
}
public int top() {
int index = list.size()-1;
return list.get(index);
}
public int getMin() {
return lowest;
}
public int Min(){
int min = Integer.MAX_VALUE;
for(int value:list){
min = value<min?value:min;
}
return min;
}
}
我的思路很简单,因为要在常数复杂度获取最小值,那就维护一个栈中最小值
但是插入时候最小值很容易获得,只需要进行比较,但是pop的时候就得重新获取最小值,这个时间复杂度就得遍历整个栈(我这里用动态数组当栈了)才能获得,时间复杂度O(n)
空间复杂度:O(n)
共以下 45 个回答
时间复杂度O(1),空间复杂度O(n)
// 最小栈元素,最开始冒出的解决办法是遍历一遍这个栈的元素,但是复杂度是O(n),降低复杂度的话需要借助辅助栈来存储最小元素
// 值得注意的点,也是我犯错误的栈b辅助栈中的元素存储的是“非严格降序的”元素,>=,而不是>
// 还有一个自己的思考误区,总认为栈中已经有元素了,要把栈中的最小值求出来,这样就完全想不到借用辅助栈的方法去解决问题
class MinStack { a,b; // 维护两个栈
public:
/** initialize your data structure here. */
//成员变量
stack
MinStack() {
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
时间复杂度:o(1)
空间复杂度:o(n)
def init(self):
“””
initialize your data structure here.
“””
self.q1 = []
self.q2 = []
为了能够保证获取最小元素的时间复杂度为o(1)
需要在申请一个列表进行辅助,存储非严格递减数据
在push的时候 需要确认辅助列表为空或者目前push的元素小于等于辅助栈的栈顶元素,将该元素也压入辅助栈
在pop的时候需要需要确认原始列表pop的内容是否等于辅助栈的栈顶元素,如果等于,则辅助栈的元素也要pop
时间复杂度:o(1)
空间复杂度:o(n)
核心思想是另外拿一个栈以存方最小值 时间复杂度和空间复杂度均为o(1) s1; s2; s1; s2;
class MinStack {
public:
/** initialize your data structure here. */
stack
stack
MinStack() {
std::stack
std::stack
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
class MinStack:
# 维护两个栈,原栈:正常push和pop;最小栈:存放小的但是后入栈的那些数
def init(self):
self.stack1 = []
self.stack2 = []
day5第一题
时间复杂度:O(1)
空间复杂度:O(1)
包含min函数的栈java
思路:用一个栈存储当前栈的最小值
代码:
class MinStack { st1,st2;
public:
stack
/** initialize your data structure here. */
MinStack() {
};
//用另一个栈记录最小值,时间O(1),O(n)
java A,B;
class MinStack{
Stack
public MinStack(){
A = new Stack<>();
B = new Stack<>();
}
public void push(int x){
A.add(x);
if(B.empty() || B.peek() >= x)
B.add(x);
}
public void pop(){
if(A.pop().equals(B.peek()))
B.pop();
}
public int top(){
return A.peek();
}
public int getMin(){
return B.peek();
}
}
提交
class MinStack { stk1; stk2;
public:
stack
stack
/** initialize your data structure here. */
MinStack() {
}
};
时间O(1),空间O(n)
class MinStack { stack; minstack;
Stack
Stack
}
一个存当前最小,一个正常存
①双栈,一个存当前最小,一个正常存时间复杂度O(1)空间复杂度O(n)
②单栈遍历,时间复杂度O(n),空间复杂度O(1)
class MinStack {A,B; (); ();
Stack
/** initialize your data structure here. */
public MinStack() {
A = new Stack
B = new Stack
}
}A; ();
方法2:
class MinStack {
Stack
private int min = Integer.MAX_VALUE;
/** initialize your data structure here. */
public MinStack() {
A = new Stack
}
}
时间复杂度:O(1)
空间复杂度:O(n)
class MinStack {A, B;
public:
stack
/** initialize your data structure here. */
MinStack() {
};
打卡
定义两个栈,一个栈作为主数据存储,一个存储从小到到大的栈
class MinStack {
}
时间复杂度O(1)
空间复杂度O(n)
时间复杂度O(1)
空间复杂度O(n)
class MinStack {
}
//打卡
定义两个栈,一个栈用作正常栈,另一个用来存储从小到大排序的栈,时间复杂度O(1),空间复杂度O(n)
栈B的顶部是栈A的最小元素。
stack A,B;
MinStack() {}
时间复杂度:O(1)
空间复杂度:O(n)
class MinStack { A, B;
Stack
/** initialize your data structure here. */
public MinStack() {
A = new Stack<>();
B = new Stack<>();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
思路:辅助栈存最小值
打卡
https://leetcode.cn/problems/bao-han-minhan-shu-de-zhan-lcof/solutions/2670164/zui-xiao-zhan-by-yukislab-bwp0
感觉这题比较简单,我感觉比那个链表的题好理解很多,可能原因在于这里没有那么多判断的条件吧,整体代码也比较好理解。 stack1 = new Stack(); stack2 = new Stack();
class MinStack {
Stack
Stack
/** initialize your data structure here. */
public MinStack() {
this.stack1 =stack1;
this.stack2 = stack2;
}
stack1.push(x);
if(stack2.isEmpty() || x <= stack2.peek()){
stack2.push(x);
}
}
//if(!stack1.isEmpty()){
//Integer, 数值 > 127 I
if(stack1.peek() stack2.peek())
//if(stack1.peek().intValue() stack2.peek().intValue())
{
stack2.pop();
}
stack1.pop();
}
//}
return stack1.peek();
}
return stack2.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack { st1, st2;
public:
stack
/** initialize your data structure here. */
MinStack() {
};打卡,关键在于用一个栈 维护降序序列
class MinStack:
Your MinStack object will be instantiated and called as such:
obj = MinStack()
obj.push(x)
obj.pop()
param_3 = obj.top()
param_4 = obj.getMin()
时间O(1) 空间O(N)
class MinStack { s1,s2;
public:
/** initialize your data structure here. */
stack
MinStack() {
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
时间复杂度O(1),空间复杂度O(n)
class Solution {
ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode temp1=headA;
ListNode temp2=headB;
while((temp1!=null)||(temp2!=null))
{
if(temp1temp2)
return temp1;
if(temp1null)
temp1=headB;
else
temp1=temp1.next;
if(temp2null)
temp2=headA;
else
temp2=temp2.next;
}
return null;
}
}
时间复杂度:O(1)
额外空间复杂度:O(n)
维护一个从栈顶到栈底的单调递增栈
时间复杂度O(1)
空间复杂度O(n)
“`C++ minstack,stack1;
class MinStack {
stack
public:
/** initialize your data structure here. */
MinStack() {
};
“`
class MinStack {
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
时间复杂度O(1),空间复杂度O(n)
思路很难想到但是并不难实现,主要是开一个辅助的“最小值栈”,只把最小值推进去,只有值比栈顶小才推进去,比栈顶大就不推进去,从而实现了在$O(1)$时间复杂度上完成了提取最小值的过程,代价只是一个栈,额外的$O(n)$的空间复杂度。
需要注意的边界条件是出栈时候,如果要出的是最小值,必须把辅助栈里的最小值一起出了,否则做不到辅助栈栈顶始终是最小值
时间复杂度:O(1)
额外空间复杂度:O(n)
时间复杂度:O(1)
空间复杂度:O(n)