博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
693. Binary Number with Alternating Bits
阅读量:4365 次
发布时间:2019-06-07

本文共 892 字,大约阅读时间需要 2 分钟。

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.

class Solution:    def hasAlternatingBits(self, n):        """        :type n: int        :rtype: bool        """        s = bin(n)        for i in range(2,len(s)-1):            if s[i]=='1':                if s[i+1]!='0':                    return False            else:                if s[i+1]!='1':                    return False        return True

转载于:https://www.cnblogs.com/bernieloveslife/p/9831788.html

你可能感兴趣的文章
STL源码剖析第二章---配置器
查看>>
[DB2]删除大数据量数据及57011错误处理
查看>>
Angular开发实践(一):环境准备及框架搭建
查看>>
随笔6
查看>>
自动类型转换
查看>>
Javascript—②函数
查看>>
HttpServletResponse设置下载文件
查看>>
Javascript 面向对象编程(二)
查看>>
异步加载script实现jsonp效果
查看>>
hihocoder #1260 : String Problem I
查看>>
解决Delphi图形化界面的TEdit、TLable等组件手动拖拽固定大小,但是编译之后显示有差别的情况...
查看>>
Linux下安装MySQL
查看>>
webdriver之富文本,Firefox配置加载
查看>>
iOS开发笔记系列-基础7(C语言特性)
查看>>
cf 164 div2 解题报告
查看>>
最佳实践 | OceanBase事务引擎的技术创新
查看>>
unity中开启和关闭协同程序
查看>>
hdu_2089_不要62(数位DP)
查看>>
red and black(BFS)
查看>>
887. Super Egg Drop
查看>>