• NC_49_longestValidParentheses NC_50_REVERSE_K_GROUP NC_51_Merge_KLists


    package org.example.interview.practice;
    
    import java.util.Deque;
    import java.util.LinkedList;
    
    /**
     * @author xianzhe.ma
     * @date 2021/8/16
     */
    
    public class NC_49_longestValidParentheses {
    
        public int longestValidParentheses (String s) {
            // write code here
            int maxans = 0;
            Deque<Integer> stack = new LinkedList<Integer>();
            stack.push(-1);
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '(') {
                    stack.push(i);//放的是左括号的下标
                } else {
                    stack.pop();//遇到右括号就pop
                    if (stack.isEmpty()) {
                        stack.push(i);//始终保证栈底是上次未匹配的右括号的下标
                    } else {
                        maxans = Math.max(maxans, i - stack.peek());
                    }
                }
            }
            return maxans;
        }
    }
    package org.example.interview.practice;
    
    import java.util.Stack;
    
    /**
     * @author xianzhe.ma
     * @date 2021/7/16
     */
    
    public class NC_50_REVERSE_K_GROUP {
    
        public ListNode reverseKGroup(ListNode head, int k) {
            // write code here
            Stack<ListNode> stack = new Stack<ListNode>();
            //初始化一个新的链表存放结果
            ListNode ret = new ListNode(0);
            //为新链表定义一个指针,防止后续操作改变链表头节点
            ListNode p = ret;
            //循环原有链表
            while (true) {
                //为每次反转计数
                int count = 0;
                //定义指针操作原始链表
                ListNode tmp = head;
                //循环入栈
                while (tmp != null && count < k) {
                    stack.push(tmp);
                    tmp = tmp.next;
                    count++;
                }
                //判断该次反转是否达到要求,此处防止因tem==null跳出循环的条件
                if (count != k) {
                    //表示剩下的节点不够k个,直接将剩余节点插入末尾结束
                    p.next = head;
                    break;
                }
                //出栈操作,反转链表
                while (!stack.isEmpty()) {
                    p.next = stack.pop();
                    p = p.next;
                }
                //重置下一次操作的初始节点
                p.next = tmp;
                head = tmp;
            }
            return ret.next;
        }
    
    
        public static class ListNode {
            int val;
            ListNode next = null;
    
            public ListNode(int val) {
                this.val = val;
            }
        }
    
        public static void main (String[] args) {
            ListNode one = new ListNode(11);
            ListNode two = new ListNode(22);
    
            ListNode three = new ListNode(33);
            ListNode four = new ListNode(44);
    
            ListNode five = new ListNode(55);
            ListNode six = new ListNode(66);
    
            one.next = two;
            two.next = three;
            three.next = four;
            four.next = five;
            five.next = six;
    
            printList(reverseKGroup2(one,2));
        }
    
        public static void  printList(ListNode head) {
            while (head != null) {
                System.out.print(head.val + " ");
                head = head.next;
            }
        }
        public static ListNode reverseKGroup2(ListNode head, int k) {
            Stack<ListNode> stack = new Stack<>();
            ListNode dummy = new ListNode(-1);
            ListNode p = dummy;
    
            while (true) {
    
                int count = 0;
                ListNode temp = head;
                while (temp!= null && count < k) {
                    stack.push(temp);
                    temp = temp.next;
                    count++;
                }
    
                if (count<k) {
                    p.next = head;
                    break;
                }
    
                while (!stack.isEmpty()) {
                    p.next = stack.pop();
                    p = p.next;
                }
                head = temp;
                p.next = temp;
    
            }
    
            return dummy.next;
        }
    }
  • 相关阅读:
    树状数组
    #135. 二维树状数组 3:区间修改,区间查询
    poj 2155 (二维树状数组 区间修改 求某点值)
    #133. 二维树状数组 1:单点修改,区间查询
    poj 3468 (区间修改 区间查询)
    树状数组 模板
    1535:【例 1】数列操作
    最通俗全面理解application context中的context是什么意思
    牛客哈理工小乐乐下象棋(深度理解bfs好题)
    牛客哈理工小乐乐打游戏(bfs深度理解好题)
  • 原文地址:https://www.cnblogs.com/juniorMa/p/15879805.html
Copyright © 2020-2023  润新知