LeetCode 190(Reverse Bits)

本文介绍了一种高效实现32位无符号整数位反转的方法,通过逐步对2、4、8、16位进行调序来完成整个位反转过程,并提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

Follow up:
If this function is called many times, how would you optimize it?

Analysis

反转一个32位的无符号整数。 例如43261596(0000 0010 1001 0100 0001 1110 1001 1100) 反转之后为964176192 (0011 1001 0111 1000 0010 1101 0100 0000)

采用类似于归并排序的方法,依次对2,4,8,16位进行调序。该例子的过程如下

   0000 0010 1001 0100 0001 1110 1001 1100
-> 0000 0001 0110 1000 0010 1101 0110 1100
-> 0000 0100 1001 0010 1000 0111 1001 0011
-> 0100 0000 0010 1001 0111 1000 0011 1001
-> 0010 1001 0100 0000 0011 1001 0111 1000
-> 0011 1001 0111 1000 0010 1001 0100 0000

对于该过程中用到的反转位数,可用下面方法,举相邻为为个例子。分别取其奇,偶数位,空白位数用0填充。

  0_0_ 0_1_ 1_0_ 0_0_ 0_0_ 1_1_ 1_0_ 1_0_
  _0_0 _0_0 _0_1 _1_0 _0_1 _1_0 _0_1 _1_0
->0000 0010 1000 0000 0000 1010 1000 1000
  0000 0000 0001 0100 0001 0100 0001 0100

代码实现则是讲该数分别对0xAAAA AAAA 和0x5555 5555分别取&操作,然后将取奇数位的右移一位,偶数位的左移一位。再将两个数进行取|操作。

  0000 0010 1000 0000 0000 1010 1000 1000
  0000 0000 0001 0100 0001 0100 0001 0100
->0000 0001 0100 0000 0000 0101 0100 0100
  0000 0000 0010 1000 0010 1000 0010 1000
->0000 0001 0110 1000 0010 1101 0110 1100

同理,对2,4,8,16位分别换位时,可采用对(0xCCCC CCCC, 0x3333 3333), (0xF0F0 F0F0, 0x0F0F0F0F), (0xFF00 FF00, 0x00FF,00FF), (0xFFFF 0000, 0x0000 FFFF)分别取&再分别取奇数的右移,取偶数的左移2,4,8,16 位。

Code

uint32_t reverseBits(uint32_t x) {
    x = ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
    x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
    x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
    x = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
    x = ((x & 0xFFFF0000) >> 16) | ((x & 0x0000FFFF) << 16);
    return x;
}
### LeetCode Top 100 Popular Problems LeetCode provides an extensive collection of algorithmic challenges designed to help developers prepare for technical interviews and enhance their problem-solving skills. The platform categorizes these problems based on popularity, difficulty level, and frequency asked during tech interviews. The following list represents a curated selection of the most frequently practiced 100 problems from LeetCode: #### Array & String Manipulation 1. Two Sum[^2] 2. Add Two Numbers (Linked List)[^2] 3. Longest Substring Without Repeating Characters #### Dynamic Programming 4. Climbing Stairs 5. Coin Change 6. House Robber #### Depth-First Search (DFS) / Breadth-First Search (BFS) 7. Binary Tree Level Order Traversal[^3] 8. Surrounded Regions 9. Number of Islands #### Backtracking 10. Combination Sum 11. Subsets 12. Permutations #### Greedy Algorithms 13. Jump Game 14. Gas Station 15. Task Scheduler #### Sliding Window Technique 16. Minimum Size Subarray Sum 17. Longest Repeating Character Replacement #### Bit Manipulation 18. Single Number[^1] 19. Maximum Product of Word Lengths 20. Reverse Bits This list continues up until reaching approximately 100 items covering various categories including but not limited to Trees, Graphs, Sorting, Searching, Math, Design Patterns, etc.. Each category contains multiple representative questions that cover fundamental concepts as well as advanced techniques required by leading technology companies when conducting software engineering candidate assessments. For those interested in improving logical thinking through gaming activities outside traditional study methods, certain types of video games have been shown beneficial effects similar to engaging directly within competitive coding platforms [^4]. --related questions-- 1. How does participating in online coding competitions benefit personal development? 2. What specific advantages do DFS/BFS algorithms offer compared to other traversal strategies? 3. Can you provide examples illustrating how bit manipulation improves performance efficiency? 4. In what ways might regular participation in programming contests influence job interview success rates? 5. Are there any notable differences between solving problems on paper versus implementing solutions programmatically?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值