【探索-中级算法】生成括号

在这里插入图片描述

得到全排列的组合,同时对于不符合要求的组合要剔除。

public List<String> generateParenthesis(int n) {
    List<String> result = new ArrayList<>();
    recall("", n, n,result);
    return result;
}
/**
 * @param leftCount 剩余可用的左括号个数
 * @param rightCount 剩余可用的右括号个数
 */
public void recall(String s, int leftCount, int rightCount,List<String> result) {
	// 没有括号可以进行组合了
    if (leftCount == 0 && rightCount == 0) {
        result.add(s);
    } else {
        if (leftCount>0) {
            String tmp = s + '(';
            int newLeftCount = leftCount - 1;
            if (newLeftCount==rightCount) {
                // 当左右括号已经使用了同样的个数,且目前为止组成的组合有效的话,
                //	才继续递归,(剪枝,但是也增加了判断的时间成本)
                // 因为有了这一步判断,则当 leftCount 和 rightCount 都为  0 时肯定
                //	是符合条件的
                if (isEffective(tmp)) recall(tmp, newLeftCount, rightCount,result);
            } else {
                recall(s + '(', newLeftCount, rightCount,result);
            }
        }
        if (rightCount>0) {
            String tmp = s + ')';
            int newRightCount = rightCount - 1;
            if (newRightCount==leftCount) {
                if (isEffective(tmp)) recall(tmp, leftCount, newRightCount,result);
            } else {
                recall(tmp, leftCount, newRightCount,result);
            }
        }
    }
}

// 判断组合是不是有效的
// 有效的定义就是对于 s[i] 如果是 '(',则在 s[j>i] 要存在 ')' 与之匹配
public boolean isEffective(String s) {
    if (s!=null&&s.length()>0) {
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if (c=='(') stack.push('(');
            else {
                if (stack.isEmpty()) return false;
                else stack.pop();
            }
        }
        return true;
    }
    return false;
}

// 对于判断组合是否有效的方法,还可以优化一下
// 即直接计算左右括号的数量,如果是有效的,那么在任意时刻左括号的数量要 >= 右括号的
public static boolean isEffective(String s) {
    if (s!=null&&s.length()>0) { 
        int num = 0;
        for (char c : s.toCharArray()) {
            if (c=='(') num++;
            else num--;
            if (num<0) return false;
        }
        if (num==0) return true;
    }
    return false;
}

根据网站提交记录进一步的优化:

public List<String> generateParenthesis(int n) {
    List<String> result = new ArrayList<>();
    recall(n, n,"",result);
    return result;
}

/**
 * @param leftCount 剩余可用的左括号个数
 * @param rightCount 剩余可用的右括号个数
 */
public void recall(int leftCount,int rightCount,String s,List<String> result) {
    if (leftCount == 0 && rightCount == 0) {
        result.add(s);
    }
    if (leftCount>0) recall(leftCount - 1, rightCount, s + '(', result);
    if (rightCount>leftCount) recall(leftCount, rightCount - 1, s + ')', result);
}

可以发现,肯定是要先用 ‘(’,然后才能用 ')' 与前面的 '(' 匹配。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值