problem

693. Binary Number with Alternating Bits

solution1:

class Solution {
public:
bool hasAlternatingBits(int n) {
int bit = -;
while(n>)
{
/*
errr...
if(n&1 && bit==1) return false;
else if(n&1) bit = 1;
if(n&1==0 && bit==0) return false;
else if(n&1==0) bit = 0;
*/
if(n&==)
{
if(bit==) return false;
bit = ;
}
else
{
if(bit==) return false;
bit = ;
}
n >>= ;//err.
}
return true;
}
};

solution2:

通过异或操作判断最低位是否在0/1之间转换进行。

class Solution {
public:
bool hasAlternatingBits(int n) {
int d = (n&);
while((n&)==d)
{
d ^= ;//
n >>= ;
}
return n==;
}
};

参考

1. Leetcode_easy_693. Binary Number with Alternating Bits;

2. Grandyang;

【Leetcode_easy】693. Binary Number with Alternating Bits的更多相关文章

  1. 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...

  2. 693. Binary Number with Alternating Bits - LeetCode

    Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...

  3. 【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation

    problem 762. Prime Number of Set Bits in Binary Representation solution1: class Solution { public: i ...

  4. [LeetCode&Python] Problem 693. Binary Number with Alternating Bits

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

  5. LeetCode 693 Binary Number with Alternating Bits 解题报告

    题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...

  6. 693. Binary Number with Alternating Bits

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  7. [LeetCode] 693. Binary Number with Alternating Bits_Easy

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

  8. 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...

  9. [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

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

随机推荐

  1. PL/SQL嵌入SQL语句

    一.PL/SQL块中只能直接嵌入SELECT.DML(INSERT,UPDATE,DELETE)以及事务控制语句(COMMIT,ROLLBACK,SAVEPOINT), 而不能直接嵌入DDL语句(CR ...

  2. CentOS7下rabbitmq的详细安装教程

    一.安装前的准备工作:[rabbitmq下载] rabbitmq下载官网地址:http://www.rabbitmq.com/ 具体的安装包的下载[这里安装的版本是3.7.5]:https://git ...

  3. TCL服务器端

    import socket def main(): # 创建套接字对象 tcp_server_socket = socket.socket(socket.AF_INET,socket.SOCK_STR ...

  4. mongodb 开发规范

    一.命名规则 1.数据库命名规则 数据库名可以是满足以下条件的任意UTF-8字符串: (1)不能是空字符串(”") : (2)不能含有”(空格)...$./..和(空字符): (3)应全部小 ...

  5. Greenplum 执行计划之广播与重分布

    关联数据在不同节点上,对于普通关系型数据库来说,是无法进行连接的.关联的数据需要通过网络流入到一个节点中进行计算,这样就需要发生数据迁移.数据迁移有广播和重分布两种.在GP中,每一个广播或重分布会产生 ...

  6. Excel开发VBA学习

    1.合并字符串A1&A22.拆分字符串LEFT(A2,SEARCH("-",A2)-1)3.下拉选项Data->Data validation->List 1. ...

  7. (2)打造简单OS-开机BIOS初始化与MBR操作系统引导详解

    ================大概了解即可=============== 1.BIOS的工作: 我们的计算机在开机之前,它是一个纯硬件的机器,但是从按下开机按钮的那一刻起,ROM上的固化程序就开始为 ...

  8. MongoDB之安装部署

    一.安装MongoDB 在安装MongoDB之前,应该先把MongoDB官方网站上下载下来,下载的地址如下: https://www.mongodb.com/download-center 下载完毕之 ...

  9. Linux常用命令大全(最完整)

    要学习Linux语法最好到专门的网站 常用语法大全:https://www.runoob.com/linux/linux-command-manual.html (菜鸟教程) 另外提供一个总结的不错的 ...

  10. Qt之模型/视图(自定义风格)

    Qt之模型/视图(自定义风格) 关于自定义风格是针对视图与委托而言的,使用事件与QSS都可以进行处理,今天关于美化的细节讲解一下. 先看下图: 先撇开界面的美观性(萝卜青菜,各有所爱),就现有的这些风 ...