Java刷题-stack
一、getMin栈
题目描述
实现一个特殊功能的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
输入描述:
第一行输入一个整数N,表示对栈进行的操作总数。
下面N行每行输入一个字符串S,表示操作的种类。
如果S为"push",则后面还有一个整数X表示向栈里压入整数X。
如果S为"pop",则表示弹出栈顶操作。
如果S为"getMin",则表示询问当前栈中的最小元素是多少。
输出描述:
对于每个getMin操作,输出一行表示当前栈中的最小元素是多少。
import java.util.Scanner;
import java.util.Stack;
public class Main {
private static Stack<Integer> stackData = new Stack<>();
private static Stack<Integer> stackMin = new Stack<>();
public static void push(int newNum) {
stackData.push(newNum);
if (stackMin.isEmpty()) {
stackMin.push(newNum);
} else if (newNum <= stackMin.peek()) {
stackMin.push(newNum);
}
}
public static int pop() {
int value = stackData.pop();
if (value == stackMin.peek()) {
stackMin.pop();
}
return value;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine(); //注意这个地方需要用nextLine()读取回车
for (int i = 0; i < n; i++) {
String str = sc.nextLine();
if (str.equals("getMin")) {
System.out.println(stackMin.peek());
} else if (str.equals("pop")) {
pop();
} else {
String[] a = str.split(" ");
int num = Integer.parseInt(a[a.length - 1]);
push(num);
}
}
}
}
nextInt(): it only reads the int value, nextInt() places the cursor in the same line after reading the input.
next(): read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.
nextLine(): reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.
nextInt():读取int类型的值,取值后,并不换行,光标不变
如果读入的字符串需要读入空格的话,注意需要用nextline读入nextInt后面输入的空格,否则默认就将回车读入了
public class ceshi {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int num=sc.nextInt();
String num1=sc.nextLine();
System.out.println(num);
System.out.println(num1);
}
}
3
3
nextLine()可以读取"\n"并换行输出,光标换行
next()只读空格之前的数据,光标换行
Java Stack 类
栈是Vector的一个子类,它实现了一个标准的后进先出的栈。
堆栈只定义了默认构造函数,用来创建一个空栈。 堆栈除了包括由Vector定义的所有方法,也定义了自己的一些方法。
Stack()
除了由Vector定义的所有方法,自己也定义了一些方法:
| 序号 | 方法描述 |
|---|---|
| 1 | boolean empty() 测试堆栈是否为空。 |
| 2 | Object peek( ) 查看堆栈顶部的对象,但不从堆栈中移除它。 |
| 3 | Object pop( ) 移除堆栈顶部的对象,并作为此函数的值返回该对象。 |
| 4 | Object push(Object element) 把项压入堆栈顶部。 |
| 5 | int search(Object element) 返回对象在堆栈中的位置,以 1 为基数。 |
字符串向整型的转换
字符串->整型
使用Integer类中的parseInt()方法
整型->字符串
- 任何类型+""可变成String类型(更简洁)
- 使用静态方法toString()
二、由两个栈组成的队列
编写一个类,用两个栈来实现队列,支持队列的基本操作
都是套路,学会就行,进步了哈哈
两个注意点
1、压入数据要全压
2、负责队列的栈不为空,就不能压入栈
import java.util.Scanner;
import java.util.Stack;
public class Main {
private static Stack<Integer> stack1 = new Stack<>();
private static Stack<Integer> stack2 = new Stack<>();
public static void add(int num) {
stack1.push(num);
}
public static int poll() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
int num = stack1.pop();
stack2.push(num);
}
}
return stack2.pop();
}
public static int peek() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
int num = stack1.pop();
stack2.push(num);
}
}
return stack2.peek();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
String str = sc.nextLine();
if (str.equals("peek")) {
System.out.println(peek());
} else if (str.equals("poll")) {
poll();
} else {
String[] a = str.split(" ");
int num = Integer.parseInt(a[a.length - 1]);
add(num);
}
}
}
}
三、递归逆序一个栈
这个题目对于递归理解有很好的帮助,我的理解就是盗梦空间,不停的进入下一个空间,直到到底层梦境中,死亡的时候就会逐层返回,这样的操作可以帮助我们转换一些数字顺序。
多看看吧,递归确实麻烦
import java.util.Scanner;
import java.util.Stack;
public class ss {
public static int reverse_Top(Stack<Integer> stack) {
int top = stack.pop();
if (stack.isEmpty()) {
return top;
} else {
int last = reverse_Top(stack);
stack.push(top);
return last;
}
}
public static void reverse_stack(Stack<Integer> stack) {
if (stack.isEmpty()) {
return;
}
int last = reverse_Top(stack);
reverse_stack(stack);
stack.push(last);
}
public static void main(String[] args) {
Stack<Integer> stack_new = new Stack<>();
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] arr = new int[num];
for (int i = 0; i < num; i++) {
arr[i] = sc.nextInt();
}
for (int i = num - 1; i >= 0; i--) {
stack_new.push(arr[i]);
}
reverse_stack(stack_new);
for (int i = 0; i < num; i++) {
System.out.print(stack_new.pop() + " ");
}
}
}
四、猫狗队列
五、用一个栈实现另一个栈的排序
这个题目注意循环的使用,唉不要乱用判断
import java.util.Scanner;
import java.util.Stack;
public class Stack_05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int num2 = 0;
Stack<Integer> stack = new Stack<>();
Stack<Integer> stack1 = new Stack<>();
for (int i = 0; i < num; i++) {
num2 = sc.nextInt();
stack.push(num2);
}
while (!stack.isEmpty()) {
int cur = stack.pop();
while (!stack1.isEmpty() && stack1.peek() < cur) {
stack.push(stack1.pop());
}
stack1.push(cur);
}
while (!stack1.isEmpty()) {
stack.push(stack1.pop());
}
for (int i = 0; i < num; i++) {
System.out.print(stack.pop()+" ");
}
}
}
六、生成窗口最大值数组
需要注意的是Linkedlist的用法一般用来形成队列,双端队列就可以实现这个题目的求解。
java.util.LinkedList 集合数据存储的结构是链表结构。方便元素添加、删除的集合。
LinkedList是一个双向链表,那么双向链表是什么样子的呢,我们用个图了解下。

