[leetcode] (周赛)868. 二进制间距

868. 二进制间距

读懂题意就出来了

class Solution {
    public int binaryGap(int N) {
        String s = Integer.toBinaryString(N);

        int ans = 0;
        int k = -1;
        int i = 0;
        while (s.charAt(i) == '0') i++;
        for (; i < s.length(); i++) {
            if (s.charAt(i) == '1') {
                k++;
                ans = Math.max(ans, k);
                k = 0;
            } else {
                k++;
            }
        }

        return ans;
    }
}
原文地址:https://www.cnblogs.com/acbingo/p/9312198.html