解题思路:该题与上题类似,如果是负数首先转化成正数(+ 2147483647+1),再处理,然后变成2进制数(如果是负数最高位是1),翻转2进制数,变成十进制数,翻转后如果最后位是1,在十进制数基础上加上2147483647+1。
Java代码实现:
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
String str="";
if(n==0) return 0;
if(n==-2147483648) return 1;
System.out.println(n);
int m=n;
if(n<0) {
n+=2147483647+1;
//str+="1";
}System.out.println(n);
while(n!=0){
int mod=n%2;
if(mod==1) str="1"+str;
else str="0"+str;
n=n/2;
}
String revString="";
for(int j=str.length();j<32;j++){
str="0"+str;
}
if(m<0) str="1"+str.substring(1,str.length());
for(int i=0;i<str.length();i++){
revString=str.charAt(i)+revString;
}
int num=0;
for(int i=revString.length()-1;i>0;i--){
num+=(Integer.valueOf(revString.charAt(i))-48)*(int)Math.pow(2,revString.length()-1-i);
}
if(revString.charAt(0)=='1') {
return 2147483647+1+num;
}
return num;
}
}
原题题目:https://leetcode.com/problems/reverse-bits/
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
Reverse Bits Repeated DNA Sequences Number of 1 Bits Gray Code Single Number Single Number II Single Number III Power of Two Missing Number Maximum Product of Word Lengths Bitwise AND of Numbers Range...
190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C++](./C++/reverse-bits.cpp) [Python](./Python/reverse-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 191 |[Number of 1 Bits]...
Reverse Bits 二进制位数 二进制的&和>> << >>== 20200525 172. Factorial Trailing Zeroes 数学分析 递归和循环 20200525 191. Number of 1 Bits 二进制 二进制如何记录1的个数 20200526 287. Find the ...
Worging with bit variables on movicon
231 Counting Bits 561 232 Maximum Product of Word Lengths 563 233 Gray Code 565 234 Permutations 567 235 Permutations II 571 236 Permutation Sequence 573 237 Generate Parentheses 575 238 Combination ...
反向位 反转 64 位整数的位。
Reverse the order of bits in an unsigned integer Chapter 20. Convert an integer to a string and a string to an integer Chapter 21. Convert a number from base b1 to base b2 Chapter 22. Given a set S, ...
Hopper is a reverse engineering tool for OS X and Linux, that lets you disassemble, decompile and debug your 32/64bits Intel Mac, Linux, Windows and iOS executables!
3. reverse_bits.c 占用一个字节的函数,一点一点地反转它。 237 => 1110 1101 after reverse 1101 1110 => 222 4. sort_int_tab.c 函数按升序对int数组进行排序(就地),该数组包含正好是'size'元素。 用于始终...
If you don't trust this tool, don't use it or even better, reverse engineer the keygeneration and tell me about your worries ;-) - The bignumber library used in this program is MIRACL 4.45 (c) by ...
██╗ ██████╗ ██╗ ██████╗ ██╗████████╗███████╗ ██║ ██╔═══██╗██║ ██╔══██╗██║╚══██╔══╝██╔════╝ ██║ ██║ ██...
examshell02 此考试外壳适用于希望为期末考试进行培训的人,练习将在适当的时候添加。 在此考试中,您将需要知道如何使用以下工具: ft_putstr ft_strlen 生成文件 ... reverse_bits ft_rrange
===========公司明智的编码问题============= ...9. Prime Number of Set Bits 10. Reverse Each Word in String 11. Find k-th character in string 12. Star Elements 13. Common Subsequence 14. Choco
LeetCode算法蛇形走位 This is a record of my exploring leetcode Overview Problem Solution no need to solve math BF findKth most common way std::reverse Finite State Machine brute ...bits
Bits(二进制中有多少个1) 、 / Easy Repeated DNA Sequences Medium Single Number(落单的数) 、 / Medium Single Number II(落单的数 II) 、 Medium Single Number III(落单的数 III) Medium Hash Function(哈希...
+ remote_tables.tv_remote_config_table.KEY_REVERSE + " blob, " + remote_tables.tv_remote_config_table.KEY_PIP + " blob);"; 2. insert one group of data into the table; 3. implement the UI, please ...
Bitset& reverse(); //返回左边n位,构成新的Bitset Bitset left(size_t n) const; //返回右边n位,构成新的Bitset Bitset right(size_t n) const; //判断b包含的位数组是否是本类的位数组的自串,如果是...
Reverse, undo, and calculate CRC32 checksums positional arguments: action flip flip the bits to convert normal(msbit-first) polynomials to reversed (lsbit-first) and vice versa reciprocal find ...
Trail of Bits托管可下载的cxx-common的预构建版本,这使得使用Rellic进行安装和运行变得相当容易。 尽管如此,下表还是Rellic的大多数依赖项。 名称版最新3.14+最新最新4.0+4.0+ 4.7.1+预制Docker映像预先构建的...
Reverse option - changes direction of graph. Show traffic in Bytes / Bits - show traffic values in bits or bytes Fix Rate - Show graph data only up to "Max Rate" and don't allow automatic ...