letcode刷题

/ Java / 0 条评论 / 1729浏览

https://leetcode-cn.com/contest/weekly-contest-93/problems/binary-gap/

public int binaryGap(int N) {
    String result= "";
    while(N>=2){
        result = N%2 + result;
        N = N/2;
    }
    result = N + result;
    System.out.println(result);
    
    //
    if(!result.substring(1,result.length()).contains("1"))
       return 0;
    int len = 1;
    int maxlen = 1;
    int j =0;
    for(int i=1;i maxlen){
                maxlen = len;
                len = 1;
            }
            j=i;
        }
        
    }
    return maxlen;
}


https: //leetcode-cn.com/contest/weekly-contest-93/problems/reordered-power-of-2/


class Solution {
    static boolean flag = false;
    public boolean reorderedPowerOf2(int N) {
        if (N == 1)
            return true;
        String[]strs = String.valueOf(N).split("");
        flag = false;
        getAllSortList(strs, 0, strs.length - 1);
        return flag;
    }
    public void getAllSortList(String[]str, int begin, int end) {
        String res = "";
        if (begin == end) {
            if (Integer.parseInt(str[end]) % 2 != 0)
                return;
            for (int i = 0; i <= end; i++) {
                res += str[i];
                if (res.indexOf("0") != 0 && res.length() == str.length) {
                    long xxx = Long.parseLong(res);
                    //System.out.println(xxx);
                    if (xxx % 2 == 0) {
                        while (xxx >> 1 >= 1) {
                            if (xxx % 2 != 0) {
                                break;
                            }
                            xxx = xxx >> 1;
                        }
                        if (xxx == 1) {
                            flag = true;
                            return;
                        }
                    }
                }
            }
        } else {
            for (int j = begin; j <= end; j++) {
                swap(str, begin, j);
                getAllSortList(str, begin + 1, end);
                swap(str, begin, j);
            }
        }
    }
    public void swap(String[]str, int a1, int a2) {
        String temp = str[a1];
        str[a1] = str[a2];
        str[a2] = temp;
    }
}