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 ...
随机推荐
- metaclass(元类)
一.创建类的执行流程 二.元类的认识 什么是元类呢?在Python3中继承type的就是元类 二.元类的示例 方式一: # 方式一 class MyType(type): '''继承type的就是元类 ...
- 继续JS之DOM对象二
前面在JS之DOM中我们知道了属性操作,下面我们来了解一下节点操作.很重要!! 一.节点操作 创建节点:var ele_a = document.createElement('a');添加节点:ele ...
- django之ORM数据库操作
一.ORM介绍 映射关系: 表名 -------------------->类名 字段-------------------->属性 表记录----------------->类实例 ...
- 【层次聚类】python scipy实现
层次聚类 原理 有一个讲得很清楚的博客:博客地址 主要用于:没有groundtruth,且不知道要分几类的情况 用scipy模块实现聚类 参考函数说明: pdist squareform linkag ...
- Unity3D用户手册
Unity Manual 用户手册 Welcome to Unity. 欢迎使用Unity. Unity is made to empower users to create the best int ...
- Java 写一段字符到指定的文本文档中,如果该文本文档不存在,则创建该文本文档
写一段字符到指定的文本文档中,如果该文本文档不存在,则创建该文本文档 import java.io.File; import java.io.FileNotFoundException; import ...
- .tar.xz文件的解压方法
废话不多说: 直接看 方法一: tar -xvJf ***.tar.gz 方法二: 先减压成 .tar 格式的文件, 再解压 .tar #xz是一个工具, 系统中没有安装,需要下载 xz -d *** ...
- jenkins 实现测试发布、预发布、真实发布、回滚发布
主要思路: 1.做三个文件夹,用于放置不可随意修改的配置文件(测试发布.预发布.真实发布) 2.每次都先修改配置文件再进行构建(构建时会先把配置文件复制到构建的目录,再同步到发布的目录) 3.发布完代 ...
- JMeter 中对于Json数据的处理方法
JMeter中对于Json数据的处理方法 http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/ J ...
- AI学习吧-结算中心
结算中心流程 在结算中心中,主要是对用户添加到购物车商品的结算,由于用户可能添加了多个课程,但是,结算时会选择性的进行支付.在结算时会选中课程id,和对应的价格策略.在后台,首先会对用户进行校验,验证 ...