Binary Number with Alternating Bits
来源:互联网 发布:阮一峰javascript历史 编辑:程序博客网 时间:2024/06/05 09:44
Given a positive integer, check whether it has alternating bits or not.
Example 1:
Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101
Example 2:
Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111
//法一:class Solution {public: bool hasAlternatingBits(int n) {//5->101 int last = n & 1;//按位与取最后一个 n >>= 1; while (n != 0) { if (last == (n & 1))//最后一个和次个比较 { return false; } else { last = n & 1; } n >>= 1; } return true; }};
//法二:class Solution {public: bool hasAlternatingBits(int n) { int d = n & 1; while ((n & 1) == d) //若d=1 { d = 1 - d;//则d=0 n >>= 1;//右移,取次个,比较次个是否等于0 } return n == 0; }};
阅读全文
0 0
- Binary Number with Alternating Bits
- leetcode 693. Binary Number with Alternating Bits
- 693. Binary Number with Alternating Bits
- python--leetcode693. Binary Number with Alternating Bits
- 693. Binary Number with Alternating Bits
- leetcode 693. Binary Number with Alternating Bits
- LeetCode-693. Binary Number with Alternating Bits
- Binary Number with Alternating Bits(leetcode)
- 693. Binary Number with Alternating Bits
- 693. Binary Number with Alternating Bits
- 693. Binary Number with Alternating Bits
- LeetCode 693. Binary Number with Alternating Bits
- 693. Binary Number with Alternating Bits
- leetcode 693. Binary Number with Alternating Bits
- LWC 53:693. Binary Number with Alternating Bits
- Binary Number with Alternating Bits问题及解法
- LeetCode 693 Binary Number with Alternating Bits 带有交替位的二进制数
- leetcode 693. Binary Number with Alternating Bits(附正则表达式)
- iOS日期选择器
- MYSQL基础
- 给一个数组,找出数组中最长的连续数列的长度
- 虚拟机系统ubuntu 16.04 设置桥接上网
- java 基础知识总结
- Binary Number with Alternating Bits
- 字符串与时间的转换
- docker swarm如何在指定的node上运行指定的容器
- 根据递推式构造矩阵
- 【SSH工具】FinalShell|—服务器管理软件,远程桌面加速工具,支持Windows,Mac OS X,Linux
- 哈理工oj 1266 斐波那契数列
- TensorFlow栗子:生成式对抗网络应用在mnist
- RS-485总线通信协议
- 51Nod-1573-美丽的集合