首页 > 其他 > 详细

Leetcode Reverse Bits

时间:2015-04-11 17:45:54      阅读:212      评论:0      收藏:0      [点我收藏+]

题目地址:https://leetcode.com/problems/reverse-bits/

题目分析:可以4bit为单位,0翻转对应0,1翻转对应8.....15翻转对应15,将这些翻转信息保存在数组中即可以O(1)的空间复杂度换来很好的时间复杂度

题目解答:

public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        int[] tb = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
        int ret = 0;
        int msk = 15;
        for(int i = 0;i<8;i++){
            int curr = n&msk;
            ret = ret<<4;
            ret |= tb[curr];
            n = n>>>4;
        }
        return ret;
    }
}

 

Leetcode Reverse Bits

原文:http://www.cnblogs.com/xiongyuesen/p/4417990.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!