题目地址
题目描述
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:
1 | Input: 22 |
Example 2:
1 | Input: 5 |
Example 3:
1 | Input: 6 |
Example 4:
1 | Input: 8 |
Note:
1 <= N <= 10^9
解题思路
题目要求找出给定值的二进制串中距离最远的两个 1
,并返回其距离。可以化为二进制串然后遍历也可以直接像我这种使用移位并且计算。
解题代码
1 | class Solution { |