leetcode 第131场周赛

https://leetcode.com/contest/weekly-contest-131

对应的leetcode题目为1021-1024

 

1021:

类似于括号序列。每次栈空的时候加到结果里即可

class Solution {
    public String removeOuterParentheses(String S) {
        List s = new ArrayList<>();
        char[] a = new char[S.length()];
        int c = 0;
        int start = 0;
        String tmp = "";
        for (int i = 0; i < S.length(); i++) {
            char cc = S.charAt(i);
            tmp += cc;
            if (cc == '(') {
                a[c] = cc;
                c++;
            } else {
                //')'
                c--;
            }
            if (c == 0) {
                s.add(tmp);
                tmp = "";
            }
        }
        StringBuilder sb = new StringBuilder();
        for(String ss : s) {
            sb.append(ss.substring(1, ss.length() - 1));
        }
//        System.out.println(sb.toString());
        return sb.toString();
    }
}

1022:dfs

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    static long mod = 10_0000_0007;

    public int sumRootToLeaf(TreeNode root) {
        if (root == null) return 0;
        long dfs = dfs(root, 0);
//        System.out.println(dfs);
        return (int) dfs;
    }

    private long dfs(TreeNode root, int val) {
        if (root == null) {
            return val;
        } else {
            val = val * 2 + root.val;
            val %= mod;
            int v = val;
            long a, b;
            if (root.left == null) {
                a = 0;
            } else {
                a = dfs(root.left, v);
            }
            if (root.right == null) {
                b = 0;
            } else {
                b = dfs(root.right, v);
            }
            if (root.left == null && root.right == null) {
                a = val;
            }
            return (a + b) % mod;
        }

    }

}

1023:枚举就行

class Solution {
    public List camelMatch(String[] queries, String pattern) {
        List r = new ArrayList<>();
        for (String query : queries) {
            boolean s = check(query, pattern);
            r.add(s);
        }
//        System.out.println(r);
        return r;
    }

    public boolean check(String queries, String pattern) {
        int c = 0;
        for (int i = 0; i < queries.length(); i++) {
            char cc = queries.charAt(i);
            if (c < pattern.length() && cc == pattern.charAt(c)) {
                c++;
            } else if (cc >= 'a' && cc <= 'z'){
                //小写
                continue;
            } else {
                //大写
                if (c >= pattern.length()) return false;
                if (pattern.charAt(c) == cc) {
                    c++;
                } else {
                    return false;
                }
            }
        }
        return c == pattern.length();
    }
}

1024:大致题意:选最少的片段组成一个区间

按照左端点排序,再按照右端点排序,每次贪心地选择满足覆盖左端点的右端点最大的那个

class Solution {
    public int videoStitching(int[][] clips, int T) {
        P[] p = new P[clips.length];
        for (int i = 0; i < clips.length; i++) {
            p[i] = new P();
            p[i].l = clips[i][0];
            p[i].r = clips[i][1];
        }
        int ans = 0;
        Arrays.sort(p);
        int cur = 0;
        for (int i = 0; i < p.length; ) {
            if (p[i].l > cur) {
                ans = -1;
                break;
            }

            int max = 0;
            int maxIndex = i;
            for (int j = i; j < p.length && p[j].l <= cur; j++) {
                if (p[j].r >= max) {
                    max = p[j].r;
                    maxIndex = j;
                }
            }
            cur = max ;
            i = maxIndex + 1;
            ans++;
            if (cur  >= T) {
                break;
            }
        }
        if (cur < T) ans = -1;
        return ans;
    }
}

class P implements Comparable

{ @Override public int compareTo(P o) { if (l != o.l) { return Integer.compare(l, o.l); } return Integer.compare(r, o.r); } int l; int r; }

 

你可能感兴趣的:(leetcode,比赛)