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的位运算符号有& ^ ~ | >> <<这六种。

先看移位,易发现如果将101010右移一位,则有10101,两者相加为111111;当111111进1则与其本身的与运算就得到0

尝试编写解题代码如下:

class Solution:
    def hasAlternatingBits(self, n):
        return ((n + (n >> 1) ) & (n + (n >> 1)+1)) == 0

Submission Result: Accepted

原文地址:https://www.cnblogs.com/kaezah/p/9844834.html

时间: 2024-10-22 15:59:04

Leetcode - 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

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 Ex

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 191:number of one 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 shoul

【LeetCode】191 - 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 762. Prime Number of Set Bits in Binary Representation

思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). 1 class Solution { 2 public int countPrimeSetBits(int L, int R) { 3 Set<Integer> prime = new HashSet<Integer>(Arrays.asList(2,3,5,7,11,13,17,19,23)); 4 int[] count = new int[1+R]; 5 int res = 0; 6 for

LeetCode算法题-Number of 1 Bits(Java实现)

这是悦乐书的第186次更新,第188篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第45题(顺位题号是191).编写一个带无符号整数的函数,并返回它所具有的"1"位数.例如: 输入:11 输出:3 说明:整数11具有二进制表示00000000000000000000000000001011 输入:128 输出:1 说明:整数128具有二进制表示00000000000000000000000010000000 本次解题使用的开发工具是eclipse,jdk使

LeetCode题解之Number of 1 Bits

1.题目描述 2.问题分析 使用C++ 标准库的 bitset 类,将整数转换为 二进制,然后将二进制表示转换为字符串,统计字符串中 1 的个数即可. 3.代码 1 int hammingWeight(uint32_t n) { 2 bitset<32> b(n); 3 string b_s = b.to_string() ; 4 5 int count_one = 0; 6 for(string::iterator it = b_s.begin(); it != b_s.end() ; ++

[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