LeetCode题解之Binary Number with Alternating Bits
1、题目描述
2、问题分析
将数值转换为二进制,然后将前面的 0 去掉,再遍历一边二进制字符串,对每个字符和其后部的字符进行比较。
3、代码
bool hasAlternatingBits(int n) {
if( n <= )
return true;
bitset<> b(n) ;
string s = b.to_string() ;
string::iterator it = s.begin() ;
while( it != s.end() ){
if( *it != '' )break;
++it;
} s.assign(it,s.end() );
it = s.begin() ;
while( it != s.end()- ){
if( *it == *(it + ) ) return false;
++it;
}
return true;
}
LeetCode题解之Binary Number with Alternating Bits的更多相关文章
- 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...
- LeetCode算法题-Binary Number with Alternating Bits(Java实现)
这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...
- 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] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [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 解题报告
题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...
- [Swift]LeetCode693. 交替位二进制数 | Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- 987. Binary Number with Alternating Bits
Description Given a positive integer, check whether it has alternating bits: namely, if two adjacent ...
随机推荐
- spring@Transactional注解事务不回滚不起作用无效的问题处理
这几天在项目里面发现我使用@Transactional注解事务之后,抛了异常居然不回滚.后来终于找到了原因. 如果你也出现了这种情况,可以从下面开始排查. 一.特性先来了解一下@Transaction ...
- 浅谈Retrofit2+Rxjava2
近几年,Retrofit犹如燎原之火搬席卷了整个Android界.要是不懂Retrofit,简直不好意思出门...由于近几个项目都没用到Retrofit,无奈只能业余时间自己撸一下,写的不好的地方,还 ...
- JAVA多态计算面积main函数调用方法
public static void main(String[] args) { Shape shape; Scanner input = new Scanner(System.in); System ...
- Python学习--23 内建模块及第三方库
本文将介绍python里常用的模块.如未特殊说明,所有示例均以python3.4为例: $ python -V Python 3.4.3 网络请求 urllib urllib提供了一系列用于操作URL ...
- JavaWeb 简单实现客户信息管理系统
项目介绍 本项目使用Jsp+Servlet+MySQL实现 功能介绍 查询客户信息:查询数据库中所有客户信息,分页展示 添加客户信息:创建新客户并添加到数据库中 搜索客户信息:根据用户的输入查询客户信 ...
- 利用https实现站点的访问
简介 它是由Netscape开发并内置于其浏览器中,用于对数据进行压缩和解压操作,并返回网络上传送回的结果.HTTPS实际上应用了Netscape的安 全套接字层(SSL)作为HTTP应用层的子层.( ...
- CodeForces -977F(突破定式思维+map应用)
题目链接: https://cn.vjudge.net/problem/CodeForces-977F /* 问题 输入n和n个数的数列 计算并输出最长增量为1的上升子序列 解题思路 用n2的最长上升 ...
- [转]webpack4.0.1安装问题和webpack.config.js的配置变化
本文转自:https://blog.csdn.net/jiang7701037/article/details/79403637 The CLI moved into a separate packa ...
- Linux下安装SQL Server 2016(准备篇SQL Server on linux)
要在Linux安装sql server,首先的先有linux,下面会讲下载,然后安装Linux. 一.下载centos 7.2 : Centos 7.2下载 大概4G左右,这个链接的下载速度还是非常快 ...
- 检测锁及死锁详细信息,及sql语句
SELECT SessionID = s.Session_id, l.request_session_id spid, a.blocked, a.start_time, a.ecid, OBJECT_ ...