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. 你应该知道的16个linux命令

    Linux常用命令很多比如cd.ls.pwd.du.df.tail.head.yum.mv.touch.mkdir.cp.tar等,根据工作内容不同,经常使用的命令也不同,比如运维人员使用tail.h ...

  2. P3599 Koishi Loves Construction——构造题

    题目 Task1:试判断能否构造并构造一个长度 $n$ 的 $1...n$ 的排列,满足其 $n$ 个前缀和在模 $n$ 的意义下互不相同 Task2:试判断能否构造并构造一个长度 $n$ 的 $1. ...

  3. jquery左划出现删除按钮,右滑隐藏

    jquery左侧划出显示删除按钮,右滑动隐藏删除按钮 <!doctype html> <html> <head> <meta charset="ut ...

  4. jquery判断input选中事件

    需求是默认第一个是选中状态,点第二个选中,第一个取消然后点支付时,跳转新页面 $(function(){ $(".nl_zhifutj a").click(function(){ ...

  5. mac XXX 已损坏,打不开。 您应该将它移到废纸篓。

    mac 上的软件我改了某些配置,打开提示这个. 是因为安全认证问题. 在系统偏好设置里面,安全与隐私设置为允许任何来源就可以. 如果新版系统没有这个选项,那么在命令行输入:sudo spctl --m ...

  6. Nginx设置禁止通过IP访问服务器并且只能通过指定域名访问

    为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,需要在nginx上设置禁止通过IP访问服务器,只能通过域名访问. 最关键的一点是,在server的设置里面添加这么一行: liste ...

  7. springboot整合springsecurity遇到的问题

    在整合springsecurity时遇到好几个问题,自动配置登录,下线,注销用户的操作,数据基于mybatis,模版引擎用的thymeleaf+bootstrap. 一.认证时密码的加密(passwo ...

  8. 使用webuploader实现大文件上传分片上传

    本人在2010年时使用swfupload为核心进行文件的批量上传的解决方案.见文章:WEB版一次选择多个文件进行批量上传(swfupload)的解决方案. 本人在2013年时使用plupload为核心 ...

  9. luogu 2617

    动态区间 $k$ 大主席树 + 树状数组树状数组的每个点对应一颗线段树首先将所有点加入数据结构 枚举 x code: for(int i = x; i <= n; i += Lowbit(i)) ...

  10. Kmeans聚类(lena图)

    lena512.raw 下载地址:https://files.cnblogs.com/files/jzcbest1016/lena512_20171219131444306.rar .raw文件可以用 ...