# Leetcode [No. 190] Reverse Bits (EASY) 解題心得 ## 題目 https://leetcode.com/problems/reverse-bits/description/ ## 思路 + 這個解法滿簡單的,因為我們要保證他是32bit的unsigned int,所以我們要做滿32次。 + 每次都是自己 $*2+n的最後一個bit$ ```c++= class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t x = 0; for (int i = 0 ; i < 32; i++) { x = x << 1; x += n % 2; n = n >> 1; // cout << x << endl; } return x; } }; ``` ### 解法分析 + time complexity: O(1) + space: O(1) ### 執行結果 ![image](https://hackmd.io/_uploads/HkzeA2awT.png) ## follow up: > Follow up: If this function is called many times, how would you optimize it? 不太懂意思 ## Second visit on 2025/05/30 第一次寫的還比這次好,真慚愧==