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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章