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-))) && (n>))
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 = ;
while(n != )
{
if(n%)
{count++;}
n = n>>;
}
return count;
}
};
//网上大神的解法
int CountOne(unsigned long n)
{
//0xAAAAAAAA,0x55555555分别是以“1位”为单位提取奇偶位
n = ((n & 0xAAAAAAAA) >> ) + (n & 0x55555555);
//0xCCCCCCCC,0x33333333分别是以“2位”为单位提取奇偶位
n = ((n & 0xCCCCCCCC) >> ) + (n & 0x33333333);
//0xF0F0F0F0,0x0F0F0F0F分别是以“4位”为单位提取奇偶位
n = ((n & 0xF0F0F0F0) >> ) + (n & 0x0F0F0F0F);
//0xFF00FF00,0x00FF00FF分别是以“8位”为单位提取奇偶位
n = ((n & 0xFF00FF00) >> ) + (n & 0x00FF00FF);
//0xFFFF0000,0x0000FFFF分别是以“16位”为单位提取奇偶位
n = ((n & 0xFFFF0000) >> ) + (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).

 //我的解法
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t temp[];
uint32_t n1 = ;
for(int i = ;i<;i++)
{
temp[i] = n % ;
n = n >> ;
}
for(int i = ;i<;i++)
{
n1 += (temp[i]<<(-i));
}
return n1;
}
};
 //网上大神的写法
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t value = ;
uint32_t mask = ;
for (uint32_t i = ; i < ; ++i) {
value = (value<< )|((n&mask)>>i);
mask <<=;
}
return value;
}
};

参考: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.

 class Solution {
public:
int singleNumber(vector<int>& nums) {
int result = ;
for(int i = ;i < nums.size();i++)
{
result = result ^ nums[i];
}
return result;
}
};

137. Single Number two

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

 class Solution {
public:
int singleNumber(vector<int>& nums) {
vector<int> flag();
int result = ;
for(int k = ;k < ;k++)
{
for(int i = ;i < nums.size();i++)
{
flag[k] += ((nums[i]>>k) & );
}
}
for(int k = ;k < ;k++)
{
result += ((flag[k] % )<<k);
}
return result;
}
};

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

 class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
vector<int> result();
int temp = ;
for(int i = ;i < nums.size();i++)
{
temp ^= nums[i];
}
int flag = ;
while(!(temp & ))
{
temp >>= ;
flag++;
}
for(int i = ;i < nums.size();i++)
{
int tmp = nums[i]>>flag;
if(tmp & )
result[] ^= nums[i];
else
result[] ^= nums[i];
}
return result;
}
};

LeetCode_Bit Manipulation的更多相关文章

  1. backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

    昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx> ...

  2. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体旋转)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  3. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体平移)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  4. Track files and folders manipulation in Windows

    The scenario is about Business Secret and our client do worry about data leakage. They want to know ...

  5. Data manipulation primitives in R and Python

    Data manipulation primitives in R and Python Both R and Python are incredibly good tools to manipula ...

  6. VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...

  7. Bash String Manipulation Examples – Length, Substring, Find and Replace--reference

    In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable wi ...

  8. windows phone 之手势识别(Manipulation)

    在Windows Phone 7的多触摸屏上可以检测到至少四根同时存在的手指,并且一起操作使触摸屏充分发挥效果. 在silverlight开发中通过事件来实现触屏事件的检测,包括低级别的和高级别的接口 ...

  9. WPF Multi-Touch 开发:高级触屏操作(Manipulation)

    原文 WPF Multi-Touch 开发:高级触屏操作(Manipulation) 在上一篇中我们对基础触控操作有了初步了解,本篇将继续介绍触碰控制的高级操作(Manipulation),在高级操作 ...

随机推荐

  1. H5本地存储技术

    H5 Web存储技术 前言 web存储技术在初期的时候被定义为HTML5的一部分作为其API.后来被独立出来作为一份独立的标准. web存储标准包含localStorage对象和sessionStor ...

  2. 继承以及Super

    一个小小的总结,主要关注以下三个问题:ES5的继承方式,ES5的继承与ES6的继承的区别,ES6的super的几种使用方式以及其中this的指向. From http://supermaryy.com ...

  3. 【Qt开发】【计算机视觉】OpenCV在Qt-MinGw下的编译库

    最近电脑重装系统了,第一件事重装OpenCV.这次直接装最新版,2014-4-25日发布的OpenCV2.4.9版本,下载链接: http://sourceforge.NET/projects/ope ...

  4. WTForms常用的验证器

    from wtforms import Form,StringField,IntegerField from wtforms import validators from wtforms.valida ...

  5. 吉首大学2019年程序设计竞赛(重现赛)-J(树形DP)

    题目链接:https://ac.nowcoder.com/acm/contest/992/J 题意:题意很清晰,就是求任意两点距离的和,结果对1e9+7取模. 思路:裸的树形DP题,一条边的贡献值=这 ...

  6. (转载)Nim博弈论

    最近补上次参加2019西安邀请赛的题,其中的E题出现了Nim博弈论,今天打算好好看看Nim博弈论,在网上看到这篇总结得超级好的博客,就转载了过来. 转载:https://www.cnblogs.com ...

  7. 解决远程连不到CentOS7虚拟机或ifconfig中没有ens33

    在使用Secure CRT连接虚拟机连接不上,可能之前虚拟机关闭不当 登到虚拟机的中断使用ifconfig发现没有ens33 猜测是CentOS图形管理中的NetworkManager接管了网络配置, ...

  8. C++序列容器之 vector常见用法总结

    一.关于vector 本文默认读者具有一定的c++基础,故大致叙述,但保证代码正确. vector是一个动态的序列容器,相当于一个size可变的数组. 相比于数组,vector会消耗更多的内存以有效的 ...

  9. JavaScript和JSON转化

    1, JSON转JavaScript JSON.parse():https://www.runoob.com/json/json-parse.html 2, JavaScript转JSON JSON. ...

  10. Python_4day

    函数 函数可以用来定义可重复代码,组织和简化 一般来说一个函数在实际开发中为一个小功能 一个类为一个大功能 同样函数的长度不要超过一屏   Python中的所有函数实际上都是有返回值(return N ...