LeetCode -- Reverse Bits
来源:互联网 发布:怎么当淘宝客服 编辑:程序博客网 时间:2024/06/18 10:15
题目描述
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?
反转一个无符号数字的二进制串。
思路:
将n转化字符串,反转字符串,将字符串转换为无符号数。
实现代码
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?
反转一个无符号数字的二进制串。
思路:
将n转化字符串,反转字符串,将字符串转换为无符号数。
实现代码
public class Solution { public uint reverseBits(uint n) { var s = ToBinaryStr(n); var s1 = ""; for(var i = s.Length - 1; i >= 0; i--){ s1 += s[i]; } var x = BinaryToN(s1); return x; }private string ToBinaryStr(uint n){var s = new uint[32];var counter = 0;while(n > 0){var l = n%2;s[31-counter] = l;n /= 2;counter ++;}return string.Join("",s);}private uint BinaryToN(string s){var n = 0;for(var i = s.Length - 1; i >= 0 ; i--){var x = s[i] == '0' ? 0 : 1;n += x * (int)Math.Pow(2, s.Length - i - 1);}return (uint)n;}}
0 0
- leetCode #190 reverse bits
- 【LeetCode】Reverse Bits
- LeetCode:Reverse Bits
- LeetCode: Reverse Bits
- LeetCode: Reverse Bits
- [LeetCode]Reverse Bits
- leetcode: Reverse Bits
- LeetCode 190 Reverse Bits
- [LeetCode] Reverse Bits
- Leetcode--Reverse Bits
- [leetcode]Reverse Bits
- [LeetCode][190][Reverse Bits]
- #leetcode#Reverse Bits
- LeetCode Reverse Bits
- LeetCode OJ Reverse Bits
- LeetCode(190) Reverse Bits
- [LeetCode 190]Reverse Bits
- LeetCode #190 Reverse Bits
- LeetCode -- Remove Duplicates From Sorted Array 2
- vs 中大括号之间垂直虚线显示
- 创业之中一些重要的事
- return特点
- diff()差分
- LeetCode -- Reverse Bits
- 白手起家学习数据科学 ——Probability之“Bayes's 原理和随机变量篇”(四)
- LeetCode -- Rotate Array
- 线程与进程的概念、使用
- android.mk编写浅析
- 人民币成功“入篮”SDR,这意味着什么?
- LeetCode -- String to Integer (atoi)
- 网页页面实现自动刷新的3种代码
- h.264转flv converter