,敢教日月换新天。为有牺牲多壮志

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10596929.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }
复制代码

 

posted @   为敢技术  阅读(286)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°