987. Binary Number with Alternating Bits
Description
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
Example
Example 1:
Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101
Example 2:
Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111.
Example 3:
Input: 11
Output: False
Explanation:
The binary representation of 11 is: 1011.
Example 4:
Input: 10
Output: True
Explanation:
The binary representation of 10 is: 1010.
public class Solution {
/**
* @param n: a postive Integer
* @return: if two adjacent bits will always have different values
*/
public boolean hasAlternatingBits(int n) {
// Write your code here
//通过位运算,判断到最高位(最高为肯定为1)
while(n != 0) {
//取出尾数
int temp = n % 2;
//将n移位,取出尾数
n= n >> 1;
//取出移位之后的尾数
int test = n % 2;
// 用过异或判断是否不同(不同便是1)
if((temp ^ test) != 1) {
return false;
}
}
return true;
}
}
987. Binary Number with Alternating Bits的更多相关文章
- 【Leetcode_easy】693. Binary Number with Alternating Bits
problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...
- 693. Binary Number with Alternating Bits - LeetCode
Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [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&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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...
- LeetCode 693 Binary Number with Alternating Bits 解题报告
题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...
- LeetCode算法题-Binary Number with Alternating Bits(Java实现)
这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...
- 693. Binary Number with Alternating Bits
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
随机推荐
- WireShark Flow capture analysis
Wiresharkl流量分析 1.图示是对WiFi数据接口的80号端口进行数据捕获,设置混杂模式 过滤表达式设置: IP地址设置过滤 ip.src==191.168.1.102 ip.dst ...
- HTML&javaSkcript&CSS&jQuery&ajax(四)
一.HTML创建响应设计 Responsive Web Design 可以改变尺寸传递网页,对于平板和移动设备是必须的 1.<!DOCTYPE html><html lang=&qu ...
- bzoj 2190
题意:求 题解:这题...数据范围是真小... 研究一下这一表达式,发现gcd(i,j)=1表示i,j互质,那么互质肯定能想到欧拉函数,可是欧拉函数要求j<i,那么我们变化一下:显然原矩阵是对称 ...
- SPY
问题 : SPY 时间限制: 1 Sec 内存限制: 128 MB 题目描述 The National Intelligence Council of X Nation receives a pie ...
- 小LK玩积木
小LK玩积木 时间限制: 1 Sec 内存限制: 128 MB 题目描述 HH最近通过黑洞APP下载了一个盗梦APP,据说能进入一个人的梦里做一些嘿嘿嘿的事情,秉着怀疑的态度HH偷偷地潜入LK的梦中 ...
- Shiro+Redis实现tomcat集群session共享
一.背景 当我们使用了nginx做项目集群以后,就会出现一个很严重的问题亟待解决,那就是:tomcat集群之间如何实现session共享的问题,如果这个问题不解决,就会出现登陆过后再次请求资源依旧 ...
- mysql 查看某个数据库中所有表的数据量
1.登录mysql 2.使用命令:use information_schema; 3.使用命令:select table_name,table_rows from tables where TABLE ...
- matlab转c++代码实现(主要包含C++ std::vector,std::pair学习,包含数组与常数相乘,数组相加减,将数组拉成一维向量,图片的读入等内容)
MATLAB部分: xmap = repmat( linspace( -regionW/2, regionW/2, regionW), regionH, 1 );%linspace [x1,x2,N] ...
- 步步为营-89-SQL语句(删除重复数据)
1:删除重复数据 --第一步:先找到重复数据 select ProcInstID from record_errorlog group by ProcInstID having count(ProcI ...
- Hadoop Shell命令(基于linux操作系统上传下载文件到hdfs文件系统基本命令学习)
Apache-->hadoop的官网文档命令学习:http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html FS Shell 调用文件系统( ...