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全栈大联盟 ...
随机推荐
- 别再人云亦云了!!!你真的搞懂了RDD、DF、DS的区别吗?
几年前,包括最近,我看了各种书籍.教程.官网.但是真正能够把RDD.DataFrame.DataSet解释得清楚一点的.论据多一点少之又少,甚至有的人号称Spark专家,但在这一块根本说不清楚.还有国 ...
- m1款MacBook Air 使用3个月总结及原生运行于apple架构软件推荐
前言 我之前一直是一个坚定的Windows/Android党,大学的时候用过几台iPhone,感觉也就那样.这次m1版本的Mac一发布我直接又转回apple阵营了,11月份的时候官网订了一台m1 版本 ...
- Java进阶专题(二十六) 数据库原理研究与优化
前言 在一个大数据量的系统中,这些数据的存储.处理.搜索是一个非常棘手的问题. 比如存储问题:单台服务器的存储能力及数据处理能力都是有限的, 因此需要增加服务器, 搭建集群来存储海量数据. 读写性能问 ...
- 痞子衡嵌入式:串行NOR Flash的DQS信号功能简介
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是串行NOR Flash的DQS信号功能. 串行NOR Flash在嵌入式里的应用相当广泛,既可用作数据存储也可以用作代码(XiP)存储, ...
- oracle check datapump jobs
reference: https://asktom.oracle.com/pls/apex/asktom.search?tag=getting-ora-31626-job-does-not-exist ...
- Go的数组
目录 数组 一.数组的定义 1.声明数组 2.初始化设值 3.指定位置设值 4.不指定长度初始化(了解) 二.数组的使用 三.数组的类型 四.数组的长度 五.迭代数组 1.初始化迭代 2.使用rang ...
- 如何用Eggjs从零开始开发一个项目(1)
前言 "纸上得来终觉浅,绝知此事要躬行."虽然node一直在断断续续地学,但总是东一榔头西一榔头的,没有一点系统,所以打算写一个项目来串联一下之前的学习成果. 为什么选择Eggjs ...
- MySQL注入点与SQL语句的关系
目录 注入位置分类 内联式 - UNION query SQL injection 终止式 - End SQL injection 堆叠式 - Stacked queries SQL injectio ...
- 手工实现一个ThreadPoolExecutor
以下代码的实现逻辑出自于公众号 码农翻身 <你管这破玩意叫线程池?> - PS:刘欣老师在我心中是软件技术行业的大刘. 线程池接口 public interface Executor { ...
- DLL劫持学习及复现
0x01 dll简介 在Windows系统中,为了节省内存和实现代码重用,微软在Windows操作系统中实现了一种共享函数库的方式.这就是DLL(Dynamic Link Library),即动态链接 ...