LeetCode 简要日记 455 & 104

170 阅读1分钟

今天做了两个简单的小题目,一丢丢难度都没有,就不细说了,记个流水

简单的比大小:

Assign Cookies

题目描述很长,实际上就是有两个数组,只要s中存在比g大的就算喂饱一个,排个序,挨个比较一下就得到答案了。

public class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.parallelSort(g);
        Arrays.parallelSort(s);
        int ig = 0,is=0;
        int count = 0;
        while(ig<g.length&&is<s.length) {
            if (g[ig] <= s[is]) {
                ig++;
                is++;
                count++;
            } else {
                is++;
            }
        }
        return count;
    }
}

简单的求二叉树最大深度:

Maximum Depth of Binary Tree

递归一下顺利解决问题

public class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
            return 0;
        int l = maxDepth(root.left);
        int r = maxDepth(root.right);
        return (l>r?l:r)+1;
    }
}