[Swift]LeetCode868. 二進制間距 | Binary Gap

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-ytishidf-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N.git

If there aren't two consecutive 1's, return 0.github

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:app

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

Example 3:spa

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

Example 4:code

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:htm

  • 1 <= N <= 10^9

給定一個正整數 N,找到並返回 N 的二進制表示中兩個連續的 1 之間的最長距離。 blog

若是沒有兩個連續的 1,返回 0 。 three

示例 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 }
相關文章
相關標籤/搜索