problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlternatingBits(int n) { ; ) { /* errr... if(n&1 && bit==1) return false; else if(n&1) bit = 1; if(n&1==0 && bit==0) return false; els…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址:https://leetcode.com/problems/binary-number-with-alternating-bits/description/ 题目描述 Given a positive integer, check whether it has alternating bits:…
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).toCharArr…
problem 762. Prime Number of Set Bits in Binary Representation solution1: class Solution { public: int countPrimeSetBits(int L, int R) { int res = 0; int bits = 0; for(int i=L; i<=R; i++) { bits = countBits(i); if(isPrime(bits) && bits!=1 ) res…
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 Output: True Explanation: The binary representation of 5 is: 101 Example 2: Input: 7 Output: False Ex…
题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. 题目分析及思路 给定一个正整数,判断它的二进制形式是否是01交替出现.如果是则返回True,否则返回False.可以先获得该数的二进制形式,之后利用切片分别获得其奇数位和偶数位的值.若全为1或0,则是满足要求的数. python代码…
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { public: bool hasAlternatingBits(int n) { n^=n>>; ))==; } }; 这里用到了判定一个二进制n,位上全部为1的方法 (n&(n+1))==0…
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 Output: True Explanation: The binary representation of 5 is: 101 Example 2: Input: 7 Output: False Ex…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/description/ 题目描述 Given two integers L and R, find the count of numbers in the…
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 Output: True Explanation: The binary representation of 5 is: 101 Example 2: Input: 7 Output: False Ex…