Skip to content

Latest commit

 

History

History
89 lines (69 loc) · 2.09 KB

_3064. Guess the Number Using Bitwise Questions I.md

File metadata and controls

89 lines (69 loc) · 2.09 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 06, 2024

Last updated : July 04, 2024


Related Topics : Bit Manipulation, Interactive

Acceptance Rate : 90.95 %


As is the nature of someone using python, one-liner = funny haha


Solutions

C

/** 
 * Definition of commonSetBits API.
 * int commonSetBits(int num);
 */

int findNumber(){
	int x = 1; // I'm assuming that this iteration of C is 32-bit ints
    int output = 0;

    for (int i = 0; i < 30; i++) {
        if (commonSetBits(x)) {
            output += x;
        }
        x = x << 2;
    }
    return output;
}

Python

# Definition of commonSetBits API.
# def commonSetBits(num: int) -> int:

class Solution:
    def findNumber(self) -> int:
        output = 0
        current = 1
        for i in range(31) :
            if commonSetBits(current) :
                output += current
            current *= 2
        return output
# Definition of commonSetBits API.
# def commonSetBits(num: int) -> int:

class Solution:
    def findNumber(self) -> int:
        return sum([2 << x for x in range(0, 31) if commonSetBits(2 << x)]) + commonSetBits(1)
# Definition of commonSetBits API.
# def commonSetBits(num: int) -> int:

class Solution:
    def findNumber(self) -> int:
        return sum([2 ** x for x in range(0, 31) if commonSetBits(2 ** x)])