zoukankan      html  css  js  c++  java
  • LeetCode_Bit Manipulation

    231. Power of Two

       Given an integer, write a function to determine if it is a power of two.

       

    class Solution {
    public:
        bool isPowerOfTwo(int n) {
            if((!(n&(n-1))) && (n>0)) 
                return true;
            else
                return false;
        }
    };

    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.

    //我的解法
    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            int count = 0;
            while(n != 0)
            {
                if(n%2)
                {count++;}
                n = n>>1;
            }
            return count;
        }
    };
    //网上大神的解法
    int CountOne(unsigned long n)
    {
        //0xAAAAAAAA,0x55555555分别是以“1位”为单位提取奇偶位
        n = ((n & 0xAAAAAAAA) >> 1) + (n & 0x55555555);
        //0xCCCCCCCC,0x33333333分别是以“2位”为单位提取奇偶位
        n = ((n & 0xCCCCCCCC) >> 2) + (n & 0x33333333);
        //0xF0F0F0F0,0x0F0F0F0F分别是以“4位”为单位提取奇偶位
        n = ((n & 0xF0F0F0F0) >> 4) + (n & 0x0F0F0F0F);
        //0xFF00FF00,0x00FF00FF分别是以“8位”为单位提取奇偶位
        n = ((n & 0xFF00FF00) >> 8) + (n & 0x00FF00FF);
        //0xFFFF0000,0x0000FFFF分别是以“16位”为单位提取奇偶位
        n = ((n & 0xFFFF0000) >> 16) + (n & 0x0000FFFF);
    
        return n;
    }

    参考: http://blog.csdn.net/yunyu5120/article/details/6692072

    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        as  00111001011110000010100101000000).

      

     1 //我的解法
     2 class Solution {
     3 public:
     4     uint32_t reverseBits(uint32_t n) {
     5         uint32_t temp[32];
     6         uint32_t n1 = 0;
     7         for(int i = 0;i<32;i++)
     8         {
     9             temp[i] = n % 2;
    10             n = n >> 1;
    11         }
    12         for(int i = 0;i<32;i++)
    13         {
    14             n1 += (temp[i]<<(31-i));
    15         }
    16         return n1;
    17     }
    18 };
     1 //网上大神的写法
     2 class Solution {
     3 public:
     4     uint32_t reverseBits(uint32_t n) {
     5         uint32_t value = 0;
     6         uint32_t mask = 1;
     7         for (uint32_t i = 0; i < 32; ++i) {
     8             value = (value<<1 )|((n&mask)>>i);
     9             mask <<=1;
    10         }
    11         return value;
    12     }
    13 };

    参考:http://blog.csdn.net/feliciafay/article/details/44536827

    136. Single Number

    Given an array of integers, every element appears twice except for one. Find that single one.

     1 class Solution {
     2 public:
     3     int singleNumber(vector<int>& nums) {
     4         int result = 0;
     5         for(int i = 0;i < nums.size();i++)
     6         {
     7             result = result ^ nums[i];
     8         }
     9         return result;
    10     }
    11 };

    137. Single Number two

    Given an array of integers, every element appears three times except for one. Find that single one.

     1 class Solution {
     2 public:
     3     int singleNumber(vector<int>& nums) {
     4         vector<int> flag(32);
     5         int result = 0;
     6         for(int k = 0;k < 32;k++)
     7         {
     8             for(int i = 0;i < nums.size();i++)
     9             {
    10                 flag[k] += ((nums[i]>>k) & 1);
    11             }
    12         }
    13         for(int k = 0;k < 32;k++)
    14         {
    15             result += ((flag[k] % 3)<<k);
    16         }
    17         return result;
    18     }
    19 };

    260. Single Number three

    Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

    For example:

    Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

     1 class Solution {
     2 public:
     3     vector<int> singleNumber(vector<int>& nums) {
     4         vector<int> result(2);
     5         int temp = 0;
     6         for(int i = 0;i < nums.size();i++)
     7         {
     8             temp ^= nums[i];            
     9         }
    10         int flag = 0;
    11         while(!(temp & 1))
    12         {
    13             temp >>= 1;
    14             flag++;
    15         }
    16         for(int i = 0;i < nums.size();i++)
    17         {
    18             int tmp = nums[i]>>flag;
    19             if(tmp & 1)
    20                 result[0] ^= nums[i];
    21             else
    22                 result[1] ^= nums[i];
    23         }
    24         return result;
    25     }
    26 };
  • 相关阅读:
    命令行推送文章到博客园
    链表的新写法
    关于vim无法复制到剪切板的问题
    Ethernet & ARP Protocol
    [从今天开始修炼数据结构]串、KMP模式匹配算法
    [从今天开始修炼数据结构]队列、循环队列、PriorityQueue的原理及实现
    [从今天开始修炼数据结构]栈、斐波那契数列、逆波兰四则运算的实现
    [从今天开始修炼数据结构]线性表及其实现以及实现有Itertor的ArrayList和LinkedList
    IntelliJ IDEA快捷键大全
    [从今天开始修炼数据结构]基本概念
  • 原文地址:https://www.cnblogs.com/niuxichuan/p/5313305.html
Copyright © 2011-2022 走看看