[Swift]LeetCode868. 二进制间距 | Binary Gap

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 binary representation of 22, there are three ones, and two consecutive pairs of 1's.
The first consecutive pair of 1's have distance 2.
The second consecutive pair of 1's have distance 1.
The answer is the largest of these two distances, which is 2.

Example 2:

Input: 5
Output: 2
Explanation: 
5 in binary is 0b101.

Example 3:

Input: 6
Output: 1
Explanation: 
6 in binary is 0b110.

Example 4:

Input: 8
Output: 0
Explanation: 
8 in binary is 0b1000.
There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.

Note:

  • 1 <= N <= 10^9

给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离。 

如果没有两个连续的 1,返回 0 。 

示例 1:

输入:22
输出:2
解释:
22 的二进制是 0b10110 。
在 22 的二进制表示中,有三个 1,组成两对连续的 1 。
第一对连续的 1 中,两个 1 之间的距离为 2 。
第二对连续的 1 中,两个 1 之间的距离为 1 。
答案取两个距离之中最大的,也就是 2 。

示例 2:

输入:5
输出:2
解释:
5 的二进制是 0b101 。

示例 3:

输入:6
输出:1
解释:
6 的二进制是 0b110 。

示例 4:

输入:8
输出:0
解释:
8 的二进制是 0b1000 。
在 8 的二进制表示中没有连续的 1,所以返回 0 。 

提示:

  • 1 <= N <= 10^9

Runtime: 4 ms
Memory Usage: 18.6 MB
 1 class Solution {
 2     func binaryGap(_ N: Int) -> Int {
 3         var res = 0
 4         var last = -1
 5 
 6         for i in 0..<32 {
 7             if (N >> i) & 1 == 1{
 8                 if last != -1 {
 9                     res = max(res, i - last)
10                 }
11                 last = i
12             }
13         }
14 
15         return res
16     }
17 }

4ms

 1 class Solution {
 2     func binaryGap(_ N: Int) -> Int {
 3         var N = N
 4         var f = false, result = 0,length = 0
 5         while N > 0 {
 6             if N%2 == 0 {
 7                 if f {
 8                     length += 1
 9                 }
10             }else {
11                 if f {
12                     result = max(result,length+1)
13                     length = 0
14                 }
15                 f = true
16             }
17             N /= 2
18         }
19         return result
20     }
21 }

Runtime: 4 ms
Memory Usage: 18.4 MB
 1 class Solution {
 2     func binaryGap(_ N: Int) -> Int {
 3         var N = N
 4         var res:Int = 0
 5         var d:Int = -32
 6         while(N > 0)
 7         {
 8             if N % 2 == 1
 9             {
10                 res = max(res, d)
11                 d = 0
12             }
13             N /= 2
14             d += 1
15         }
16         return res
17     }
18 }

16ms

 1 class Solution {
 2         func binaryGap(_ N: Int) -> Int {    
 3         var arr:[Int] = []
 4         var x = N
 5         while x != 0 {
 6             arr.append(x%2)
 7             x = x/2
 8         }
 9         
10         var start = -1
11         var max = 0
12         for i in 0..<arr.count{
13             let m = arr[i]
14             if m == 1 {
15                 if start != -1{
16                     let p = i - start
17                     if p > max{
18                         max = p
19                     }
20                 }
21                 start = i
22             }
23         }        
24         return max
25     }
26 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10596929.html