693. Binary Number with Alternating Bits

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
Explanation:
The binary representation of 7 is: 111.

Example 3:

Input: 11
Output: False
Explanation:
The binary representation of 11 is: 1011.

Example 4:

Input: 10
Output: True
Explanation:
The binary representation of 10 is: 1010.

判断一个数的二进制中,01是否是交替出现的

C++(3ms):
 1 class Solution {
 2 public:
 3     bool hasAlternatingBits(int n) {
 4         int t1 , t2 ;
 5         t1 = n%2 ;
 6         n/=2 ;
 7         while(n){
 8           t2 = n%2 ;
 9           n/=2 ;
10           if (t2 == t1)
11               return false ;
12           t1 = t2 ;
13         }
14         return true ;
15     }
16 };

C++(3ms):

 1 class Solution {
 2 public:
 3     bool hasAlternatingBits(int n) {
 4         int d = n&1 ;
 5         while((n&1) == d){
 6             d = 1-d ;
 7             n >>= 1 ;
 8         }
 9         return n == 0 ;
10     }
11 };

java(14ms):

 1 class Solution {
 2     public boolean hasAlternatingBits(int n) {
 3         int d = n&1 ;
 4         while((n&1) == d){
 5             d = 1-d ;
 6             n >>= 1 ;
 7         }
 8         return n == 0 ;
 9     }
10 }
 
时间: 2024-11-06 02:03:48

693. Binary Number with Alternating Bits的相关文章

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).toCharArr

Leetcode - 693. Binary Number with Alternating Bits

题目为 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. 解题思路: 题设要求判断形如101010的数字,那么如何在复杂度最小的情况下给出算法呢 首先看一下用python解决本题有哪些基本工具. 说到二进制,首先想到的是位运算符号,python的位运算符号有& ^ ~ | >> &

[LeetCode] Prime Number of Set Bits in Binary Representation

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of 1s present when written in bin

762. Prime Number of Set Bits in Binary Representation 二进制表示形式中的素数位数

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of 1s present when written in bin

191. 求1的位数 Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should retu

LeetCode OJ:Number of 1 Bits(比特1的位数)

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

[LeetCode]Number of 1 Bits

Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the fun

Number of 1 Bits

Description: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the functio

【leetcode】Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu