LeetCode笔记:693. Binary Number with Alternating Bits

LeetCode --- 693. Binary Number with Alternating Bits 解题报告 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 Output: True Explanation: The binary representation of 5 is: 101 Example 2: Input: 7 Output: Fals. 阅读详情

LeetCode笔记:693. Binary Number with Alternating Bits

96 Cloudox_ 关注

2018.01.13 10:20 字数 468 阅读 16评论 0喜欢 0

问题(Easy):

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.

Example 1:
Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101

Example 2:
Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111.

Example 3:
Input: 11
Output: False
Explanation:
The binary representation of 11 is: 1011.

Example 4:
Input: 10
Output: True
Explanation:
The binary representation of 10 is: 1010.

大意:

给出一个正整数,检查其是否有交替的比特位:也就是说,如果相邻的两个比特位始终有不同的值。

例1:
输入:5
输出:True
解释:
5的二进制表示是:101

例2:
输入:7
输出:False
解释:
5的二进制表示是:111

例3:
输入:11
输出:False
解释:
5的二进制表示是:1011

例4:
输入:10
输出:True
解释:
5的二进制表示是:1010

思路:

最简单的就是循环右移并用模2来获得最低位的值,并依次进行比较看是否不同。

代码(C++):

class Solution {
public:
    bool hasAlternatingBits(int n) {
        int temp = n % 2;
        while (n > 0) {
            n = n >> 1;
            if (n % 2 == temp) return false;
            temp = n % 2;
        }
        return true;
    }
};

他山之石:

交替的比特位其实是个很有特点的排列,如果右移一次,得到的数可以刚好和原本的数互补,比如101010,右移得到10101:

101010
010101

相加就会得到:

11111

我们可以利用这个特点,如果是交替的比特位,右移一位后相加可以得到一个全是1的数,再加1会的得到一个最高位为1,其余位为0的数,与全是1的数相与将为0,但如果不是交替的比特位,就没有这个特性,因此可以这样判断,速度更快。

class Solution {
    public boolean hasAlternatingBits(int n) {
        int a = (n ^(n>>1));
        return (a&(a+1)) ==0;
        
    }
}

合集:https://github.com/Cloudox/LeetCode-Record

基于ENVI5.6与SARscape的哨兵一号时序InSAR实战:从数据准备到PS/SBAS结果解译 本文详细介绍了基于ENVI5.6与SARscape软件,利用哨兵一号(Sentinel-1)卫星数据,进行PS-InSAR与SBAS-InSAR时序分析,以监测地表形变的完整实战流程。内容涵盖从数据下载、软件配置、预处理、核心处理步骤到结果解译与质量评估,重点解析了PS与SBAS方法的关键参数设置、常见问题及解决方案,旨在帮助用户掌握从数据准备到成果分析的完整技术链条。 阅读详情

相关推荐

固定时间收敛的控制器设计(基础知识)

针对控制系统的有限时间收敛和固定时间收敛给出一些常用定理。

qq_40241332的博客 1万+

【Leetcode】693. 交替位二进制数

题目描述:给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等。示例 1:输入: 5 输出: True 解释: 5的二进制数是: 101 示例 2:输入: 7 输出: False 解释: 7的二进制数是: 111 示例 3:输入: 11 输出: False 解释: 11的二进制数是: 1011  示例 4:输入: 10 输出: True 解释: 10的二进制...

EasonWong的博客 774

Windows10完美驱动Apple Studio Display全攻略:从雷电转接到亮度调节(附N卡兼容列表)

本文详细介绍了在Windows10系统下如何完美驱动Apple Studio Display显示器,包括雷电接口转接方案、BootCamp驱动安装及NVIDIA显卡兼容性测试。特别推荐GTX 1660 Super等显卡,提供5K@60Hz稳定输出,并分享色彩管理及日常使用技巧,帮助用户充分发挥这款苹果显示器的性能。

weixin_29284657的博客 1097

Java实现 LeetCode 693 交替位二进制数(位运算)

693. 交替位二进制数 给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等。 示例 1: 输入: 5 输出: True 解释: 5的二进制数是: 101 示例 2: 输入: 7 输出: False 解释: 7的二进制数是: 111 示例 3: 输入: 11 输出: False 解释: 11的二进制数是: 1011 示例 4: 输入: 10 输出: Tru...

南 墙 1万+

Nvidia AGX Xavier刷机指北

因为工作的关系,要在AGX上安装CUDA。nvidia官网上没找到适合AGX版本的CUDA,只能走刷机的路子。刷机过程中,也遇到一些坑,做个记录。 需要有一台ubuntu系统的电脑作为主机Host。我的主机系统为Ubuntu18.04 整个刷机过程为: 在Host上安装sdkmanager,整个刷机过程主要通过sdkmanager来操作。 在Host自动下载一些系统组件,各种deb包 Host上制作OS系统镜像,烧录到AGX中 Host上之前下载好的deb包,安装到AGX中。 完结,撒花 ...

sinat_38685124的博客 6296

【Qt】使用QMediaPlayer播放视频

window平台可以安装K-Lite,linux平台一般各发行版都会默认安装GSteam。播放RTSP视频时,可以使用VLC播放器搭建一个视频推流服务器,用于拉流的客户端的调试。3、如果没有添加Qt环境变量,请把关联的Qt5MultimediaWidgets.dll和Qt5Multimedia.dll放到进程路径下否则也会报ServiceMissingError错误。2、需要安装解码器,否则报 ServiceMissingError 无法找到服务的错误。1、需要在.pro工程文件中引用对应模块。

奔涌的后浪的专栏 2221

