public class MyStack {
int[] array;
int size;
public MyStack(){
array = new int[3];
}
public int push(int e){
ensureCapacity();
array[size++] = e;
return e;
}
public int pop(){
int e = peek();
size--;
return e;
}
public int peek(){
if(empty()){
throw new RuntimeException("栈为空,无法获取栈顶元素");
}
return array[size-1];
}
public int size(){
return size;
}
public boolean empty(){
return 0 == size;
}
private void ensureCapacity(){
if(size == array.length){
array = Arrays.copyOf(array, size*2);
}
}
}
1.5 栈的应用场景
1.5.1栈的定义
public class MyStack k{
// 定义一个栈
public int []elem;
public MyStack() {
this.usesize = 0;
this.elem =new int[SIZEMAX];
}
// 现有栈元素大小
public int usesize;
}
1.5.2入栈
// 入栈
public void push(int val) {
if (isFull()) {
this.elem= Arrays.copyOf(elem,2*elem.length);
}
elem[usesize]=val;
usesize++;
}
// 检查栈是否满
private boolean isFull() {
return usesize==size();
}
如果栈的空间存储已满,那么需要扩容
1.5.3出栈
// 去除栈顶元素
public int pop() {
if (isEmpty()) {
return -1;
}
int ret=elem[usesize-1];
// elem[usesize]=null;
usesize--;
return ret;
}
//检查是否空
public boolean isEmpty() {
return usesize==0;
}
首先要判断栈是否为空,使用 isEmpty() 方法,有的话出栈
1.5.4查找栈
//检测栈顶元素
public int peek() {
if (isEmpty()) {
return -1;
}
return elem[usesize-1];
//返回一个错误值
}
//检查是否空
public boolean isEmpty() {
return usesize==0;
}
1.5.5判断栈顶大小以及是否为空
//检查是否空
public boolean isEmpty() {
return usesize==0;
}
// 返回栈的大小
public int size() {
return usesize;
}
栈刷题:(与次序相关,多考虑栈)
1.选择
1. 若进栈序列为 1,2,3,4 ,进栈过程中可以出栈,则下列不可能的一个出栈序列是(C)
A: 1,4,3,2 B: 2,3,4,1
C: 3,1,4,2 D: 3,4,2,1
出栈顺序,可进可出
2.一个栈的初始状态为空。现将元素1、2、3、4、5、A、B、C、D、E依次入栈,然后再依次出栈,则元素出栈的顺
序是( B)。
A: 12345ABCDE B: EDCBA54321 C: ABCDE12345 D: 54321EDCBA
此题很简单,依次出栈顺序为E DCBA 54321
2. 将递归转化为循环
比如:逆序打印链表
// 递归方式
void printList(Node head){
if(null != head){
printList(head.next);
System.out.print(head.val + " ");
}
}
// 循环方式
void printList(Node head){
if(null == head){
return;
}
Stack<Node> s = new Stack<>();
// 将链表中的结点保存在栈中
Node cur = head;
while(null != cur){
s.push(cur);
cur = cur.next;
}
// 将栈中的元素出栈
while(!s.empty()){
System.out.print(s.pop().val + " ");
}
}
3. 括号匹配
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
// 判断左括号
if (ch == '(' || ch == '[' || ch == '{') {
stack.push(ch);
} else {
// 遇到了右括号
if (stack.empty()) {
return false;
} else {
char top = stack.peek();
// 检查是否匹配
if ((top == '(' && ch == ')') ||
(top == '{' && ch == '}') ||
(top == '[' && ch == ']')) {
stack.pop();
} else {
return false;
}
}
}
}
return stack.empty();
}
}
4. 逆波兰表达式求值
import java.util.Stack;
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack=new Stack<>();
for(int i=0;i<tokens.length;i++){
String tmp=tokens[i];
if(!isOperation(tokens[i])){
Integer val=Integer.valueOf(tokens[i]);
stack.push(val);
}else{
// 从栈中弹出两个操作数
int val2 = stack.pop();
int val1 = stack.pop();
// 根据运算符执行相应的操作
switch (tokens[i]) {
case "+":
stack.push(val1 + val2);
break;
case "-":
stack.push(val1 - val2);
break;
case "*":
stack.push(val1 * val2);
break;
case "/":
// 为了处理除法,需要注意整数除法的特性
stack.push(val1 / val2);
break;
}
}
}
return stack.pop();
}
public boolean isOperation(String a){
if(a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/")){
return true;
}else{
return false;
}
}
}
stack.push(Integer.valueOf(token)); 这行代码的意思是将字符串 token 转换为整数,然后将这个整数推入栈中。具体步骤如下:
5.
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pushV int整型一维数组
* @param popV int整型一维数组
* @return bool布尔型
*/
public boolean IsPopOrder (int[] pushV, int[] popV) {
// write code here
Stack<Integer>stack=new Stack<>();
int j=0;
for(int i=0;i<pushV.length;i++){
stack.push(pushV[i]);
while(j<popV.length&&!stack.empty()&&stack.peek()==popV[j]){
stack.pop();
j++;
}
}
return stack.empty();
}
}
6.
到这里,竹竹零就要和大家说再见了🍕🍕🍕
如果您觉得有失偏颇请您在评论区指正,如果您觉得不错的话留个好评再走吧!!
您的鼓励就是对我最大的支持! ! !