7月算法训练------第十四天(栈)解题报告

7月算法训练------第十四天(栈)解题报告

题目类型:栈
题目难度:简单

第一题、1614. 括号的最大嵌套深度

  1. 题目链接:1614. 括号的最大嵌套深度
  2. 思路分析:
    遍历整个字符串,如果是'(',则将其压入栈中;
    如果是')',则将栈的长度记录一下哎,然后压出一个')'
    当记录栈长度的ArrayList在循环完之后,还为空,则说明输入的字符串中不含'('‘)'
    ArrayList中的最大值返回。
  3. 代码:
class Solution {
    public int maxDepth(String s) {
        // if(s.length() <= 1) return 0;
        char[] ch = s.toCharArray();
        ArrayList<Integer> list = new ArrayList();
        LinkedList<Character> clist = new LinkedList();
        // int sum;
        for(char c : ch){
            if(c == '('){
                clist.push('(');
            }
            if(c == ')'){
                list.add(clist.size());
                clist.pop();
            }
        }
        if(list.isEmpty()) return 0;
        Integer[] a = new Integer[list.size()];
        list.toArray(a);
        Arrays.sort(a);
        return a[a.length - 1];
    }
}

第二题、剑指 Offer 06. 从尾到头打印链表

  1. 题目链接:剑指 Offer 06. 从尾到头打印链表
  2. 思路分析:
    遍历整个链表,将链表的元素用栈存储起来,然后申请一个长度和栈的长度一样的数组,将栈内元素压出,赋给数组。
  3. 代码:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        Stack<Integer> st = new Stack();
        while(head != null){
            st.push(head.val);
            head = head.next;
        }
        int[] a = new int[st.size()];
        for(int i = 0; i < a.length; i++){
            a[i] = st.pop();
        }
        return a;
    }
}

第三题、1381. 设计一个支持增量操作的栈

  1. 题目链接:1381. 设计一个支持增量操作的栈
  2. 思路分析:
    用一个LinkedList stack来表示这个栈;定义一个int型数maxSize表示最大容量
  • CustomStack(int maxSize):将stack创建对象,给maxSize赋值;
  • push(int x):如果stack的长度大于maxSize,则说明不能继续添加,所以直接返回;否则,就将x加入stack的末尾。
  • pop():如果stack长的为0,则说明stack中没数,返回-1;否则,将stack的末尾值删除并返回。
  • increment(int k, int val):如果stack的长度小于k,则说明stack中所有值都要加val,所以遍历整个stack,将stack中的数都加val;否则,就遍历stack中前k个元素,将他们加val
  1. 代码:
class CustomStack {
    LinkedList<Integer> stack;
    int maxSize;
    public CustomStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new LinkedList();
    }
    
    public void push(int x) {
        if(stack.size() >= maxSize){
            return;
        }else{
            stack.addLast(x);
        }
    }
    
    public int pop() {
        if(stack.size() == 0) return -1;
        return stack.removeLast();
    }
    
    public void increment(int k, int val) {
        if(stack.size() <= k){
            for(int i = 0; i < stack.size(); i++){
                int a = stack.get(i);
                stack.set(i, a+val);
            }
        }else{
            for(int i = 0; i < k; i++){
                int a = stack.get(i);
                stack.set(i, a+val);
            }
        }
    }
}

/**
 * Your CustomStack object will be instantiated and called as such:
 * CustomStack obj = new CustomStack(maxSize);
 * obj.push(x);
 * int param_2 = obj.pop();
 * obj.increment(k,val);
 */

第四题、1441. 用栈操作构建数组

  1. 题目链接:1441. 用栈操作构建数组
  2. 思路分析:
    用一个栈模拟加入过程,将num加入到栈中(1<=num<=n)。并将"Push"加入答案中,如果栈顶元素与数组元素不相等时,则说明需要将数字弹出栈,所以就将"Pop"加入答案;当相等时,说明需要遍历下一个数字,将index++;
  3. 代码:
class Solution {
    private final static String PUSH = "Push";
    private final static String POP = "Pop";

    public List<String> buildArray(int[] target, int n) {
        List<String> ans = new ArrayList<>();
        Stack<Integer> stack = new Stack<>();
        for (int num = 1, index = 0; num <= n && index < target.length; ++num) {
            stack.add(num);
            ans.add(PUSH);
            if (target[index] != stack.peek()) {
                stack.pop();
                ans.add(POP);
            } else index++; 
        }
        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值