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. Very important notes about Spring @Transnational(Srping事务注解 @Transnational重要注意事项)

    Sprint @Transnational is being ignored in the following cases: 1. when the caller method is calling ...

  2. GCC 9.2 2019年8月12日 出炉啦

    GNU 2019-08-12 发布了 GCC 9.2https://gcc.gnu.org/onlinedocs/9.2.0/ 有详细的说明 MinGW 上可用的 GCC 9.2 版本下载地址 [ m ...

  3. 如何让JavaScript元素运动起来 ?

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. java:solr

    1.solr(数据导入solr自带数据库):   ImportItemController.java: package com.solr.controller; import org.springfr ...

  5. java:面向对象(多态,final,抽象方法,(简单工厂模式即静态方法模式),接口)

    * 生活中的多态:同一种物质,因环境不同而表现不同的形态. * 程序中多态:同一个"接口",因不同的实现而执行不同的操作. * 多态和方法的重写经常结合使用,子类重写父类的方法,将 ...

  6. 了解XPath与XPath轴

    XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航. 节点(Node) 在 XPath 中,有七种类型的节点:元素.属性.文本.命名空间.处理 ...

  7. 组件推荐Forloop.HtmlHelpers 用来实现MVC的js加载顺序

    最近在开发的时候遇到js加载顺序的问题,layui在底部声明了js,但是我想在页面其他地方使用分布视图,分布视图内有自己的js逻辑,发现不能执行,一看就发现,这里的js应该加在layui后面执行才能有 ...

  8. 【Git】工作中99%能用到的git命令

    Git使用笔记 1.第一次使用github ============================================= 1)github注册账号 使用邮箱注册账号 先不要创建版本库 2 ...

  9. PJzhang:今天才搞清身份证、银行卡……的编码规则

    猫宁!!! ​​   之前思考过常见证件的编码规则,抽空查了一下,发现挺有意思.   一般查询证件或者手机号归属地都是直接百度小工具,但是背后的查询机制如何,可能大多人不甚了解.   介绍几种生活中最 ...

  10. 介绍 5 个实用的 Ajax 库

    参考链接:https://cuiqingcai.com/6806.html?utm_medium=hao.caibaojian.com&utm_source=hao.caibaojian.co ...