LeetCode-190. Reverse Bits

网友投稿 618 2022-10-03

LeetCode-190. Reverse Bits

LeetCode-190. Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

Example:

Input: 43261596Output: 964176192Explanation: 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?

题解:

注意32位即可

class Solution {public: string getBin (uint32_t n) { string res; while (n > 0) { res += to_string(n % 2); n /= 2; } while (res.size() != 32) { res += '0'; } return res; } uint32_t toDec (string s) { reverse(s.begin(), s.end()); uint32_t a = 0; uint32_t res = 0; for (int i = 0; i < s.length(); i++) { res += pow(2, a) * (s[i] - 48); a++; } return res; } uint32_t reverseBits(uint32_t n) { return toDec(getBin(n)); }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序不支持table标签怎么办(小程序 table表格)
下一篇:orange pi pc plus香橙派使用体验
相关文章

 发表评论

暂时没有评论,来抢沙发吧~