实际开发中对一个集合元素的添加与删除经常涉及到首尾操作,而LinkedList提供了大量首尾操作的方法.
add poll peek三种



import java.util.LinkedList;
import java.util.Scanner;
public class sdfd {
public static int[] getMaxWindows(int[] arr, int w) {
//错误判断
if (arr == null || w < 1 || arr.length < w) {
return null;
}
LinkedList<Integer> qmax = new LinkedList<>();
int[] res = new int[arr.length - w + 1];
int index = 0;
for (int i = 0; i < arr.length; i++) {
while (!qmax.isEmpty() && arr[qmax.peekLast()] <= arr[i]) {
qmax.pollLast();
}
qmax.addLast(i);
if (qmax.peekFirst() == i - w) {
qmax.pollFirst();
}
if(i>w-1){
res[index++]=arr[qmax.peekFirst()];
}
}
return res;
}
public static void main(String[] args) {
}
}
Java刷题-stack的更多相关文章
- JS、JAVA刷题和C刷题的一个很重要的区别
就是最近在做树方面的题时,发现JS和JAVA刷题和C刷题的一个很重要的区别就是传入null的区别 当遍历的时候,C传参数时可以传进去null的指针,因为递归进去,出来时,指针还是指着那个地方 但是JS ...
- 牛客网Java刷题知识点之为什么HashMap和HashSet区别
不多说,直接上干货! HashMap 和 HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到Collection框架以及多线程的面试,可以说是不完整.而Collection框架的 ...
- 牛客网Java刷题知识点之为什么HashMap不支持线程的同步,不是线程安全的?如何实现HashMap的同步?
不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...
- 牛客网Java刷题知识点之Map的两种取值方式keySet和entrySet、HashMap 、Hashtable、TreeMap、LinkedHashMap、ConcurrentHashMap 、WeakHashMap
不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...
- 牛客网Java刷题知识点之ArrayList 、LinkedList 、Vector 的底层实现和区别
不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...
- 牛客网Java刷题知识点之垃圾回收算法过程、哪些内存需要回收、被标记需要清除对象的自我救赎、对象将根据存活的时间被分为:年轻代、年老代(Old Generation)、永久代、垃圾回收器的分类
不多说,直接上干货! 首先,大家要搞清楚,java里的内存是怎么分配的.详细见 牛客网Java刷题知识点之内存的划分(寄存器.本地方法区.方法区.栈内存和堆内存) 哪些内存需要回收 其实,一般是对堆内 ...
- 牛客网Java刷题知识点之HashMap的实现原理、HashMap的存储结构、HashMap在JDK1.6、JDK1.7、JDK1.8之间的差异以及带来的性能影响
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- 牛客网Java刷题知识点之UDP协议是否支持HTTP和HTTPS协议?为什么?TCP协议支持吗?
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- 牛客网Java刷题知识点之TCP、UDP、TCP和UDP的区别、socket、TCP编程的客户端一般步骤、TCP编程的服务器端一般步骤、UDP编程的客户端一般步骤、UDP编程的服务器端一般步骤
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...
随机推荐
- PAUL ADAMS ARCHITECT:薪资追不上房价美一半家庭难买房
尽管上一年度美国经济遭受重创,但美国房价依旧持续蹿扬,据最新调查显示,美国大部分地区的房价已经到了一般家庭无法负担的水准. 美国房价上涨持续强劲,主要受益美国人居家办公需求(受大环境影响,目前美国有7 ...
- PAUL ADAMS ARCHITECT:澳大利亚楼市保持涨势
澳大利亚最新房价变化显示,住宅价格指数连续第10周上涨,包括五个主要首府城市的上涨了0.29%. 12月截至24日,布里斯班以1.03%涨幅领跑,五个首府城市平均涨幅0.78%. 在过去3个月里,悉尼 ...
- BGV暴涨千倍,未来或将超越YFI领跑DeFi全场!
毫无疑问,YFI在2020年上半年以一己之力掀翻了DeFi市场的热潮.迄今为止,YFI的新鲜资讯从不缺席,最近也是频频登上各大知名媒体热搜.其币价远远超过比特币价格,也让资本市场注意到DeFi市场原来 ...
- django学习-9.windows系统安装mysql8教程
1.前言 mysql是最流行的关系型数据库管理系统之一,我们可以在本地windows环境下搭建一个mysql的环境,便于学习. 当前我采取的搭配是: windows7(window8和window10 ...
- Mybatis-06 动态Sql
Mybatis-06 动态Sql 多对一处理 多个学生,对应一个老师 对于学生这边而言,关联多个学生,关联一个老师 [多对一] 对于老师而言,集合,一个老师又很多学生 [一对多] 1.创建数据库 2. ...
- JUnit5学习之三:Assertions类
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- JVM基础入门
内存模型 我理解的jvm: 个人理解的jvm流程: .java反编译为.class文件 经类加载器到上图的内存模型 方法区:存静态 常量区(String在里面) 本地栈区:调本地服务其它库的方法 计数 ...
- 【Notes_8】现代图形学入门——几何(基本表示方法、曲线与曲面)
几何 几何表示 隐式表示 不给出点的坐标,给数学表达式 优点 可以很容易找到点与几何之间的关系 缺点 找某特定的点很难 更多的隐式表示方法 Constructive Solid Geometry .D ...
- 使用PowerDesigner进行数据库设计并直接把设计好的表导出相应的建表语句
Power Designer:数据库表设计工具 PowerDesigner是Sybase公司的一款软件,使用它可以方便地对系统进行分析设计,他几乎包括了数据库模型设计的全过程.利用PowerDesig ...
- 【HTB系列】靶机Teacher的渗透测试详解
出品|MS08067实验室(www.ms08067.com) 本文作者:大方子(Ms08067实验室核心成员) Kali: 10.10.14.50 靶机地址:10.10.10.153 先用nmap 对 ...