
位运算
张荣华_csdn
这个作者很懒,什么都没留下…
展开
-
寻找奇数
有一个整型数组A,其中只有一个数出现了奇数次,其他的数都出现了偶数次,请打印这个数。要求时间复杂度为O(N),额外空间复杂度为O(1)。给定整形数组A及它的大小n,请返回题目所求数字。class OddAppearance {public: int findOdd(vector<int> A, int n) { // write code here i...原创 2018-06-15 08:48:24 · 355 阅读 · 0 评论 -
Single Number II
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:Your algorithm should have a linear runtime complexity. Cou...原创 2018-06-28 17:52:13 · 333 阅读 · 0 评论 -
Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.Example 1:Input: [5,7]Output: 4Example 2:Input: [0,1]Output: 0从题目中给的例子来...原创 2018-07-13 00:10:49 · 213 阅读 · 0 评论 -
全组合
假设原有元素 n 个,则最终组合结果是2^n-1个。用位操作方法:假设元素原本有:a,b,c 三个,则 1 表示取该元素,0 表示不取。故取a则是001,取ab则是011,取abc是111;这些结果的位图值都是 1,2…2^n-1。所以从值 1 到值2^n-1 依次输出结果: 001,010,011,100,101,110,111 。对应输出组合结果为:a,b,ab,c,ac,bc,a...原创 2018-07-15 00:55:50 · 326 阅读 · 0 评论 -
数中唯一只出现一次的数字
题目:在一个数组中除一个数字只出现一次之外,其他数字都出现了三次。请找出那个只出现一次的数字。如果一个数字出现三次,那么它的二进制表示的每一位也出现三次。如果把所有出现三次的数字的二进制表示的每一位都分别加起来,那么每一位的和都能被3整除。 我们把数组中所有数字的二进制表示的每一位都加起来。如果某一位的和能被3整除,那么那个只出现一次的数字二进制表示中对应的那一位为0;否则为1.int F...原创 2018-07-22 00:13:42 · 316 阅读 · 0 评论 -
数的分组
题目:有10个任意的正整数,将其分为两组A和B,要求组A中每个数据的和与组B中每个数据的和之差的绝对值最小。#include "stdio.h"#include "math.h"void main(){ unsigned int i,j,k=0,difference=0,r; unsigned int sum_A=0, sum_B=0; int a[10]; ...原创 2018-07-27 00:02:04 · 363 阅读 · 0 评论 -
Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB...原创 2018-07-09 00:11:28 · 248 阅读 · 0 评论 -
不用加减乘除做加法
题目:写一个函数,求两个整数之和,要求在函数体内不得使用加减乘除四则运算符号。思路:将两个整数的加法使用二进制来求和,例如5,17,5的二进制为101,17的二进制为10001,101+10001=10110。 再通过位运算来代替二进制的加法。 第一步不考虑进位对每一位相加,这和异或的结果是一样的。 接着考虑第二步进位,可以理解为两个数先做位与运算,然后再左移一位。 第三步把两个步...原创 2018-07-28 00:07:28 · 180 阅读 · 0 评论 -
整数与IP地址间的转换
原理:ip地址的每段可以看成是一个0-255的整数,把每段拆分成一个二进制形式组合起来,然后把这个二进制数转变成一个长整数。举例:一个ip地址为10.0.3.193每段数字 相对应的二进制数10 000010100 000000003 00000011...原创 2018-08-08 10:26:28 · 440 阅读 · 0 评论 -
Power of Two
Given an integer, write a function to determine if it is a power of two.Example 1:Input: 1Output: true Explanation: 20 = 1Example 2:Input: 16Output: trueExplanation: 24 = 16Example 3:...原创 2018-08-06 12:32:08 · 140 阅读 · 0 评论 -
求最大连续bit数
题目描述功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1 输入: 一个byte型的数字 输出: 无 返回: 对应的二进制数字中1的最大连续数输入描述:输入一个byte数字输出描述:输出转成二进制之后连续1的个数#include<iostream>using namesp...原创 2018-08-14 10:34:04 · 289 阅读 · 0 评论 -
136.只出现一次的数字
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?示例 1:输入: [2,2,1]输出: 1示例 2:输入: [4,1,2,1,2]输出: 4class Solution {public: int singleNumber(vec...原创 2018-10-23 12:50:17 · 173 阅读 · 0 评论 -
137.只出现一次的数字II
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。说明:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?示例 1:输入: [2,2,3,2]输出: 3示例 2:输入: [0,1,0,1,0,1,99]输出: 99class Solution {public: int singleN...原创 2018-10-23 12:53:14 · 177 阅读 · 0 评论 -
201.数字范围按位与
给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。示例 1: 输入: [5,7]输出: 4示例 2:输入: [0,1]输出: 0class Solution {public: int rangeBitwiseAnd(int m, int n) { ...原创 2018-10-30 14:39:05 · 622 阅读 · 0 评论 -
338.比特位计数
给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。示例 1:输入: 2输出: [0,1,1]示例 2:输入: 5输出: [0,1,1,2,1,2]进阶:给出时间复杂度为O(n*sizeof(integer))的解答非常容易。但你可以在线性时间O(n)内用一趟扫描做到吗? 要求算法的空...原创 2018-11-09 14:43:33 · 192 阅读 · 0 评论 -
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).Example 1:Input: 11Output: 3Explanation: Integer 11 has binary representa...原创 2018-07-12 00:18:09 · 174 阅读 · 0 评论 -
Reverse Bits
Reverse bits of a given 32 bits unsigned integer.Example:Input: 43261596Output: 964176192Explanation: 43261596 represented in binary as 00000010100101000001111010011100, return 9641761...原创 2018-07-12 00:17:58 · 196 阅读 · 0 评论 -
寻找奇数出现II
给定一个整型数组arr,其中有两个数出现了奇数次,其他的数都出现了偶数次,找到这两个数。要求时间复杂度为O(N),额外空间复杂度为O(1)。给定一个整形数组arr及它的大小n,请返回一个数组,其中两个元素为两个出现了奇数次的元素,请将他们按从小到大排列。测试样例:[1,2,4,4,2,1,3,5],8返回:[3,5]class OddAppearance {public: vector<...原创 2018-06-15 08:48:51 · 189 阅读 · 0 评论 -
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).Example 1:Input: 11Output: 3Explanation: Integer 11 has binary representa...原创 2018-06-09 07:56:40 · 196 阅读 · 0 评论 -
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).Example 1:Input: 11Output: 3Explanation: Integer 11 has binary representa...原创 2018-06-09 07:56:01 · 192 阅读 · 0 评论 -
Reverse Bitsclass
Reverse bits of a given 32 bits unsigned integer.Example:Input: 43261596Output: 964176192Explanation: 43261596 represented in binary as 00000010100101000001111010011100, return 9641761...原创 2018-06-08 09:10:40 · 251 阅读 · 0 评论 -
Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.Example 1:Input: [5,7]Output: 4Example 2:Input: [0,1]Output: 0class Sol...原创 2018-06-08 09:11:14 · 239 阅读 · 0 评论 -
Add Binary
Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.Example 1:Input: a = "11", b = "1"Output: "100"Example 2:Inp...原创 2018-06-17 01:35:13 · 133 阅读 · 0 评论 -
不使用任何判断比较两个整数
对于两个32位整数a和b,请设计一个算法返回a和b中较大的。但是不能用任何比较判断。若两数相同,返回任意一个。给定两个整数a和b,请返回较大的数。class Compare {public: int foo(int n){ //n为正返回0,n为负返回1; return n^1; } int sign(int n){ //为负返回1,为正返回0。 ...原创 2018-06-15 08:47:27 · 204 阅读 · 0 评论 -
N!阶层的二进制表示中最低位1的位置
其实如果最后一位是0,那么进行右移操作,直到最后一位是1结束,也就是求质因子2的个数,然后再加1即可即有:[N/2]+[N/4]+[N/8]......int CountLowestOne(int n) { int ret=0; while(n) { n>>=1; ret+=n; } return ...原创 2018-06-18 10:06:31 · 725 阅读 · 0 评论 -
判断一个整数是否为2的方幂
return n>0&&((n&(n-1))==0)注意:n&(n-1)中,n-1将n的二进制表示中的最后一个1变成0,如果n满足2的方幂,那么n&(n-1)必为0.原创 2018-06-18 10:06:45 · 808 阅读 · 0 评论 -
最大公约数
问题:求两个正整数的最大公约数方法一:辗转相除法int gcd(int x,int y){return (!y)?x:gcd(y,x%y);}方法二:辗转相减法int gcd(int x,int y){if(x<y) return gcd(y,x);if(y==0) return x;else return gcd(x-y,y);}方法三:利用移位运算和减法运算int gcd(int x,i...原创 2018-06-18 10:07:16 · 380 阅读 · 0 评论 -
格雷码
在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code),另外由于最大数与最小数之间也仅一位数不同,即“首尾相连”,因此又称循环码或反射码。格雷码的特点:1.格雷码属于可靠性编码。它在相邻位间转换时,只有一位产生变化。它大大地减少了由一个状态到下一个状态时逻辑的混淆。由于这种编码相邻的两个码组之间只有一位不同,因而在用于方向的转角位移量-数字量的转换中...原创 2018-06-21 01:05:56 · 1797 阅读 · 0 评论 -
Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray...原创 2018-06-21 01:06:04 · 144 阅读 · 0 评论 -
游戏任务标记
游戏里面有很多各式各样的任务,其中有一种任务玩家只能做一次,这类任务一共有1024个,任务ID范围[1,1024]。请用32个unsigned int类型来记录着1024个任务是否已经完成。初始状态都是未完成。 输入两个参数,都是任务ID,需要设置第一个ID的任务为已经完成;并检查第二个ID的任务是否已经完成。 输出一个参数,如果第二个ID的任务已经完成输出1,如果未完成输出0。如果第一或第二个I...原创 2018-06-30 22:00:17 · 550 阅读 · 1 评论 -
Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using ...原创 2018-06-28 17:23:51 · 168 阅读 · 0 评论 -
421.数组中两个数的最大异或值
给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 。找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i, j < n 。你能在O(n)的时间解决这个问题吗?示例:输入: [3, 10, 5, 25, 2, 8]输出: 28解释: 最大的结果是 5 ^ 25 = 28.class ...原创 2018-11-15 19:01:52 · 793 阅读 · 0 评论