190 - Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).

Follow up:
If this function is called many times, how would you optimize it?

Related problem: Reverse Integer

Solution 1:

  class Solution {
public:
uint32_t reverseBits(uint32_t n) { //runtime:4ms
unsigned ret = ;
unsigned x = << ;
unsigned y = ;
while(x){
if(x & n)ret |= y; //或ret += y;
x >>= ;
y <<= ;
}
return ret;
}
};

Solution 2:

  class Solution {
public:
uint32_t reverseBits(uint32_t n) { //runtime:4ms
unsigned int bit = ;
unsigned int result = ;
while(bit<)
{
if((n>>bit) & == )
result = result + (<<(-bit));
bit ++;
} return result;
}
};

191 - Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.

Solution:n&(n-1)实现n与n-1的按位与,消除最后一位1

 1 class Solution {
2 public:
3 int hammingWeight(uint32_t n) {
4 int count=0;
5 while(n){
6 n &= (n-1);
7 count++;
8 }
9 return count;
10 }
11 };

【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 【LeetCode】287. Find the Duplicate Number

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...

  3. 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...

  4. 【LeetCode】190. Reverse Bits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...

  5. 【LeetCode】190. Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  6. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  7. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  8. 【LeetCode】476. 数字的补数 Number Complement

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,476, 补数,二进制,Pyth ...

  9. 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)

    7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...

随机推荐

  1. 卷积神经网络和CIFAR-10:Yann LeCun专访 Convolutional Nets and CIFAR-10: An Interview with Yann LeCun

    Recently Kaggle hosted a competition on the CIFAR-10 dataset. The CIFAR-10 dataset consists of 60k 3 ...

  2. 在cmd中登录ftp服务器

    http://jingyan.baidu.com/article/0bc808fc8778ee1bd485b93b.html C:\Users\Administrator>ftpftp> ...

  3. $ is not defined错误类型

    <script src='js/jtouch/jquery.js'></script> <script type="text/javascript"& ...

  4. 在tomcat目录下启动tomcat,可以正常访问tomcat主页,然在在eclipse中集成了tomcat却访问不了tomcat主页,却能访问发布的项目

    tomcat server在eclipse中正常配置了,在eclipse建tomcat服务是在server 视图那里new server建立的,但把项目部署到tomcat后却发现tomcat主页报40 ...

  5. OpenStack介绍

    简介 OpenStack是一个开源的云计算管理平台项目,由几个主要的组件组合起来完成具体工作.OpenStack支持几乎所有类型的云环境,项目目标是提供实施简单.可大规模扩展.丰富.标准统一的云计算管 ...

  6. android 使用代码实现 RelativeLayout布局

    只需把代码copy进入oncreate方法即可运行 RelativeLayout rl = new RelativeLayout(this); Button btn1 = new Button(thi ...

  7. Python3 学习第八弹: 模块学习一之模块变量

    __name__变量 用于判断该python文件是否作为主程序运行.若该文件为导入,__name__值为其文件名,若为主程序,则其值为__main__ 这也就是为什么经常看到有一些python文件中有 ...

  8. Centos 6.5LAMP服务器(Apache+PHP+MySQL)的搭建

    1.首先看下你的防火墙是否处于开启状态,如果是开启状态,按照如下方法来配置你的防火墙(如果你在安装虚拟机时就没有开启过防火墙,那么这一步就省略了): 1.配置防火墙,开启80端口.3306端口 vi ...

  9. bzoj4199

    看到这题我就伤心,当初想到了正解却因为各种sb原因没有写…… 好吧,其实我的正解是比较挫的…… 大家似乎都用了后缀数组,我用了后缀自动机(后缀树) 其实SAM是很好想得,用SAM建出后缀树后 我们考虑 ...

  10. 02.C语言关于指针的学习笔记

    指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址. 要搞清一个指针需要搞清指针的四方面的内容: 指针的类型,指针所指向的 类型,指针的值或者叫指针所指向的内存区,还有指针本身所占据的内 ...