693. Binary Number with Alternating Bits - LeetCode
Question
693. Binary Number with Alternating Bits
Solution
思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等,返回true,如果有相等的就返回false
Java实现:
public boolean hasAlternatingBits(int n) {
char last = '2'; // 非0非1即可
for (char c : Integer.toBinaryString(n).toCharArray()) { // int转二进制字符串
if (c == last) return false;
last = c;
}
return true;
}
693. Binary Number with Alternating Bits - LeetCode的更多相关文章
- 【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 ...
- 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
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] 693. Binary Number with Alternating Bits_Easy
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [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 ...
随机推荐
- leetcode 1962. 移除石子使总数最小
题目描述: 给你一个整数数组 piles ,数组 下标从 0 开始 ,其中 piles[i] 表示第 i 堆石子中的石子数量.另给你一个整数 k ,请你执行下述操作 恰好 k 次: 选出任一石子堆 p ...
- break,return,continue的区别和作用
学习目标: 理解break.return.continue在循环中的区别和作用 学习内容: 1.break break表示结束当前所在的循环. 循环输出到3,当i等于4后,跳出当前循环,继续向下执行循 ...
- macos停止MySQL服务
1.命令行中 使用 find /usr -name mysql 查找自己电脑中MySQL的安装位置 例如我查找到我电脑MySQL安装位置是 /usr/local/Cellar/mysql@5.6/5 ...
- JavaScript中数组的方法和字符串方法总结
数组是首先的一个对象, 可以通过Array构造器创建一个数组,数组方法总结如下 cacat() 链接两个数组 join() 将数组链接成字符串 pop() 删除最后一个元素 shift() 删 ...
- 详解防抖函数(debounce)和节流函数(throttle)
本文转自:https://www.jianshu.com/p/f9f6b637fd6c 闭包的典型应用就是函数防抖和节流,本文详细介绍函数防抖和节流的应用场景和实现. 函数防抖(debounce) 函 ...
- 创建可以运行宿主机GPU的容器
1.安装NVIDIA Container Runtime apt-get参考https://blog.csdn.net/li_ellin/article/details/107180516 yum参考 ...
- C++STL 中的数值算法(iota、accumulate、adjacent_difference、inner_product、partial_sum)
以下算法均包含在头文件 numeric 中 ##1.iota 该函数可以把一个范围内的序列从给定的初始值开始累加 先看用法. 例: 假设我需要一个长度为10,从5开始递增的序列 vector<i ...
- python中字符串、列表访问
一.列表 列表由一系列按特定顺序排列的多个元素或空元素组成,包含字母表中所有字母.数字0~9或所有家庭成员姓名的列表:列表中各元素间可以没有任何关系:实际使用过程中,通常给列表指定一个表示复数的名称, ...
- Springmvc 如何配置一个MAVEN项目,eclipse视图详解
1,首先主页创建一个maven project项目, 2-3,创建完成会报错因为缺少WEB-INF文件右键点击选取,会自动创建web.xml文件 4 ,调试版本 因为默认创建的maven版本不支持,所 ...
- 2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意)
2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意) P1363 幻象迷宫 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 幻象迷宫可以认为是无限 ...