leetcode693:Binary Number with Alternating Bits
判断一个数字的二进制形式是不是01交替的。
如5=101,返回True
如7=111,返回False
这道题可以用位运算来实现。看到01交替,就想到移位运算。如果n是01交替的,移位之后进行异或,则得到的数字x各个位都是1。也就是x=n^(n>>1)
剩下的任务就是判断一个数字是否是2的幂减一。也就是((x+1)&x)==0
leetcode693:Binary Number with Alternating Bits的更多相关文章
- Leetcode693.Binary Number with Alternating Bits交替位二进制数
给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等. 示例 1: 输入: 5 输出: True 解释: 5的二进制数是: 101 示例 2: 输入: 7 输出 ...
- 【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交替出现,遍历其二进制字符串,下一个与上一个不等 ...
- [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 ...
- 987. Binary Number with Alternating Bits
Description Given a positive integer, check whether it has alternating bits: namely, if two adjacent ...
- [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 ...
随机推荐
- 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset
bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...
- MapReduce模式MapReduce patterns
After having modified and run a job in the last post, we can now examine which are the most frequent ...
- GUI程序设计
1. 对话框(JDialog)使用示例 例1. JDialog简单使用示例. import javax.swing.JLabel; public class demoJDialog { JFrame ...
- 常用sql001_partition by 以及 row_number()和 dense_rank()和rank()区别
create table student ( sid varchar2(10), --学号 sname varchar2(10), --姓名 classid varchar2(10), --班级号 s ...
- TCP的TIME_WAIT快速回收与重用
声明一点: Linux中是无法修改tcp的TIME_WAIT值的,除非重新编译,起码我是没有找到怎么改.值得注意的是,net.ipv4.tcp_fin_timeout这个参数是FIN_WAIT_2的值 ...
- Longest Common Prefix leetcode java
题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是 ...
- 任务栈 启动模式 Task Flag launchMode MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Medication Reconciliation Overview
http://www.hcsinc.net/HCS-Medication-Reconciliation/med-rec-overview.html At HCS, we've worked with ...
- 安全删除U盘或其他硬件 unlocker的使用方法
1 假如我在优盘里新建一个文档,并且正在读写文件. 点击右下角的安全删除的时候弹出如下对话框. 2 在该优盘上右击,Unlocker,弹出的对话框显示NOTEPAD.EXE正在使用. 3 在确保左侧是 ...
- redis队列及多线程应用
由于xxx平台上自己的博客已经很久没更新了,一直以来都是用的印象笔记来做工作中知识的积累存根,不知不觉印象笔记里已经有了四.五百遍文章.为了从新开始能与广大攻城狮共同提高技术能力与水平,随决心另起炉灶 ...