【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 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.
题目大意
判断一个数的二进制表示中是不是相邻的两个数字都是不同的。
解题方法
遍历判断
想法很朴素,判断二进制数任意两个字符是否是不同的即可。
可以用相加为1来判断,也可以直接用不等来判断。
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
bin_n = bin(n)[2:]
return all(int(bin_n[i]) + int(bin_n[i+1]) == 1 for i in xrange(len(bin_n) - 1))
直接判断相邻的数字是不同的:
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
bin_n = bin(n)[2:]
return all(bin_n[i] != bin_n[i+1] for i in xrange(len(bin_n) - 1))
判断是否是交替模式
看别人的代码,速度更快,这是因为符合题目要求的二进制数字的模式是已知的,可以生成所有的模式,从而这个数字是否在这些模式之中。直接判断是不是在所有交替的字符串之间即可。
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
b = 0b1010101010101010101010101010101010101010101010101010101010101010
while b > 0:
if b == n:
return True
b = b >> 1
return False
位运算
又是骚操作。这个操作的是我们先使用n ^ (n - 1),如果是01交错的二进制数字,在经历了这个操作之后,得到的全部是1的二进制数字。第二步使用与运算判断是否是全部为1.
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
n ^= (n >> 1)
return not (n & n + 1)
日期
2018 年 1 月 17 日
2018 年 11 月 9 日 —— 睡眠可以
【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)的更多相关文章
- LeetCode 693 Binary Number with Alternating Bits 解题报告
题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...
- 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 ...
- [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_Easy
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 693. Binary Number with Alternating Bits
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
随机推荐
- perl中tr的用法(转载)
转载:http://blog.sina.com.cn/s/blog_4a0824490101hncz.html (1)/c表示把匹配不上的字符进行替换. $temp="AAAABCDEF&q ...
- 二叉树——根据遍历结果,画出对应的二叉树 转载至:http://canlynet.blog.163.com/blog/static/255013652009112602449178/
这道题目很经典,具体如下: 已知遍历结果如下,试画出对应的二叉树: 前序:A B C E H F I J D G K 中序:A H E C I F J B D K G 解题要点: 1.前序.中序.后序 ...
- Hive-删除表(drop、truncate的区别)
Hive删除操作主要分为几大类:删除数据(保留表).删除库表.删除分区.我将以下图为例清空iot_devicelocation中的数据,之后再删除表.库等. 解释: use xpu123; #使用 ...
- Selenium-IDE在火狐上的扩展
昨天突然想学学 Selenium,就上网查了一些介绍,发现一些教程基本都是比较老版本的了,使用起来略有不便,所以今天试着写一些最新版本的.请参考Selenium官网.文章以下内容都是在 Mac 机器上 ...
- C语言中储存的大小端问题
一.大小端定义 研究变量的高低字节:从左往右看,字节序递增,也就是最右边是最低字节,最右边是最高字节.如 int i = 0x01020304, 01是高字节,04是低字节.如果是字符串如char a ...
- C++构造函数和析构函数初步认识(2)
构造函数的三个作用1.构造对象2.对象初始化3.类型转换 //Test1.h #include<iostream> using namespace std; //构造对象 //初始化对象 ...
- Prompt branches and tab completion
$ chmod +x ~/.git-prompt.sh $ chmod +x ~/.git-completion.bash $ atom ~/.bash_profile 编辑.bash_profile ...
- Output of C++ Program | Set 5
Difficulty Level: Rookie Predict the output of below C++ programs. Question 1 1 #include<iostream ...
- vue-cli安装记录
docker安装 docker network rm mydkdocker network create --subnet=192.168.1.0/24 mydk cat centos-7-x86_ ...
- 项目cobbler+lamp+vsftp+nfs+数据实时同步(inotify+rsync)
先配置好epel源 [root@node3 ~]#yum install epel-release -y 关闭防火墙和selinux [root@node3 ~]#iptables -F [root@ ...