当初我为啥要这样学数据结构呢?

原创声明 本文首发于微信公众号【程序员黄小斜】 本文作者:黄小斜 转载请务必在文章开头注明出处和作者。 简介 学习编程,数据结构是你必须要掌握的基础知识,那么数据结构到底是什么呢? 根据百度百科的介绍,数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。数据结构往往同高效的检索算法和索引技...

Java技术江湖 2万+

曙光服务器RAID配置避坑指南:MegaRAID SAS 9361-8i实战(含Foreign状态处理)

本文详细解析了曙光服务器搭配MegaRAID SAS 9361-8i控制器的RAID配置全流程,包括RAID0和RAID5的创建、Foreign状态处理及性能优化策略。特别针对磁盘迁移中的Foreign状态问题,提供了系统化的解决方案和高级恢复技巧,助力企业级IT运维提升存储配置的稳定性和可靠性。

weixin_29200741的博客 418

leetcode693+判断二进制是否相连不同数字

https://leetcode.com/problems/binary-number-with-alternating-bits/description/ class Solution { public: bool hasAlternatingBits(int n) { int flag = n%2; n = n/2; while (n...

小堃哥的专栏 311

使用 MATLAB/Simulink 对风光互补发电系统进行建模

随着全球对清洁能源的需求日益增长,风力发电和光伏发电作为两种主要的可再生能源形式,在电力供应中的占比越来越高。然而,由于风电和光伏的间歇性和不确定性,单独依赖其中任何一种能源都会给电网带来挑战。因此,风光互补发电系统(Wind-PV Hybrid System)应运而生,通过合理配置风力发电机和光伏电池板的数量,可以有效提高系统的稳定性和可靠性。此外,为了确保整个电力系统的安全稳定运行,还需要研究风光互补发电系统与主网之间的协调调度策略。本教程将详细介绍如何使用。

amy_mhd的博客 121

华为昇腾开发——多模型资源管理(C++)

在使用Ascend ACL(Ascend Computing Language)的ACLLite库进行多模型推理时,合理的资源管理至关重要。

如果想成为中心,那么就到中心去吧。 577

leetcode_693_交替位二进制数_easy

给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等。 示例 1: 输入: 5 输出: True 解释: 5的二进制数是: 101 示例 2: 输入: 7 输出: False 解释: 7的二进制数是: 111 示例 3: 输入: 11 输出: False 解释: 11的二进制数是: 1011  示例 4: 输入: 10 输出: ...

Snow_Jie的博客 559

2018安徽省青少年信息学奥林匹克(AHOI)初中组

12 种合法的排列分别为:1, 3, 2, 4, 2, 3, 1, 4, 3, 1, 4, 2, 3, 2, 4, 1, 1, 3, 4, 2, 2, 3,4,1,1,4,2,3, 2,4,1,3, 4,1,3,2, 4,2,3,1, 1,4,3,2, 2,4,3,1。分为 2 组,一组的队员实力值是{4, 5, 2, 3},一组是{-4, -3, -5},其中最小的组人数为 3,可以发现没有比 3 更优的分法了。[0, 1, 1, 2]同样不是合法的分组方案,因为出现了两个实力值为 1 的选手。

weixin_42358969的博客 1143

Leetcode学习笔记:#693. Binary Number with Alternating Bits

Leetcode学习笔记:#693. Binary Number with Alternating Bits Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. 实现: boolean hasA...

ccystewart的博客 189

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode //C - 693. Binary Number with Alternating Bits

【代码】LeetCode //C - 693. Binary Number with Alternating Bits。

Made in Code 873

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

【LeetCode】693. Binary Number with Alternating Bits

Python版本:python3.6.2 693. Binary Number with Alternating Bits Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.

sunflower_kris的博客 364

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

【Leetcode_easy】693. Binary Number with Alternating Bits

problem 693.Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlternatingBits(int n) { int bit = -1; while(n>0) { /...

weixin_30312659的博客 126

[leetcode] 693. Binary Number with Alternating Bits

Description Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 Output: True Explanation: The binary re...

Learning from the mistakes 288

leetcode 693. Binary Number with Alternating Bits

原题: Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 Output: True Explanation: The binary r

zhaihaixu的博客 994

LeetCode-693. Binary Number with Alternating Bits [C++][Java]

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.

贫道绝缘子的博客 1281

LeetCode 693. Binary Number with Alternating Bits

693. Binary Number with Alternating Bits Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5

Gong_zhuo的博客 322

LeetCode-693. Binary Number with Alternating Bits

693. Binary Number with Alternating Bits Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input:

link98 561

LeetCode 693: Binary Number with Alternating Bits

LeetCode 693: Binary Number with Alternating Bits 题目描述 判断一个整数的二进制表示是否会出现连续的 0 和 1。 解题思路 对于 1010 这种位级表示的数,把它向右移动 1 位得到 101,这两个数每个位都不同,因此异或得到的结果为1111。 AC代码 class Solution { public: bool hasAlter...

HakunaMatata的博客 280

[leetcode]693. Binary Number with Alternating Bits

[leetcode]693. Binary Number with Alternating Bits Analysis ummmm~—— [ummmm~] Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have d...

weixin_32135877的博客 198

python leetcode 693. Binary Number with Alternating Bits

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.判断正整数的二进制数是否全为交替位。也就是说只要存在“00”或者“11”即为否。Example 1:Input: 5 Output: True ...

DreamerLHQ的博客 438
上一篇: 【190-Reverse Bits(反转二制)】
下一篇: 【LeetCode】Number Complement 解题报告
binbigdata
博客等级 码龄9年 227粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值