LeetCode 풀기 - 190. Reverse Bits

백지우·2026년 9월 16일

LeetCode

목록 보기
54/76
  • LeetCode 최소 하루에 하나는 풀자는 마인드
  • 사용언어: JAVA

190. Reverse Bits

풀이

  • 방법: result를 한칸씩 왼쪽으로 쉬프트 하면서 n의 마지막 비트들을 더하기
class Solution {
    public int reverseBits(int n) {
        int result = 0;
        for (int i = 0; i < 32; i++) {
            result <<= 1;
            result |= (n & 1);
            n >>>= 1;
        }

        return result;
    }
}
  • 코드 작성하다가 느낀점

0개의 댓글