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
Leetcode - 693. Binary Number with Alternating Bits
相關推薦
Leetcode - 693. Binary Number with Alternating Bits
integer self 解題思路 基本 acc sub class num different 題目為 Given a positive integer, check whether it has alternating bits: namely, if two adj
LeetCode 693 Binary Number with Alternating Bits 解題報告
alter n) bool dds pytho bits have name clas 題目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacen
leetcode練習:693. Binary Number with Alternating Bits
nat ati blog logs asa turn clas parse bits 693是一道很簡單的題目。 var hasAlternatingBits = function(n) { var bin = []; while(n>0)
693. Binary Number with Alternating Bits - LeetCode
bin AS als 返回 question nat tco ref RR Question 693.?Binary Number with Alternating Bits Solution 思路:輸入一個整數,它的二進制01交替出現,遍歷其二進制字符串,下一個與上一個
[LeetCode&Python] Problem 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: In
【LeetCode】693. Binary Number with Alternating Bits
693. Binary Number with Alternating Bits Problem Example Solution Problem 給一個正整數,判斷他的二進位制形式 是否是0,1交替的 Examp
[LeetCode] 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 O
7 交替的二進位制位 Binary Number with Alternating Bits
題很簡單,一眼就知道是關於二進位制運算的,而且我在第一時間就發現符合條件的整數,與它的左移1位後的結果相加,除了尾數全為1.十進位制考慮的話,即整數n需滿足n + (n<<1) = 2^i-1-i//2。 但是實現上有些問題 首先放上失敗程式碼: class Solution
LeetCode-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 th
LeetCode演算法題-Number of 1 Bits(Java實現)
這是悅樂書的第186次更新,第188篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第45題(順位題號是191)。編寫一個帶無符號整數的函式,並返回它所具有的“1”位數。例如: 輸入:11 輸出:3 說明:整數11具有二進位制表示000000000000000000000
[LeetCode] Prime Number of Set Bits in Binary Representation
pri return not prim 統計 all bits clu leet Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a p
LeetCode-Java-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 pri
LeetCode-Prime Number of Set Bits in Binary Representation
Description: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bit
【LeetCode】762. Prime Number of Set Bits in Binary Representation(C++)
題目: 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 the
【LeetCode】762. Prime Number of Set Bits in Binary Representation
Prime Number of Set Bits in Binary Representation Problem Example Solution 一堆題放一塊太擠了,還是分開放=.=,也能寫得詳細一點 Problem
[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.
LeetCode 762. 762. Prime Number of Set Bits in Binary Representation
題目連結:二進位制表示中質數個計算置位 - 力扣 (LeetCode) Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime numbe
191. Number of 1 Bits(LeetCode)
fun 其他 represent num span take style bin eight Write a function that takes an unsigned integer and returns the number of ’1‘ bits it has
[leetcode-191-Number of 1 Bits]
and 右移 urn returns style this spa mingw -s Write a function that takes an unsigned integer and returns the number of ’1‘ bits it has (als
762. Prime Number of Set Bits in Binary Representation 二進制表示形式中的素數位數
num number uri auto func href order ger xpl Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having