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-))) && (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的更多相关文章
- 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> ...
- Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体旋转)
Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...
- Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体平移)
Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...
- 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 ...
- Data manipulation primitives in R and Python
Data manipulation primitives in R and Python Both R and Python are incredibly good tools to manipula ...
- 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 ...
- 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 ...
- windows phone 之手势识别(Manipulation)
在Windows Phone 7的多触摸屏上可以检测到至少四根同时存在的手指,并且一起操作使触摸屏充分发挥效果. 在silverlight开发中通过事件来实现触屏事件的检测,包括低级别的和高级别的接口 ...
- WPF Multi-Touch 开发:高级触屏操作(Manipulation)
原文 WPF Multi-Touch 开发:高级触屏操作(Manipulation) 在上一篇中我们对基础触控操作有了初步了解,本篇将继续介绍触碰控制的高级操作(Manipulation),在高级操作 ...
随机推荐
- linux 基础 文件操作
cat -A /etc/passwdnl -ba passwd cat -A man_db.conf more man_db.conf less man_db.conf head -n 5 /var/ ...
- Jenkins 有用的API
/quietDown: Put Jenkins in a Quiet mode, in preparation for a restart. In that mode Jenkins don’t st ...
- 【转】一次HBase问题的解决过程(Status: INCONSISTENT)
[From]https://www.cnblogs.com/quchunhui/p/9583746.html ==版本信息== HBase:2.7.1 Storm:1.0.1 RocketMQ:3.4 ...
- Linux中如何批量删除目录下文件后缀
1. rename rename分为perl版本和C版本,以下截图是C版本效果: perl版本:rename 's/.bak//' *.bak 2. for循环+awk 3. for循环+cut 4. ...
- python爬虫播放mp3
我不明白这里出了什么问题.每次我试图在pyglet播放声音,我得到以下错误:WAVEFormatException: AVbin is required to decode compressed me ...
- Android开发实例 Unity显示Toast
Android中的Toast是一种简易的消息提示框. 当视图显示给用户,在应用程序中显示为浮动.和Dialog不一样的是,它永远不会获得焦点,无法被点击.用户将可能是在中间键入别的东西.Toast类的 ...
- android4.2 webkit 中的jni
在android 应用开发中使用WebView,当一个webveiw 被创建时, 也会去load 他所对应的动态库,这里动态库也就是传说中的webkit 内核等. C++ 层与java 层的交互也是通 ...
- Net 文件和流 I/O
文件和流 I/O 2017/03/30 文件和流 I/O(输入/输出)是指在存储媒介中传入或传出数据. 在 .NET Framework 中,System.IO 命名空间包含允许以异步方式和同步方式对 ...
- java中利用hanlp比较两个文本相似度的步骤
使用 HanLP - 汉语言处理包 来处理,他能处理很多事情,如分词.调用分词器.命名实体识别.人名识别.地名识别.词性识别.篇章理解.关键词提取.简繁拼音转换.拼音转换.根据输入智能推荐.自定义分词 ...
- Java文件读写分析
本文内容:IO流操作文件的细节分析:分析各种操作文件的方式. 读写一个文件 从一个示例开始分析,如何操作文件: /** * 向一个文件中写入数据 * @throws IOException */ pr ...