[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 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
"""
b=bin(n)[2:] l=len(b)
flag=True for i in range(l):
if i!=l-1:
if b[i]==b[i+1]:
flag=False
break return flag
[LeetCode&Python] Problem 693. 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】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&Python] Problem 762. Prime Number of Set Bits in Binary Representation
Given two integers L and R, find the count of numbers in the range [L, R](inclusive) having a prime ...
- 693. Binary Number with Alternating Bits
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- [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] 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 ...
随机推荐
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...
- mount: unknown filesystem type 'LVM2_member'解决方案【转】
一台服务器,普通/dev/sda1/2(硬盘一) 同步数据到 lvm_member(硬盘二) rsync两硬盘数据同步: From: http://hi.baidu.com/williwill/ite ...
- android--------HttpURLConnection的get,post和图片加载
URLConnection是个抽象类,它有两个直接子类分别是HttpURLConnection和JarURLConnection.另外一个重要的类是URL,通常URL可以通过传给构造器一个String ...
- MySql 定时完成备份
<?php /*定时备份数据库文件*/ //设置时区 date_default_timezone_set('PRC'); //创建目录 $dirname = 'e:/mysql_dump/'.d ...
- UVA-208 Firetruck (回溯)
题目大意:给一张无向图,节点编号从1到n(n<=20),按字典序输出所有从1到n的路径. 题目分析:先判断从1是否能到n,然后再回溯. 注意:这道题有坑,按样例输出会PE. 代码如下: # in ...
- C#窗体控件简介ListBox
ListBox 控件 ListBox 控件又称列表框,它显示一个项目列表供用户选择.在列表框中,用户 一次可以选择一项,也可以选择多项. 1.常用属性: (1) Items属性: 用于存放列表框中的列 ...
- hibernate中删除表遇到主键被外键引用违反完整约束条件不能删除的问题
MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据.可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况. SET FOREIGN_KEY_CHECKS ...
- transition多个属性同时渐变(left/top)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- 小程序animation动画效果综合应用案例(交流QQ群:604788754)
如果案例有问题,可到QQ群找到今日相关压缩文件下载测试. WXML: <view class="cebian"> <view animation="{{ ...
- css display&&hidden
display:none与visible:hidden的区别 display:none和visible:hidden都能把网页上某个元素隐藏起来,但两者有区别: display:none ---不为被 ...