【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 = -;
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的更多相关文章
- 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...
- 693. Binary Number with Alternating Bits - LeetCode
Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...
- 【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 ...
- [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 ...
- LeetCode 693 Binary Number with Alternating Bits 解题报告
题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...
- 693. Binary Number with Alternating Bits
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- [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 ...
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
随机推荐
- from module_name import var
######## a.py ######## aa = "Hello World" ####### b.py ######## from a import aa print aa ...
- sql sever 触发器的概念和使用
触发器简介: 触发器是一种特殊的存储过程,它的执行不是由程序调用,也不是手动执行,而是由事件来触发.触发器是当对某一个表进行操作.例如:update.insert.delete这些操作的时候,系统会自 ...
- Excel开发VBA学习
1.合并字符串A1&A22.拆分字符串LEFT(A2,SEARCH("-",A2)-1)3.下拉选项Data->Data validation->List 1. ...
- [bzoj] Network
http://www.lydsy.com/JudgeOnline/problem.php?id=3732 /* Kruskal 最小生成树 树链剖分 最大值查询 注意:可能会有几块不联通的图 */ # ...
- 数据结构实验之查找一:二叉排序树 (SDUT 3373)
二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),也称二叉搜索树. #include <stdio.h> #include <s ...
- gcc/g++ 链接库的编译与链接
GCC编译步骤 gcc -E t1.c -o t1.i 预处理 gcc -S t1.i -o t1.s 转成汇编语言 gcc -c t1.s -o t1.o 转成机器码 gcc t1.o -o t1. ...
- RSYNC @ERROR: AUTH FAILED ON MODULE XXX 解决思路及附录RSYNC常见问题及解决办法
使用rsync往服务器上传文件时,client报如下异常: @ERROR: auth failed on module XXX rsync error: error starting client-s ...
- JRebel for IntelliJ激活
好久没用jrebel了,跟前端进行项目联调总是有些许改动,还是热部署方便. 目前用的idea版本:IntelliJ IDEA 2019.2 JRebel插件版本:JRebel for IntelliJ ...
- zabbix (二)安装
一.centos7源码安装zabbix3.x 1.安装前环境搭建 下载最新的yum源 #wget -P /etc/yum.repos.d http://mirrors.aliyun.com/repo/ ...
- Spring Boot :Failed to instantiate SLF4J LoggerFactory Reported exception:
Spring Boot出现以下错误: Failed to instantiate SLF4J LoggerFactory Reported exception: Failed to instantia ...