最全【一起学数据结构与算法】深度学习栈_深度学习技术栈(1),爱了爱了

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事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;

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值