第21天--算法(Leetcode 687,49,50,54)

687.最长同值路径

public int longestUnivaluePath(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return process(root).notBeginX - 1;
    }
    public Info process(TreeNode root) {
        if(root == null) {
            return new Info(0,0);
        }
        TreeNode l = root.left;
        TreeNode r = root.right;
        Info left = process(l);
        Info right = process(r);
        int beginX;
        int notBeginX;
        beginX = 1;
        if(l != null) {
            if(root.val == l.val) {
                beginX = 1 + left.beginX;
            }
        }
        if(r != null) {
            if(root.val == r.val) {
                beginX = Math.max(1 + right.beginX,beginX);
            }
        }
        notBeginX = Math.max(beginX,Math.max(left.notBeginX,right.notBeginX));
        if(l != null && r != null && root.val == l.val && root.val == r.val) {
            notBeginX = Math.max(notBeginX,left.beginX + right.beginX + 1);
        }
        return new Info(beginX,notBeginX);
    }
class Info {
    int beginX;
    int notBeginX;
    public Info(int beginX,int notBeginX) {
        this.beginX = beginX;
        this.notBeginX = notBeginX;
    }
}
49.字母异位词分组
public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList<>();
        HashMap<String,List<String>> hm = new HashMap<>();
        for(String str : strs) {
            char s[] = str.toCharArray();
            Arrays.sort(s);
            String key = String.valueOf(s);
            if(!hm.containsKey(key)) {
                hm.put(key,new ArrayList<String>());
            }
            hm.get(key).add(str);
        }
        for(List<String> l : hm.values()) {
            res.add(l);
        }
        return res;
    }
50.pow(x,n)
public double myPow(double x, int n) {
        if(x == 1 || x == 0) {
            return x;
        }
        if(n == 0) {
            return 1D;
        }
        if(x == -1) {
            if((n & 1) == 0) {
                return 1D;
            }else {
                return -1D;
            }
        }
        if(n == Integer.MIN_VALUE) {
            return 0;
        }
        double t = x;
        double ans = 1D;
        boolean isFu = n < 0;
        n = isFu ? -n : n;
        while(n != 0) {
            if((n & 1) != 0) {
                ans *= t;
            }
            t *= t;
            n >>= 1;
        }
        return isFu ? 1D / ans : ans;
    }
54.螺旋矩阵
public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        int tR = 0;
        int tC = 0;
        int dR = matrix.length - 1;
        int dC = matrix[0].length - 1;
        while(tR <= dR && tC <= dC) {
            process(res,matrix,tR ++,tC ++,dR --,dC --);
        }
        return res;
    }
    public void process(List<Integer> res,int[][] matrix,int tR,int tC,int dR,int dC) {
        if(tR == dR) {
            for(int i = tC;i <= dC;i ++) {
                res.add(matrix[tR][i]);
            }
        }else if(tC == dC) {
            for(int i = tR;i <= dR;i ++) {
                res.add(matrix[i][tC]);
            }
        }else {
            int curR = tR;
            int curC = tC;
            while(curC != dC) {
                res.add(matrix[tR][curC]);
                curC ++;
            }
            while(curR != dR) {
                res.add(matrix[curR][dC]);
                curR ++;
            }
            while(curC != tC) {
                res.add(matrix[dR][curC]);
                curC --;
            }
            while(curR != tR) {
                res.add(matrix[curR][tC]);
                curR --;
            }
        }
    }
 
posted @ 2022-01-11 16:41  现在开始努力  阅读(21)  评论(0)    收藏  举报