• LeetCode Weekly Contest 255


    一、找出数组的最大公约数

    func findGCD(nums []int) int {
        max, min := nums[0],nums[0]
        for _,v :=range nums{
            if v > max{
                max = v
            }
            if v < min{
                min = v
            }
        }
        return gcd(max, min);
    }
    
    func gcd(a, b int) int{
        if b !=  0 {
            return gcd(b, a%b)
        }else{
            return a
        }
    }
    

    二、找出不同的二进制字符串

    看到题目的数据范围 n<=16, 当时dfs一下,在判断是否存在

    class Solution {
        List<String> res = new ArrayList<>();
        void dfs(int u,int n, String s){
            if(u == n){
                res.add(s);
                return;
            }
            dfs(u+1, n, s+"1");
            dfs(u+1, n, s+"0");
        }
        public String findDifferentBinaryString(String[] nums) {
            int n = nums.length;
            dfs(0, n, "");
            Set<String> set = new HashSet<>();
            for(String s : nums) set.add(s);
            for(String s : res){
                if(!set.contains(s)){
                    return s;
                }
            }
            return "";
        }
    }
    

    赛后也看到不错的解法
    使用二进制枚举的方式来判断

    class Solution {
        public String findDifferentBinaryString(String[] nums) {
            int n = nums.length;
            Set<String> set = new HashSet<>();
            for(String s : nums) set.add(s);
            for(int i=0; i<(1<<n); i++){
                String s ="";
                for(int j=0; j<n; j++){
                    if((i &1<<j) != 0){
                        s +="1"; 
                    }else{
                        s+="0";
                    }
                }
                if(!set.contains(s)){
                    return s;
                }
            }
            return "";
        }
    }
    

    还有一种方式,将字符串转成数字

    class Solution {
        public String findDifferentBinaryString(String[] nums) {
            int n = nums.length;
            Set<Integer> set = new HashSet<>();
            for(String s : nums){
                int num = Integer.parseInt(s, 2);
                 set.add(num);
            }
            int m = 0;
            while(true){
                if(!set.contains(m)){
                    String res = Integer.toBinaryString(m);
                    int t = res.length();
                    if(t < n){
                        for(int i=0; i<n-t; i++){
                            res = "0"+res;
                        }
                    }
                    return res;
                }
                m++;
            }
        }
    }
    

    三、最小化目标值与所选元素的差


    比赛时使用dfs,超时来,赛后看别人的代码,使用背包方式。

    class Solution {
        public int minimizeTheDifference(int[][] mat, int target) {
            int n = mat.length;
            int m = mat[0].length;
            int max = 70*70;
            boolean dp[][] = new boolean[n+1][max+1];
            dp[0][0] = true;
            for(int i=0; i<n; i++){
                for(int j=0; j<=max; j++){
                    if(!dp[i][j]){
                        continue;
                    }
                    for(int k=0; k<m; k++){
                        dp[i+1][j+mat[i][k]] = true;
                    }
                }
            }
            int res = Integer.MAX_VALUE;
            for(int i=0; i<=max; i++){
                if(dp[n][i]){
                 res = Math.min(res, Math.abs(target-i));
                }
            }
            return res;
        }
    }
    
  • 相关阅读:
    1. Two Sum
    100. Same Tree
    101. Symmetric Tree
    103. Binary Tree Zigzag Level Order Traversal
    102. Binary Tree Level Order Traversal
    STL的一些技巧函数使用
    104. Maximum Depth of Binary Tree
    apache开源项目--solr
    apache开源项目--HBase
    apache开源项目--ZooKeeper
  • 原文地址:https://www.cnblogs.com/lzeffort/p/15172805.html
Copyright © 2020-2023  润新知