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. JVM 监控工具 jstack 和 jvisualvm 的使用

    Java线程状态 线程的五种状态 * 新建:new(时间很短) * 运行:runnable * 等待:waitting(无限期等待),timed waitting(限期等待) * 阻塞:blocked ...

  2. 使用Nagios打造专业的业务状态监控

    想必各个公司都有部署zabbix之类的监控系统来监控服务器的资源使用情况.各服务的运行状态,是否这种监控就足够了呢?有没有遇到监控系统一切正常确发现项目无法正常对外提供服务的情况呢?本篇文章聊聊我们如 ...

  3. Vue + Element UI 实现权限管理系统 前端篇(九):接口格式定义

    接口请求格式定义 前台显示需要后台数据,我们这里先把前后端交互接口定义好,没有后台的时候,也方便用mock模拟. 接口定义遵循几个规范: 1. 接口按功能模块划分. 系统登录:登录相关接口 用户管理: ...

  4. JDK8简化if-else

    简化if-else 1234567891011 User user = ...if (user != null) { String userName = user.getUserName(); if ...

  5. ExecutorService接口概要

    ExecutorService接口继承于Executor接口,主要提供以下额外功能: 管理终结 产生Future对象,用于跟踪一个或多个任务的进度.   ExecutorService可以被shut ...

  6. sqoop部署及使用

    一.概述 sqoop是hive.hdfs.hbase等与RDMBS(mysql等)之间的沟通桥梁,主要通过JDBC与RDMBS进行交互.有两个版本sqoop1和sqoop2,sqoop1架构简单,使用 ...

  7. 超详细的Java时间工具类

    package com.td.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.Pa ...

  8. UVa 442 Matrix Chain Multiplication(栈的应用)

    题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...

  9. Nullable<T>、Nullable、null、?修饰符的区别

    这章我们讨论一下Nullable<T>.Nullable.null.?修饰符的区别 原创文章 Nullable<T>的前世今生 讨论它们之前,我们有必要讨论一下Nullable ...

  10. CentOS 7.2安装Docker-ce

    1.Docker分类 Docker Engine改为Docker CE(社区版) 它包含了CLI客户端.后台进程/服务以及API.用户像以前以同样的方式获取.Docker Data Center改为D ...