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 ...
随机推荐
- 关于配置 TeamCity 清理历史 artifacts 问题
使用 CI 一段时间后,artifacts 占用的磁盘会很大,可以配置保留多少天的 artifacts,具体如下: Administration Click the Edit link for any ...
- Silverlight中使用MVVM(3)—进阶
这篇主要引申出Command结合MVVM模式在应用程序中的使用 我们要做出的效果是这样的 就是提供了一个简单的查询功能将结果绑定到DataGrid中,在前面的基础上,这个部分相对比较容易实现了 我们在 ...
- html中img图片居中
直接看代码 style="display:block; margin:0 auto;" 可以看到,蓝色的代码是让 块状元素居中 而红色的代码,是让img转成块状元素 ···原来im ...
- Linux进程间的通信
一.管道 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: A. 管道是半双工的,数据只能向一个方向流动: B. 需要双工通信时,需要建立起两个管道: C. 只能用于父子进程或者兄弟 ...
- Linux 普通用户启动nginx
众所周知,apache的80端口为系统保留端口,如果通过其他非root用户启动,会报错如下: ()Permission denied: make_sock: could not bind to add ...
- QT QPushButton
#include<QApplication> #include<QWidget> #include<QPushButton> #include<QMenu&g ...
- 【PyTorch深度学习60分钟快速入门 】Part0:系列介绍
说明:本系列教程翻译自PyTorch官方教程<Deep Learning with PyTorch: A 60 Minute Blitz>,基于PyTorch 0.3.0.post4 ...
- webpack4 自学笔记三(提取公用代码)
全部的代码及笔记都可以在我的github上查看, 欢迎star:https://github.com/Jasonwang911/webpackStudyInit/tree/master/commonT ...
- [转]SQL - Create XML - How to set Unicode UTF-8
本文转自:https://stackoverflow.com/questions/44754356/sql-create-xml-how-to-set-unicode-utf-8 I found a ...
- 详解contextConfigLocation|Spring启动过程详解
spring的应用初始化流程一直没有搞明白,刚刚又碰到了相关的问题.决定得好好看看这个流程.我们在开发spring的项目当中基本上都会在web.xml通过: <context-param> ...