LEETCODE 190
来源:互联网 发布:优化英文名 编辑:程序博客网 时间:2024/06/12 01:01
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).
思路:
本来求二进制的方法需要将得到的二进制代码从后往前排序,相当于这里需要的倒序,每求一位二进制,相当于除以二,倒序相当于乘以二。
当一个二进制位1010101111时,倒序为1111010101,这里要求32位,在后面添加0,相当于乘以2.
class Solution {public: uint32_t reverseBits(uint32_t n) { uint32_t sum = 0; int count = 0; while(n) { sum = sum * 2 + n%2; n = n/2; ++count; } for(int i = count; i < 32; ++i) sum = sum *2; return sum; }};
0 0
- LEETCODE 190
- leetCode #190 reverse bits
- LeetCode 190 Reverse Bits
- [LeetCode][190][Reverse Bits]
- LeetCode(190) Reverse Bits
- [LeetCode 190]Reverse Bits
- LeetCode #190 Reverse Bits
- [leetcode 190]Reverse Bits
- LeetCode 190 Reverse Bits
- leetCode 190-Reverse Bits
- leetcode-190 Reverse Bits
- LeetCode 190 Reverse Bits
- leetcode 190 Reverse Bits
- LeetCode 190 :Reverse Bits
- 【leetcode 190 Reverse Bits 】
- leetcode 190:Reverse Bits
- Reverse Bits (leetcode 190)
- leetcode[190]:Reverse Bits
- 会议纪要二:自主去优化你的代码,优化你的逻辑
- 小明的存钱计划
- Sicily 1013 Going Home
- Android-Frame Animation介绍
- POJ 3186
- LEETCODE 190
- Angular2 进阶
- 谁获得了最高奖学金
- JSP学习笔记
- Android-XML转成View过程简述
- BZOJ3781 小B的询问 题解&代码 【附莫队总结】
- python 异常
- codeforce 344 div2 前三题
- 侯捷对进入IT行业的年轻人的建议