• [leetCode]1111. 有效括号的嵌套深度


    在这里插入图片描述

    均分最大深度

    • 遍历seq计算最大深度maxDepth
    • 遍历seq,遇到"(",当A子序列的最大深度小于ceil(maxDepth/2)时分配给A,反之分配给B
    • 遇到")", 当A的最大深度不为0时说明A子序列还有未被匹配的"(",所以分配给A,否则分配给B
    class Solution {
        public int[] maxDepthAfterSplit(String seq) {
            List<Integer> ans = new ArrayList<>();
            if (seq.length() == 0) return new int[]{};
            // 记录当前深度
            int depth = 0;
            // 记录最大深度
            int maxDepth = 0;
            for (int i = 0; i < seq.length(); i++) {
                if (seq.charAt(i) == '(') {
                    depth++;
                    if (depth > maxDepth)
                        maxDepth = depth;
                } else {
                    depth--;
                }
            }
            int aDepth = 0;
            // 最大深度二分之一的上界
            int mid = 1 + ((maxDepth - 1) / 2);
            for (int i = 0; i < seq.length(); i++) {
                if (seq.charAt(i) == '(') {
                    if (aDepth < mid) {
                        ans.add(0);
                        aDepth++;
                    }else {
                        ans.add(1);
                    }
                } else {
                    // A中还有未被配对的左括号
                    if (aDepth > 0) {
                        ans.add(0);
                        aDepth--;
                    } else {
                        ans.add(1);
                    }
                }
            }
            int[] res = new int[ans.size()];
            for (int i = 0; i < res.length; i++) {
                res[i] = ans.get(i);
            }
            return res;
        }
    }
    

    均分当前深度

    如果A子序列的深度小于当前深度的一半,那么也肯定小于最大深度的一半

    class Solution {
        public int[] maxDepthAfterSplit(String seq) {
            if (seq.length() == 0) return new int[]{};
            int[] ans = new int[seq.length()];
            // 记录当前深度
            int depth = 0;
            int aDepth = 0;
            for (int i = 0; i < seq.length(); i++) {
                if (seq.charAt(i) == '(') {
                    depth++;
                    // 计算当前深度的一半
                    int mid = 1 + ((depth - 1) / 2);
                    if (aDepth < mid) {
                        ans[i] = 0;
                        aDepth++;
                    } else {
                        ans[i] = 1;
                    }
                } else {
                    depth--;
                    if (aDepth > 0) {
                        ans[i] = 0;
                        aDepth--;
                    } else {
                        ans[i] = 1;
                    }
                }
            }
            return ans;
        }
    }
    

    奇偶性

    通过奇偶性将深度为奇数的括号与深度为偶数的括号平分

    class Solution {
        public int[] maxDepthAfterSplit(String seq) {
            if (seq.length() == 0) return new int[]{};
            int[] ans = new int[seq.length()];
            // 记录当前深度
            int depth = 0;
            for (int i = 0; i < seq.length(); i++) {
                if (seq.charAt(i) == '(') {
                    ans[i] = ++depth % 2;
                } else {
                    ans[i] = depth-- % 2;
                }
            }
            return ans;
        }
    }
    
  • 相关阅读:
    bzoj2428 [HAOI2006]均分数据 模拟退火
    Jersey入门三:创建一个JavaEE的Web项目
    Jersey入门二:运行项目
    Jersey入门一:从Maven Archetype创建jersey项目
    Bootstrap进阶七:LESS语法详解
    Bootstrap进阶六:动态样式语言LESS简介
    Bootstrap进阶五:Web开发中很实用的交互效果积累
    Bootstrap进阶四:jQuery插件详解
    Bootstrap进阶三:jQuery插件概览
    Bootstrap进阶二:基本组件
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13859909.html
Copyright © 2020-2023  润新知