Leetcode693.Binary Number with Alternating Bits交替位二进制数
给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等。
示例 1:
输入: 5 输出: True 解释: 5的二进制数是: 101
示例 2:
输入: 7 输出: False 解释: 7的二进制数是: 111
示例 3:
输入: 11 输出: False 解释: 11的二进制数是: 1011
示例 4:
输入: 10 输出: True 解释: 10的二进制数是: 1010
class Solution {
public:
bool hasAlternatingBits(int n) {
int flag = -1;
while(n)
{
int temp = n % 2;
n /= 2;
if(flag != -1)
{
if(flag == temp)
return false;
}
flag = temp;
}
return true;
}
};
Leetcode693.Binary Number with Alternating Bits交替位二进制数的更多相关文章
- leetcode693:Binary Number with Alternating Bits
判断一个数字的二进制形式是不是01交替的. 如5=101,返回True 如7=111,返回False 这道题可以用位运算来实现.看到01交替,就想到移位运算.如果n是01交替的,移位之后进行异或,则得 ...
- 693. Binary Number with Alternating Bits - LeetCode
Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...
- 【Leetcode_easy】693. Binary Number with Alternating Bits
problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...
- [Swift]LeetCode693. 交替位二进制数 | Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [LeetCode] 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...
- 987. Binary Number with Alternating Bits
Description Given a positive integer, check whether it has alternating bits: namely, if two adjacent ...
- LeetCode 693 Binary Number with Alternating Bits 解题报告
题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...
- [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 ...
随机推荐
- springboot新增jsp的支持
一.添加依赖 <!-- 添加对jsp的支持 --> <!-- web 依赖 --> <dependency> <groupId>org.springfr ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- Windows API 第三篇
1.获得程序自身的路径: DWORD GetModuleFileName( HMODULE hModule, // handle to module LPTSTR lpFilename, // pat ...
- Gym 100712H
Gym 100712Hhttps://vjudge.net/problem/195715/origin先缩点,再建立新图,然后跑两遍dfs求树上最长路 #include<iostream> ...
- JZOJ5857 【NOIP提高组模拟A组2018.9.8】没有上司的舞会
题目 Description "那么真的有果尔德施坦因这样一个人?"他问道. "是啊,有这样一个人,他还活着.至于在哪里,我就不知道了." "那么那个 ...
- csp-s模拟测试b组加餐antipalindome,randomwalking,string题解
题面:https://www.cnblogs.com/Juve/articles/11599318.html antipalindome: 打表找规律? 对于一个回文串,我们只要保证3位以内不回文即可 ...
- webpack配置根据浏览器自动添加css前缀的loader
1.安装 postcss-loader autoprefixer npm install postcss-loader autoprefixer --save-dev 2.配置webpack.conf ...
- layer时间插件
引入: <link rel="stylesheet" href="<{$cdnsite}>/default/common/layui/css/layui ...
- camtasia Studio 7 的使用
最近领导给了个任务,要把我们的三维应用功能做个视频,好带出去宣传.通过搜索,发现大家都说camtasia Studio好用,很快在网上找到了,与大家分享链接: http://pan.baidu.com ...
- thinkphp浏览历史功能实现方法
这篇文章主要介绍了thinkphp浏览历史功能实现方法,可实现浏览器的浏览历史功能,是非常实用的技巧,需要的朋友可以参考下 本文实例讲述了thinkphp浏览历史功能实现方法,分享给大家供大家参考.具 ...