leetcode 记录

Posted 保护眼睛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 记录相关的知识,希望对你有一定的参考价值。

965.单值二叉树

class Solution965 {
    public boolean isUnivalTree(TreeNode root) {
        if (root == null) return true;
        return judge(root, root.val);
    }

    public boolean judge(TreeNode root, int val) {
        if (root == null) return true;
        if (root.val != val) return false;
        return judge(root.left, val) && judge(root.right, val);
    }
}

961.重复N次的元素

class Solution961 {
    public static void main(String[] args) {
        System.out.println(repeatedNTimes(new int[]{1,2,3,3}));
    }
    public static int repeatedNTimes(int[] nums) {
        int resNum = 0;
        int[] res = new int[10000];
        for (int i = 0; i < nums.length; i++) {
            res[nums[i]]++;
            if (res[nums[i]] >= 2) {
                return nums[i];
            }
        }
        return -1;
    }
}

944.删列造序

class Solution944 {
    public static void main(String[] args) {
        System.out.println(minDeletionSize(new String[]{"cba", "daf", "ghi"}));
    }

    public static int minDeletionSize(String[] strings) {
        int res = 0;
        for (int i = 0; i < strings[0].length(); i++) {
            for (int j = 0; j < strings.length - 1; j++) {
                if (strings[j].charAt(i) > strings[j + 1].charAt(i)) {
                    res++;
                }
            }

        }
        return res;
    }
}

938.二叉搜索树的范围和

class Solution938 {
    int res;
    int low;
    int high;

    public int rangeSumBST(TreeNode root, int low, int high) {
        this.low = low;
        this.high = high;
        inorderTree(root);
        return res;
    }

    public void inorderTree(TreeNode root) {
        if (root == null) return;
        inorderTree(root.left);
        if (root.val >= low && root.val <= high) {
            res += root.val;
        }
        inorderTree(root.right);
    }

    public int rangeSumBST1(TreeNode root, int low, int high) {
        if (root == null) return 0;
        if (root.val < low) {
            return rangeSumBST(root.right, low, high);
        }
        if (root.val > high) {
            return rangeSumBST(root.left, low, high);
        }
        return root.val + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high);
    }

}

922.按奇偶排序数据II

/**
 * user:ypc;
 * date:2021-06-03;
 * time: 15:02;
 */
class Solution922 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(sortArrayByParityII(new int[]{4, 2, 5, 7})));
    }

    public static int[] sortArrayByParityII(int[] nums) {
        int i = 0;
        int j = 1;
        while (j < nums.length && i < nums.length) {
            while (i < nums.length && nums[i] % 2 == 0) {
                i += 2;
            }
            while (j < nums.length && nums[j] % 2 == 1) {
                j += 2;
            }
            if (i < nums.length && j < nums.length) {
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j] = tmp;
            }
        }
        return nums;
    }

}

914.卡牌分组

class Solution914 {
    public static void main(String[] args) {
        System.out.println(hasGroupsSizeX(new int[]{1, 1, 1, 2, 2, 2, 3, 3, 3}));
    }

    public static boolean hasGroupsSizeX(int[] deck) {
        int[] count = new int[10000];
        for (int c : deck) {
            count[c]++;
        }

        int g = 0;
        for (int i = 0; i < 10000; ++i) {
            if (count[i] == 0) continue;
            g = gcd(count[i], g);
            if (g == 1) return false;
        }
        return true;
    }

    public static int gcd(int x, int y) {
        return x == 0 ? y : gcd(y % x, x);
    }
}

905.按奇偶排序数组

class Solution905 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(sortArrayByParity(new int[]{1,3,2,4})));
    }
    public static int[] sortArrayByParity(int[] nums) {
        int i = 0;
        int j = nums.length-1;
        while (i<j) {
            while (nums[i] % 2 == 0 && i < j) {
                i++;
            }
            while (nums[j] % 2 != 0 && i < j) {
                j--;
            }
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
        return nums;
    }
}

872.叶子相似的树

class Solution872 {
    public ArrayList<Integer> list1 = new ArrayList<>();
    public ArrayList<Integer> list2 = new ArrayList<>();

    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        inorderTree(root1, list1);
        inorderTree(root2, list2);
        return list1.equals(list2);
    }

    public void inorderTree(TreeNode root, ArrayList<Integer> list) {
        if (root == null) {
            return;
        }
        inorderTree(root.left, list);
        if (root.right == null && root.left == null) {
            list.add(root.val);
        }
        inorderTree(root.right, list);
    }
}

868.求二进制数的间距

class Solution868 {
    public static void main(String[] args) {
        System.out.println(binaryGap(22));
    }

    public static int binaryGap(int n) {
        int res = 0;
        int index = 0;
        while (n != 0) {
            if ((n & 1) == 1) {
                res = (index > res ? index : res);
                index = 1;
            } else if (index > 0) {
                index++;
            }
            n = n >>> 1;
        }
        return res;
    }
}

867.求一个矩阵的转置矩阵

class Solution867 {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
        int[][] res = transpose(matrix);
        for (int i = 0; i < res.length; i++) {
            System.out.println(Arrays.toString(res[i]));
        }
    }

    public static int[][] transpose(int[][] matrix) {
        int[][] res = new int[matrix[0].length][matrix.length];
        for (int i = 0; i < matrix[0].length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                res[i][j] = matrix[j][i];
            }
        }
        return res;
    }
}

896.判断一个数列是否是单调数列

/**
 * user:ypc;
 * date:2021-06-02;
 * time: 15:05;
 */
class Solution896 {
    public boolean isMonotonic(int[] nums) {
        boolean increase = false;
        boolean decrease = false;
        int len = nums.length;
        for (int i = 1; i < len; i++) {
            if (nums[i] > nums[i - 1]) {
                increase = true;
            }
            if (nums[i] < nums[i - 1]) {
                decrease = true;
            }
        }
        return increase && decrease ? false : true;
    }
}

以上是关于leetcode 记录的主要内容,如果未能解决你的问题,请参考以下文章

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

discuz X3.1 源代码阅读,记录代码片段

提效小技巧——记录那些不常用的代码片段

用于从 cloudkit 检索单列的代码模式/片段

错误记录Flutter 混合开发获取 BinaryMessenger 报错 ( FlutterActivityAndFragmentDelegate.getFlutterEngine() )(代码片段

Leetcode.1024 视频拼接