693. Binary Number with Alternating Bits
来源:互联网 发布:2015中国贸易顺差数据 编辑:程序博客网 时间:2024/06/06 03:02
题目:给定一个正整数,判断该整数的二进制形式相邻的两位是否是不同的。例子如下:
Example 1:
Input: 5Output: TrueExplanation:The binary representation of 5 is: 101
Example 2:
Input: 7Output: FalseExplanation:The binary representation of 7 is: 111.
Example 3:
Input: 11Output: FalseExplanation:The binary representation of 11 is: 1011.
Example 4:
Input: 10Output: TrueExplanation:The binary representation of 10 is: 1010.
最直接的解题思路是,求得整数的二进制形式的每一位,遍历去判断。代码如下:
class Solution {public: bool hasAlternatingBits(int n) { while (n) { int temp1 = n % 2; int temp2 = n / 2 % 2; if (temp1 == temp2) return false; n /= 2; } return true; }};
阅读全文
0 0
- 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
- LeetCode-693. Binary Number with Alternating Bits
- 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
- Binary Number with Alternating Bits
- LWC 53:693. Binary Number with Alternating Bits
- python--leetcode693. Binary Number with Alternating Bits
- Binary Number with Alternating Bits(leetcode)
- Binary Number with Alternating Bits问题及解法
- leetcode 693. Binary Number with Alternating Bits(附正则表达式)
- LeetCode 693 Binary Number with Alternating Bits 带有交替位的二进制数
- bootstrap中使用actionFormatter跳转modal并传递数据的
- Python基础总结
- winshark重要数据结构
- 赛车八码滚雪球走势技巧
- Eclipse配置MyBatis的xml自动提示
- 693. Binary Number with Alternating Bits
- arm的cache 介绍
- tomcat部署war包
- 面向对象程序设计上机练习九(对象指针)
- 机器学习
- Retrofit网络请求库应用02——json解析
- Clone Graph
- Java抽象类
- 已知rsa的模数和指数 生成pem公钥文件