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 ...
随机推荐
- Mybatis编写配置文件时,需要注意配置节点的顺序
mybatis-config.xml配置文件配置时,要注意节点顺序 <properties>...</properties> <settings>...</s ...
- Leetcode152. Maximum Product Subarray乘积的最大子序列
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. 示例 2 ...
- Gym 100712H
Gym 100712Hhttps://vjudge.net/problem/195715/origin先缩点,再建立新图,然后跑两遍dfs求树上最长路 #include<iostream> ...
- 再不懂时序就 OUT 啦!,DBengine 排名第一时序数据库,阿里云数据库 InfluxDB 正式商业化!
云数据库 InfluxDB® 版介绍 阿里云数据库 InfluxDB® 版已于近日正式启动商业化 . 云数据库 InfluxDB® 是基于当前最流行的开源数据库 InfluxDB 提供的在线数据库服务 ...
- Ionic 列表、文本 自动 换行
1.采用row 布局的row-warp 来处理 <div class="item item-icon-right"> <span>图片相册</span ...
- Django项目:CRM(客户关系管理系统)--70--60PerfectCRM实现CRM学生上课记录
#urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...
- Android实战技巧之四十一:制作自己的Android SDK
标签: sdkandroid定制sdk 2015-09-21 18:05 11237人阅读 评论(2) 收藏 举报 分类: Android(260) 版权声明:本文为博主原创文章,未经博主允许 ...
- 深入浅出 Java Concurrency (15): 锁机制 part 10 锁的一些其它问题[转]
主要谈谈锁的性能以及其它一些理论知识,内容主要的出处是<Java Concurrency in Practice>,结合自己的理解和实际应用对锁机制进行一个小小的总结. 首先需要强调的一点 ...
- 使用em为单位制作两列弹性布局
一.DIV布局按照定位的方法分为:浮动方法(float),坐标定位方法(position),还有就是两者相结合的方法. 二.DIV布局按照定义单位的不同可分为:固定宽度布局.流体布局.弹性布局和混合布 ...
- PHP基于openssl实现的非对称加密操作
使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密. 1.安装openssl和php的openssl扩展 2.生成私钥:openssl genrsa 用于生成 ...