欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

[Leedcode][JAVA][第155题][最小栈][基本类型包装类]

发布时间:2023/12/10 编程问答 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 [Leedcode][JAVA][第155题][最小栈][基本类型包装类] 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

【问题描述】

【解答思路】

1. 两个栈实现

1.1、辅助栈和数据栈同步

特点:编码简单,不用考虑一些边界情况,就有一点不好:辅助栈可能会存一些“不必要”的元素。

1.2、辅助栈和数据栈不同步

特点:由“辅助栈和数据栈同步”的思想,我们知道,当数据栈进来的数越来越大的时候,我们要在辅助栈顶放置和当前辅助栈顶一样的元素,这样做有点“浪费”。基于这一点,我们做一些“优化”,但是在编码上就要注意一些边界条件。

(1)辅助栈为空的时候,必须放入新进来的数;

(2)新来的数小于或者等于辅助栈栈顶元素的时候,才放入,特别注意这里“等于”要考虑进去,因为出栈的时候,连续的、相等的并且是最小值的元素要同步出栈;

(3)出栈的时候,辅助栈的栈顶元素等于数据栈的栈顶元素,才出栈。

总结一下:出栈时,最小值出栈才同步;入栈时,最小值入栈才同步。

1.1、辅助栈和数据栈同步

import java.util.Stack;public class MinStack {// 数据栈private Stack<Integer> data;// 辅助栈private Stack<Integer> helper;/*** initialize your data structure here.*/public MinStack() {data = new Stack<>();helper = new Stack<>();}// 思路 1:数据栈和辅助栈在任何时候都同步public void push(int x) {// 数据栈和辅助栈一定会增加元素data.add(x);if (helper.isEmpty() || helper.peek() >= x) {helper.add(x);} else {helper.add(helper.peek());}}public void pop() {// 两个栈都得 popif (!data.isEmpty()) {helper.pop();data.pop();}}public int top() {if(!data.isEmpty()){return data.peek();}throw new RuntimeException("栈中元素为空,此操作非法");}public int getMin() {if(!helper.isEmpty()){return helper.peek();}throw new RuntimeException("栈中元素为空,此操作非法");} }

1.2、辅助栈和数据栈不同步

import java.util.Stack;public class MinStack {// 数据栈private Stack<Integer> data;// 辅助栈private Stack<Integer> helper;/*** initialize your data structure here.*/public MinStack() {data = new Stack<>();helper = new Stack<>();}// 思路 2:辅助栈和数据栈不同步// 关键 1:辅助栈的元素空的时候,必须放入新进来的数// 关键 2:新来的数小于或者等于辅助栈栈顶元素的时候,才放入(特别注意这里等于要考虑进去)// 关键 3:出栈的时候,辅助栈的栈顶元素等于数据栈的栈顶元素,才出栈,即"出栈保持同步"就可以了public void push(int x) {// 辅助栈在必要的时候才增加data.add(x);// 关键 1 和 关键 2if (helper.isEmpty() || helper.peek() >= x) {helper.add(x);}}public void pop() {// 关键 3:data 一定得 pop()if (!data.isEmpty()) {// 注意:声明成 int 类型,这里完成了自动拆箱,从 Integer 转成了 int,因此下面的比较可以使用 "==" 运算符// 参考资料:https://www.cnblogs.com/GuoYaxiang/p/6931264.html// 如果把 top 变量声明成 Integer 类型,下面的比较就得使用 equals 方法int top = data.pop();if(top == helper.peek()){helper.pop();}}}public int top() {if(!data.isEmpty()){return data.peek();}throw new RuntimeException("栈中元素为空,此操作非法");}public int getMin() {if(!helper.isEmpty()){return helper.peek();}throw new RuntimeException("栈中元素为空,此操作非法");}}
2. 一个栈+辅助

当有更小的值来的时候,要把之前的最小值入栈,当前更小的值再入栈即可。当这个最小值要出栈的时候,下一个值便是之前的最小值了。

入栈 2 ,同时将之前的 min 值 3 入栈,再把 2 入栈,同时更新 min = 2 | 2 | min = 2 | 3 | | 5 | |_3_| stack 入栈 6 | 6 | min = 2 | 2 | | 3 | | 5 | |_3_| stack 出栈 6 | 2 | min = 2 | 3 | | 5 | |_3_| stack 出栈 2 | 2 | min = 2 | 3 | | 5 | |_3_| stack class MinStack {int min = Integer.MAX_VALUE;Stack<Integer> stack = new Stack<Integer>();public void push(int x) {//当前值更小if(x <= min){ //将之前的最小值保存stack.push(min);//更新最小值min=x;}stack.push(x);}public void pop() {//如果弹出的值是最小值,那么将下一个元素更新为最小值if(stack.pop() == min) {min=stack.pop();}}public int top() {return stack.peek();}public int getMin() {return min;} }
3. 链表同步法
  • 链表头插法实现基本功能
  • 最小值实现在 Node 节点中增加一个 min 字段,每次加入一个节点的时候,只要确定它的 min 值即可。
class MinStack {class Node{int value;int min;Node next;Node(int x, int min){this.value=x;this.min=min;next = null;}}Node head;//每次加入的节点放到头部public void push(int x) {if(null==head){head = new Node(x,x);}else{//当前值和之前头结点的最小值较小的做为当前的 minNode n = new Node(x, Math.min(x,head.min));n.next=head;head=n;}}public void pop() {if(head!=null)head =head.next;}public int top() {if(head!=null)return head.value;return -1;}public int getMin() {if(null!=head)return head.min;return -1;} }

【总结】

1.思路总结 最小栈 两个栈/一个栈+一个额外存储 / 链表实现
2. 包装类的用法

这里只讨论六种数字基本类型对应的包装类,因为它们是使用最频繁的,这些类中常用的方法可分为两类:一种是本类型与其它基本类型之间的转换,另一种是字符串与本类型和基本类型之间的转换。如下图,是Integer类中的一些常用方法:

其中的前五个,都是属于第一种,即与其它基本类型之间的转换。下面的三个则属于第二种,是字符串与本类型及基本类型之间的转换。

A、本类型与其它类型转换

示例如下:

Integer temp1 = new Integer(45);int temp2 = 45;byte t1 = temp1.byteValue(); //包装类的转换byte t2 = (byte)temp2; //基本数据类型的强制类型转换

B、本类型和对应基本类型转换

JDK1.5引入了自动装箱和拆箱的机制,那么什么是装箱和拆箱呢?

装箱就是将基本类型转换成包装类,分为自动装箱和手动装箱。同样地,拆箱就是将包装类型转换成基本类型,也分为自动拆箱和手动拆箱。

示例如下:

int a1=4;//手动装箱 Integer aI1 = new Integer(a1); //自动装箱 Integer aI2 = a1;//手动拆箱 int ai1 = aI1.intValue(); //自动拆箱 int ai2 = aI2;

C、字符串和基本类型转换

c1.基本类型转换成字符串类型

c1a. 包装类 的toString()方法

c1b. String 类的valueOf()方法

c1c. 空字符串加一个基本类型变量

//基本类型转换成字符串类型有三种方法int b = 1;String b1 = Integer.toString(b);String b2 = String.valueOf(b); String b3 = b+""; System.out.println("b1: "+b1+"b2: "+b2+"b3: "+b3);

c2.字符串转换成基本类型

c2a. 包装类的parse***()静态方法

c2b. 包装类的valueOf()方法

//字符串转换成基本类型的方法有两种
String b = “121”;
int c1 = Integer.parseInt(b);
int c2 = Integer.valueOf(b);

3. java基本类型 & 包装类 & 异同



3.Integer与int数据的比较

public class TestInteger {public static void main(String[] args) {int t1 = 46;int t2 = 46;Integer t3 = 46;Integer t4 = new Integer(46);Integer t5 = t1;Integer t6 = new Integer(t2);System.out.println("t1 == t2 " + (t1 == t2));System.out.println("t1 == t3 " + (t1 == t3));System.out.println("t1 == t4 " + (t1 == t4));System.out.println("t1 == t5 " + (t1 == t5));System.out.println("t1 == t6 " + (t1 == t6));System.out.println("t4 == t2 " + (t4 == t2));System.out.println("t5 == t2 " + (t5 == t2));System.out.println("t6 == t2 " + (t6 == t2));System.out.println("t4 == t3 " + (t4 == t3));System.out.println("t5 == t3 " + (t5 == t3));System.out.println("t6 == t3 " + (t6 == t3));System.out.println("t3 equals t4 " + (t3.equals(t4)));System.out.println("t3 equals t5 " + (t3.equals(t5)));System.out.println("t3 equals t6 " + (t3.equals(t6)));System.out.println("t3 equals t4 " + (t3.equals(t4)));System.out.println("t4 equals t5 " + (t4.equals(t5)));System.out.println("t4 equals t6 " + (t4.equals(t6)));System.out.println("t5 equals t6 " + (t5.equals(t6))); } }

转载链接:https://leetcode-cn.com/problems/min-stack/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-38/
转载链接:https://leetcode-cn.com/problems/min-stack/solution/shi-yong-fu-zhu-zhan-tong-bu-he-bu-tong-bu-python-/
参考链接:https://www.cnblogs.com/GuoYaxiang/p/6931264.html

总结

以上是生活随笔为你收集整理的[Leedcode][JAVA][第155题][最小栈][基本类型包装类]的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。