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的更多相关文章

  1. 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...

  2. LeetCode算法题-Binary Number with Alternating Bits(Java实现)

    这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...

  3. 693. Binary Number with Alternating Bits - LeetCode

    Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...

  4. 【Leetcode_easy】693. Binary Number with Alternating Bits

    problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...

  5. [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  6. [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 ...

  7. LeetCode 693 Binary Number with Alternating Bits 解题报告

    题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...

  8. [Swift]LeetCode693. 交替位二进制数 | Binary Number with Alternating Bits

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  9. 987. Binary Number with Alternating Bits

    Description Given a positive integer, check whether it has alternating bits: namely, if two adjacent ...

随机推荐

  1. shell脚本补缺

    一.执行脚本的三种方法. chmod  +x  添加执行权限 ./example.sh        相对路径 root/test/example.sh  绝对路径 bash example.sh   ...

  2. 【IT笔试面试题整理】字符串转数组+数组转字符串

    [试题描述]定义一个函数,字符串转数组数组转字符串 [参考代码] public static int strToInt(String str) { int i = 0, num = 0; char[] ...

  3. mysql索引总结(4)-MySQL索引失效的几种情况

    mysql索引总结(1)-mysql 索引类型以及创建 mysql索引总结(2)-MySQL聚簇索引和非聚簇索引 mysql索引总结(3)-MySQL聚簇索引和非聚簇索引 mysql索引总结(4)-M ...

  4. webpack4 自学笔记二(typescript的配置)

    全部的代码及笔记都可以在我的github上查看, 唤醒star: https://github.com/Jasonwang911/webpackStudyInit/tree/master/typesc ...

  5. 递推、数位DP解析(以HDU 2089 和 HDU 3555 为例)

    HDU 2089 不要62 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2089 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人 ...

  6. 嵌入式Linux安装Dropbear SSH server

    0. 背景 OpenSSH因为其相对较大,一般不太适用于嵌入式平台,多用于PC或者服务器的Linux版本中. Dropbear是一个相对较小的SSH服务器和客户端.它运行在一个基于POSIX的各种平台 ...

  7. C# Code First 配置

    前言 所谓code first,是代码优先,而不是传统的数据库优先. code first的好处多多,可不用考虑数据库,只考虑面向对象.下面我们介绍一下code first的配置. 下一篇文章地址:C ...

  8. ASP.NET Core (二):入门

    上一篇:ASP.NET Core(一):简介 下一篇:(待续) 英文原版:Getting Started 1. 安装 .NET Core 2. 创建 .NET Core 项目 在命令提示符窗口输入命令 ...

  9. WEB控件没有什么所谓好不好,而是用得好不好

    这几天Insus.NET有写几篇博文,虽然写得没怎么样,但均是Insus.NET现实开发过程中所遇或是所想的一些内容.<没有什么,开发ASP.NET时随便写写,想到什么写什么>http:/ ...

  10. Windows7下IIS+php配置教程 http://www.jb51.net/article/113812.htm

    这篇文章主要为大家详细介绍了Windows7下IIS+php配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 WINDOWS 7 IIS+php配置教程,具体内容如下 打开 开始 -> ...