`
songlj
  • 浏览: 15875 次
社区版块
存档分类
最新评论

Reverse Bits

 
阅读更多


解题思路:该题与上题类似,如果是负数首先转化成正数(+ 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/


版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

    cpp-算法精粹

    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...

    LeetCode最全代码

    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]...

    lrucacheleetcode-LeetCode:复制和思考

    Reverse Bits 二进制位数 二进制的&和&gt;&gt; &lt;&lt; &gt;&gt;== 20200525 172. Factorial Trailing Zeroes 数学分析 递归和循环 20200525 191. Number of 1 Bits 二进制 二进制如何记录1的个数 20200526 287. Find the ...

    Es_BitsReverse_moviconbits_

    Worging with bit variables on movicon

    Coding Interview In Java

    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 ...

    ReverseBits:反转 64 位整数的位

    反向位 反转 64 位整数的位。

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    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 Disassembler v3.5.9.1

    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!

    cLanguageFuntions:一些C函数和程序的源代码-C language program source code

    3. reverse_bits.c 占用一个字节的函数,一点一点地反转它。 237 =&gt; 1110 1101 after reverse 1101 1110 =&gt; 222 4. sort_int_tab.c 函数按升序对int数组进行排序(就地),该数组包含正好是'size'元素。 用于始终...

    RSATool2.exe

    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 ...

    LOLBITS:使用后台智能传输服务(BITS)作为通信协议和Direct Syscalls + Dinvoke进行EDR用户模式挂钩规避的C2框架

    ██╗ ██████╗ ██╗ ██████╗ ██╗████████╗███████╗ ██║ ██╔═══██╗██║ ██╔══██╗██║╚══██╔══╝██╔════╝ ██║ ██║ ██...

    examshell02:考核中间体

    examshell02 此考试外壳适用于希望为期末考试进行培训的人,练习将在适当的时候添加。 在此考试中,您将需要知道如何使用以下工具: ft_putstr ft_strlen 生成文件 ... reverse_bits ft_rrange

    Company-Wise-Coding-Questions

    ===========公司明智的编码问题============= ...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算法蛇形走位-leetcode_way:leetcode解决方案的降价

    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

    颜色分类leetcode-leetcode.etc:OJ、leetcode等解决方案

    Bits(二进制中有多少个1) 、 / Easy Repeated DNA Sequences Medium Single Number(落单的数) 、 / Medium Single Number II(落单的数 II) 、 Medium Single Number III(落单的数 III) Medium Hash Function(哈希...

    android fisrtdemo

    + 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源代码

    Bitset& reverse(); //返回左边n位,构成新的Bitset Bitset left(size_t n) const; //返回右边n位,构成新的Bitset Bitset right(size_t n) const; //判断b包含的位数组是否是本类的位数组的自串,如果是...

    crc32:CRC32工具

    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 ...

    rellic:Rellic从LLVM位代码产生goto-free C输出

    Trail of Bits托管可下载的cxx-common的预构建版本,这使得使用Rellic进行安装和运行变得相当容易。 尽管如此,下表还是Rellic的大多数依赖项。 名称版最新3.14+最新最新4.0+4.0+ 4.7.1+预制Docker映像预先构建的...

    STG (SNMP Traffic Grapher)

    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 ...

Global site tag (gtag.js) - Google Analytics