본문 바로가기
Problem Solving/LeetCode

[LeetCode] 762. Prime Number of Set Bits in Binary Representation - Java

by graycode 2026. 10. 11.

• 문제 링크

 

Prime Number of Set Bits in Binary Representation - LeetCode

Can you solve this real interview question? Prime Number of Set Bits in Binary Representation - Given two integers left and right, return the count of numbers in the inclusive range [left, right] having a prime number of set bits in their binary representa

leetcode.com

 

• 풀이 코드

public class Solution {

    public int countPrimeSetBits(int left, int right) {
        int cnt = 0, mask = 665772;
        for (int i = left; i <= right; i++) if ((mask & (1 << Integer.bitCount(i))) != 0) cnt++;

        return cnt;
    }

}

댓글