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全栈大联盟 ...
随机推荐
- PAA房产智慧社区:解决社区管理服务的痛点难点
社区,是社交与生活的舞台,更是家的延伸.社区之所有能够有所创新发展,得益于借助数字化和智能化.智能化给社区带来的便利体现在社区门禁可以人脸识别:AI的摄像头可以自动捕获异常的现象,便于社区管理员第一时 ...
- 通过setMouseTracking实现用鼠标拖动控件
1 import sys 2 from PyQt5.Qt import * 3 4 class Mwindow(QWidget): 5 leftclick = False 6 7 def __init ...
- 力扣1052. 爱生气的书店老板-C语言实现-中等难度
题目 传送门 文本 今天,书店老板有一家店打算试营业 customers.length 分钟.每分钟都有一些顾客(customers[i])会进入书店,所有这些顾客都会在那一分钟结束后离开. 在某些时 ...
- 能取值亦能赋值的Python切片
切片,就像面包,给几刀,切成一片一片,可以做成吐司,也可以做成三明治,口味更佳: 列表(list).元组(tuple).字符串(str)都能进行切片,得到子片段,实际上切片操作比想象的要强大很多,能取 ...
- C#从1970年开始到现在时间的总秒数
TimeSpan timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1)); string timeStamp = ((int)timeSpan. ...
- 🎊 Element UI 新春快报
新年好,Element UI 开发团队给各位支持我们的开发者们拜个晚年,祝大家在新的一年里工作没 bug, 天天不加班. 在过去一年里,Element UI 团队在稳定维护 Vue 2.x 版本的同时 ...
- Django自学计划之集装箱货物运输物流仓储一站式ERP系统
业余开始学习时间:2018年1月 业余学习时间段:每天下班晚饭后时间+无事的星期六和星期天+上班时的空闲时间 自学目标: 1.我们要用管理的思维来写我们的系统! 2.我们要用我们的ERP系统帮助中小集 ...
- JS常用数值验证
1.正整数验证 正整数是大于0的整数. function validateInteger(val) { return Number.isInteger(val) && val > ...
- Linux 查看磁盘是否为SSD
第一步,找到磁盘 ll /dev/sd* ll /dev/vd* 第二步,查对应磁盘类型 cat /sys/block/sda/queue/rotational 结果: 返回0:SSD盘 返回1: ...
- Tango with Django 1.9 中文——1.概述
译者注:英文书地址https://leanpub.com/tangowithdjango19/,作者为Leif Azzopardi 和 David Maxwell.本文仅为尝试.学习和交流,著作权利归 ...