LeetCode 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.
题目分析及思路
给定一个正整数,判断它的二进制形式是否是01交替出现。如果是则返回True,否则返回False。可以先获得该数的二进制形式,之后利用切片分别获得其奇数位和偶数位的值。若全为1或0,则是满足要求的数。
python代码
class Solution:
def hasAlternatingBits(self, n: int) -> bool:
b_str = bin(n)
b_substr = b_str[2:]
evens = b_substr[0::2]
odds = b_substr[1::2]
if evens.count('1') == len(evens) and odds.count('0') == len(odds):
return True
else:
return False
LeetCode 693 Binary Number with Alternating Bits 解题报告的更多相关文章
- 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...
- 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 ...
随机推荐
- d3实现的力向导图
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Selenium Web 自动化 - 如何找到元素
Selenium Web 自动化 - 如何找到元素 2016-07-29 1. 什么是元素? 元素:http://www.w3school.com.cn/html/html_elements.asp ...
- oracle中decode的用法(例子)
使用结构: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下: IF 条件=值1 THEN RETURN(翻译值1)ELSIF 条件=值2 THEN ...
- 1. ansible-playbook 变量定义与引用
简单的playbook playbook 是ansible的核心组件,使用的是YAML语法. 下面请看简单的playbook代码 [root@LeoDevops playb]# cat nginx.y ...
- PowerDesigner 16PDM显示备注
默认显示的列是Name及类型 name和code不支持同时显示,设置方法: Tool->Model Options->Name Convention->右侧display中选择显示n ...
- 用Physijs在场景中添加物理效果
1.创建可用Physijs的基本Three.js场景 创建一个可用Physijs的Three.js场景非常简单,只要几个步骤即可.首先我们要包含正确的文件, 需要引入physi.js文件.实际模拟物理 ...
- iOS mac终端下的SQL语句
本文转载至 http://blog.csdn.net/majiakun1/article/details/41281801 我们都知道数据库的创建可以借助图形化的数据库工具软件,但也可以在Mac终端下 ...
- MapReduce处理HBase出错:XXX.jar is not a valid DFS filename
原因:Hadoop文件系统没有检查路径时没有区分是本地windows系统还是Hadoop集群文件系统 解决: 只需将Map和Reduce的init方法最后一个参数(boolean addDepend ...
- 【SpringCloud微服务实战学习系列】服务治理Spring Cloud Eureka
Spring Cloud Eureka是Spring Cloud Netflix微服务中的一部分,它基于NetFlix Sureka做了二次封装,主要负责完成微服务架构中的服务治理功能. 一.服务治理 ...
- Ubuntu屏幕录像软件推荐-Kazam
sudo apt-get install kazam https://blog.csdn.net/weixin_40153532/article/details/79337630