算法编程 括号专题

1、有效的括号

class Solution {
    public boolean isValid(String s) {
        
        if(s.length() % 2 == 1) return false;

        Stack<Character> stack = new Stack<Character>();
        for(int i=0; i<s.length(); i++){

            if((s.charAt(i) == '(') || (s.charAt(i) == '[') || (s.charAt(i) == '{'))
                stack.push(s.charAt(i));
            if(!stack.isEmpty()){
                if(((stack.peek() == '(') && (s.charAt(i) == ')')) || ((stack.peek() == '[') && (s.charAt(i) == ']')) || ((stack.peek() == '{') && (s.charAt(i) == '}')))
                    stack.pop();
            }
        }
        return stack.size() == 0;
    }
}

2、括号生成

class Solution {

    List<String> result = new ArrayList<String>();

    public List<String> generateParenthesis(int n) {

        backtrack(new String(), 0, 0, n);
        return result;
    }

    public void backtrack(String cur, int open, int close, int max){

        if(cur.length() == max*2){
            result.add(cur);
            return ;
        }

        if(open < max){
            backtrack(cur+"(", open+1, close, max);
        }

        if(close < open){
            backtrack(cur+")", open, close+1, max);
        }
    }
}

3、最长有效括号

class Solution {
    public int longestValidParentheses(String s) {
        
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(-1);
        int result = 0;

        for(int i=0; i<s.length(); i++){

            if (s.charAt(i) == '(') {
                stack.push(i);
            } else {
                if(!stack.empty()) stack.pop();
                if (stack.empty()) {
                    stack.push(i);
                } else {
                    result = Math.max(result, i - stack.peek());
                }
            }
        }
        return result;
    }
}

4、根据二叉树创建字符串

/**
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public StringBuilder result = new StringBuilder();
    public String tree2str(TreeNode t) {
        
        dfs(t);
        return new String(result);
    }

    public void dfs(TreeNode current){

        if(current == null) return;

        result.append(current.val);

        if(current.left == null && current.right == null)
            return;
        else{
            result.append("(");
            dfs(current.left);
            result.append(")");

            if(current.right != null){
                result.append("(");
                dfs(current.right);
                result.append(")");
            }
        }
    }
}

5、删除最外层括号

class Solution {
    public String removeOuterParentheses(String S) {
        
        int count = 0, start = 0;
        StringBuilder result = new StringBuilder();

        for(int i=0; i<S.length(); i++){

            if(S.charAt(i) == '(')
                count++;
            if(S.charAt(i) == ')')
                count--;

            if(count == 1 && S.charAt(i) == '(')
                start = i;
            if(count == 0)
                result.append(S.substring(start+1, i));
        }
        return result.toString();
    }
}

6、使有效的括号添加最少

class Solution {
    public int minAddToMakeValid(String S) {
        int ans = 0, bal = 0;
        for (int i = 0; i < S.length(); ++i) {
            bal += S.charAt(i) == '(' ? 1 : -1;
            
            if (bal == -1) {
                ans++;
                bal++;
            }
        }

        return ans + bal;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值