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(a…
Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N. If there aren't two consecutive 1's, return 0. Example 1: Input: 22 Output: 2 Explanation: 22 in binary is 0b10110. In the…
题目要求 Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N. If there aren't two consecutive 1's, return 0. 题目分析及思路 给定一个正整数,返回该数二进制形式中连续两个1的最长间隔.若是没有连续的两个1,则返回0.可将该数转成二进制形式,得到的结果…