[LeetCode] 693. Binary Number with Alternating Bits_Easy
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.
判断"00" 和"11"是否在str(bin(n))里面即可.
Code
class Solution:
def alternateBits(self, n):
n = bin(n)
return "" not in n and "" not in n
[LeetCode] 693. Binary Number with Alternating Bits_Easy的更多相关文章
- 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】693. Binary Number with Alternating Bits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...
- [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 ...
- 693. Binary Number with Alternating Bits
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- LeetCode算法题-Binary Number with Alternating Bits(Java实现)
这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...
- LeetCode题解之Binary Number with Alternating Bits
1.题目描述 2.问题分析 将数值转换为二进制,然后将前面的 0 去掉,再遍历一边二进制字符串,对每个字符和其后部的字符进行比较. 3.代码 bool hasAlternatingBits(int n ...
随机推荐
- B - Calculation 2
Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...
- oracle相关的知识
01.表空间的创建与删除 Spool 目录 (把sql语句都记录在txt文件中)spool e:\xxx.txtSpool off 结束 SQL> --清除屏幕信息SQL> cle ...
- html表格的基本用法
表格的基本用法 1.<!DOCTYPE html><html><head lang="en"> <meta charset="U ...
- FastDFS数据迁移
参考:https://blog.csdn.net/frvxh/article/details/56293502 FastDFS安装配置参考:https://www.cnblogs.com/minseo ...
- 转发一篇好文:36氪翻译自medium的文章: 读书没有 KPI:为什么坚持“一年读 100 本书”没用?
你只是为了达成所谓的数量目标而读书. 编者按:读书本是一项安静.缓慢的活动,但随着现代社会节奏的加快,信息技术的广泛普及,读书这一行为模式也开始发生了变化.越来越多的人开始碎片化阅读,并且越来越多的文 ...
- dos基本指令
目录操作 dir 操作磁盘文件目录 md / mkdir <folder name> 创建子目录make directory cd 改变当前工作目录,返回上一级 cd .. rd 删 ...
- nethogs 查看 Linux 进程的网络使用
有时候我们客户会发现服务器或 VPS 网络慢,进一步发现大量带宽被占用,一些客户到这里为止就不知道怎么办了.有什么简单办法能找出哪个程序(或者进程)占用了带宽呢?Linux 监控流量的小工具不少,如 ...
- [dpdk] service core
dpdk 17.11 增加了一组新的API,serivce core 如命名,就是用一组core跑service函数. 我自己的测试程序如下: https://github.com/tony-caot ...
- Calcite - StreamingSQL
https://calcite.apache.org/docs/stream.html Calcite's SQL is an extension to standard SQL, not ano ...
- 约数,gcd,exgcd.
很多题都是要求出什么最大公约数或者最小公倍数什么的,也有一些题目是和约数个数有关的,所以需要总结一下. 首先最大公约数和最小公倍数怎么求呢? 当然是观察法了,对于一些很聪明的孩纸他们一般随便一看就秒出 ...