【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 FileHandle 模块使用
打开多个文件时必备啊 1 use FileHandle; 2 3 my (%index,%fh); 4 # 创建句柄,这里利用gzip 压缩下 5 foreach my $k(keys %index) ...
- 数据分析体系 — 用户粘性的两个计算指标(DAU/MAU和月人均活跃天数)
很多运营都了解DAU(日活跃用户数)和MAU(月活跃用户数)的重要性,但在某些情况下这两个数值本身并不能反映出太多问题,这个时候就要引用到[DAU/MAU]的概念,即[日活/月活] 用户粘性的两个计算 ...
- Spark3学习【基于Java】3. Spark-Sql常用API
学习一门开源技术一般有两种入门方法,一种是去看官网文档,比如Getting Started - Spark 3.2.0 Documentation (apache.org),另一种是去看官网的例子,也 ...
- Demo03找素数
package Deom1;import java.awt.*;import java.util.Scanner;public class lx {//输入任意两个正整数,求出这两个正整数之间素数的个 ...
- MybatisPlus入门程序
参考资料:MybatisPlus官网 环境搭建 创建数据库 CREATE DATABASE `mybatisplus` USE `mybatisplus` CREATE TABLE `user ...
- linux系统中tomcat的安装及使用
linux系统中tomcat的安装及使用 linux系统中安装tomcat tar.gz/tar文件格式安装 先下载好该文件,将文件放置在校安装的目录下, 如果是tar.gz后缀使用 tar -zxv ...
- MapReduce02 序列化
目录 MapReduce 序列化 概述 自定义序列化 常用数据序列化类型 int与IntWritable转化 Text与String 序列化读写方法 自定义bean对象实现序列化接口(Writable ...
- 紧张 + 刺激,源自一次 OOM 历险
作者 | 蚂蝗 背景 Erda 是集 DevOps.微服务治理.多云管理以及快数据管理等多功能的开源一站式企业数字化平台.其中,在 DevOps 模块中,不仅有 CI/CD.项目协同等功能,同时还 ...
- absent, absolute, absorb
absent Absenteeism is a habitual [习惯性的] pattern of absence from a duty or obligation [职责] without go ...
- 零基础学习java------day1------计算机基础以及java的一些简单了解
一. java的简单了解 Java是一门面向对象编程语言,不仅吸收了C++的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征.Java语言作为静态 ...