0
点赞
收藏
分享

微信扫一扫

RAFT:引领 Llama 在 RAG 中发展

烟中雯城 2024-05-09 阅读 6

1.将递归转化为循环

比如:逆序打印链表

// 递归方式
        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+" ");
        }
        }
public void show3(ListNode head) {
        if(head == null) {
            return;
        }
        if(head.next == null) {
            System.out.println(head.val);
            return;
        }
        show3(head.next);
        System.out.println(head.val);
    }
 
    public void show4() {
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        //依次出栈
        while (!stack.empty()) {
            ListNode tmp = stack.pop();
            System.out.println(tmp.val);
        }
    }

2.括号匹配

https://leetcode.cn/problems/valid-parentheses/description/

 3.逆波兰表达式

150. 逆波兰表达式求值 - 力扣(LeetCode)

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        for(String s:tokens){
            if(!isOpera(s)){
                //数字:放入栈当中
                stack.push(Integer.parseInt(s));//将字符串转成整数
            }
            else{
                //弹出栈顶的两个元素
                int num2=stack.pop();
                int num1=stack.pop();
                switch(s){
                    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();
    }
 
 
    public boolean isOpera(String s){
        if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
            return true;
        }
        return false;
    }
}

4.栈的压入、弹出序列

1)何时入栈---每次都入

2)什么时候出栈

栈不为空的时候并且栈顶元素和k下标的元素相同时可以出栈

栈的压入、弹出序列_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;
 
public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;//变量popA这个数组
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);
            while (!stack.empty() && j < popA.length
                    && stack.peek() == popA[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
}

 5.最小栈

import java.util.Stack;
 
class MinStack {
    private Stack<Integer> stack ;
    private Stack<Integer> minStack ;
    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }
    
    public void push(int val) {
        stack.push(val);
        //第一次在最小栈当中存储元素
        if(minStack.empty()) {
            minStack.push(val);
        }else {
            if(val <= minStack.peek()) {
                minStack.push(val);
            }
        }
    }
    
    public void pop() {
        //栈为空 则不能进行弹出元素
        if(stack.empty()) {
            return;
        }
        int val = stack.pop();
        if(val == minStack.peek()) {
            minStack.pop();
        }
    }
    //获取栈顶元素 和 最小栈没有关系
    public int top() {
        if(stack.empty()) {
            return -1;
        }
        return stack.peek();
    }
 
    //获取元素 不是删除元素
    public int getMin() {
        return minStack.peek();
    }
    
}
举报

相关推荐

0 条评论