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),在高级操作 ...
随机推荐
- golang(08)接口介绍
原文链接 http://www.limerence2017.com/2019/09/12/golang13/#more 接口简介 golang 中接口是常用的数据结构,接口可以实现like的功能.什么 ...
- 【JVM学习笔记】类加载器
概述 类加载器用来把类加载到Java虚拟机中.从JDK1.2版本开始,类的加载过程采用父委托机制,这种机制能更好地保证Java平台的安全.在此委托机制中,除了Java虚拟机自带的根类加载器以外,其余的 ...
- Ajax操作的四个步骤
Ajax操作的四个步骤: 创建Ajax对象 连接服务器 发送请求 接收返回信息 <!DOCTYPE html> <html> <head lang="en&qu ...
- python基础知识(元组)
元组 不能更改内容 元组 (元素1,元素2) 元组的创建和删除 使用赋值运算符直接创建元组 元组名 = (元素1,元素2........) 只创建一个元素的元组 元组名 = (元素1,) 创建空 ...
- 技术简历写这么写,才能得到BAT面试官们的青睐
公众号[程序员江湖] 作者陆小凤,985 软件硕士,阿里 Java 研发工程师,在技术校园招聘.自学编程.计算机考研等方面有丰富经验和独到见解,目前致力于分享程序员干货和学习经验,同时热衷于分享作为程 ...
- 【JulyEdu-Python基础】第 2 课:关键字和循环控制
大纲 变量和类型常见字符串处理条件判断循环控制函数 变量和类型 基本变量类型 对于python所有对象都是继承自object的类 容器: 整数 print(type(1234)) <class ...
- Java中流的操作练习
文件中的学生信息 学生信息存储在TXT文件中,我们需要对信息进行基本的,增.删.改.查,全部显示操作. 1.学生类/Student package com.yujiao.student; public ...
- sqlalchemy一对一关系映射
#encoding: utf-8 from sqlalchemy import create_engine,Column,Integer,String,Float,func,and_,or_,Text ...
- 2019牛客暑期多校训练营(第一场)-A (单调栈)
题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...
- 【Linux 网络编程】常用TCP/IP网络编程函数
(1)函数socket /**************************************************************** ** 功能:创建一个套接字用于通信 ** 参 ...