网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
s1.push(3);
s1.push(4);
System.out.println(s1.size());
![在这里插入图片描述](https://img-blog.csdnimg.cn/12531f48223a4cae9a317119a6c7bea6.png)
## 三、栈的模拟实现
### 3.1 push
public boolean isFull() {
return this.usedSize == this.elem.length;
}
public void push(int val) {
if (isFull()) {
//扩容
this.elem = Arrays.copyOf(this.elem,2\*this.usedSize);
}
this.elem[this.usedSize] = val;
this.usedSize++;
}
### 3.2 isEmpty
public boolean isEmpty() {
return this.usedSize == 0;
}
### 3.3 pop
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈为空");
}
int oldVal = this.elem[usedSize-1];
this.usedSize--;
return oldVal;
}
### 3.4 peek
public int peek() {
if (isEmpty()) {
throw new RuntimeException("栈为空");
}
return this.elem[usedSize-1];
}
### 3.5 MyStack.java
import java.lang.reflect.Array;
import java.util.Arrays;
@SuppressWarnings({“all”})
public class MyStack {
public int[] elem;
public int usedSize;
public MyStack() {
this.elem = new int[5];
}
public void push(int val) {
if (isFull()) {
//扩容
this.elem = Arrays.copyOf(this.elem,2\*this.usedSize);
}
this.elem[this.usedSize] = val;
this.usedSize++;
}
public boolean isFull() {
return this.usedSize == this.elem.length;
}
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈为空");
}
int oldVal = this.elem[usedSize-1];
this.usedSize--;
return oldVal;
}
public int peek() {
if (isEmpty()) {
throw new RuntimeException("栈为空");
}
return this.elem[usedSize-1];
}
public boolean isEmpty() {
return this.usedSize == 0;
}
}
## 四、经典题
### 4.1 有效的括号
![在这里插入图片描述](https://img-blog.csdnimg.cn/a6990816bcfc492cb6e94252bb004ad8.png)
class Solution {
public boolean isValid(String s) {
Stack 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()) {
System.out.println("右括号多");
return false;
}
char top = stack.peek();//哪个左括号
if (top == '(' && ch ==')' || top == '[' && ch ==']' || top == '{' && ch =='}') {
stack.pop();
} else {
System.out.println("左右括号不匹配");
return false;
}
}
}
if (!stack.empty()) {
System.out.println("左括号多");
return false;
}
return true;
}
}
### 4.2 逆波兰表达式求值
![在这里插入图片描述](https://img-blog.csdnimg.cn/ef8bd88dec5748da9f06bf6246c91dfb.png)
class Solution {
public int evalRPN(String[] tokens) {
Stack stack = new Stack<>();
for (int i = 0; i < tokens.length; i++) {
String val = tokens[i];
if (!isOperation(val)) {
stack.push(Integer.parseInt(val));
} else {
int num2 = stack.pop();
int num1 = stack.pop();
switch (val) {
case “+”:
stack.push(num1+num2);
break;
case “-”:
stack.push(num1-num2);
break;
case “*”:
stack.push(num1*num2);
break;
case “/”:
stack.push(num1/num2);
break;
}
}
}
return stack.pop();
}
private boolean isOperation(String x) {
if (x.equals(“+”) || x.equals(“-”) || x.equals(“*”) || x.equals(“/”)) {
return true;
}
return false;
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!