位运算
文章平均质量分 50
Bryan要加油
这个作者很懒,什么都没留下…
展开
-
Bitwise AND of Numbers Range
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.class Solution {public: int rangeBitwiseAnd(int m, int n) { int offset = 0; while(m !原创 2015-04-22 11:28:49 · 565 阅读 · 0 评论 -
136. Single Number
Given an 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 ext原创 2014-02-27 16:20:12 · 539 阅读 · 0 评论 -
318. Maximum Product of Word Lengths
ProblemGiven a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower cas原创 2016-04-23 11:19:03 · 425 阅读 · 0 评论 -
338. Counting Bits
ProblemGiven a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array. Example: For原创 2016-04-23 10:26:20 · 325 阅读 · 0 评论 -
67. Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".class Solution {public: string addBinary(string a, string b) {原创 2015-06-26 15:29:11 · 615 阅读 · 0 评论 -
342. Power of Four
ProblemGiven an integer (signed 32 bits), write a function to check whether it is a power of 4.Example: Given num = 16, return true. Given num = 5, return false.Solution常规解法class Solution {public:原创 2016-05-14 11:56:52 · 391 阅读 · 0 评论 -
231. Power of Two
ProblemGiven an integer, write a function to determine if it is a power of two.Solution如果一个数是2的幂,那么它的二进制表示中就只有一个“1”。 n&(n-1)得到的结果的二进制表示中的1的个数比n中少一个。 如果n&(n-1)的结果是0,说明n是2的幂。class Solution {public:原创 2016-05-14 11:49:25 · 354 阅读 · 0 评论 -
29. Divide Two Integers
ProblemDivide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. Solution 参考。 在这道题中,要求两个整数的商,但是不能使用除法、乘法和取模操作,所以只能使用位运算。 首先看个例子,15除以3,15是被除数原创 2016-05-12 18:54:12 · 352 阅读 · 0 评论 -
最接近的数
题目描述有一个正整数,请找出其二进制表示中1的个数相同、且大小最接近的那两个数。(一个略大,一个略小)给定正整数int x,请返回一个vector,代表所求的两个数(小的在前)。保证答案存在。测试样例:2 返回:[1,4]解答位操作法取得后一个较大的树 以数字13948为例,二进制表示如下: 我们想让这个数大一点,但又不会太大,同时1的个数又要保持不变。你会发现:给定一个数 和两个位的原创 2016-03-21 16:10:10 · 1254 阅读 · 0 评论 -
二进制小数
题目描述有一个介于0和1之间的实数,类型为double,返回它的二进制表示。如果该数字无法精确地用32位以内的二进制表示,返回“Error”。给定一个double num,表示0到1的实数,请返回一个string,代表该数的二进制表示或者“Error”。测试样例:0.625 返回:0.101解答注意,为了表示清晰,我们x2,x10 x_{2},x_{10} 来表示x是二进制还是十进制。 首先,我原创 2016-03-21 15:49:17 · 515 阅读 · 0 评论 -
二进制插入
题目描述有两个32位整数n和m,请编写算法将m的二进制数位插入到n的二进制的第j到第i位,其中二进制的位数从低位数到高位且以0开始。 给定两个数int n和int m,同时给定int j和int i,意义如题所述,请返回操作后的数,保证n的第j到第i位均为零,且m的二进制位数小于等于i-j+1。测试样例:1024,19,2,6 返回:1100解答这个问题的解决可以分为三个步骤:将n中从第j位到原创 2016-03-21 15:15:47 · 766 阅读 · 0 评论 -
不用加减乘除做加法
题目描述写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。解答 首先,我们分析人们是如何做十进制的加法的。例如,如何得到7+15=22这个结果。实际上,我们可以分成三步进行: 只做各位相加,不进位。这个时候的相加结果是12(个位数5和7相加不进位是2,十位数0和1相加结果是1);做进位,5+7有进位,进位的值是10;把第1步和第二步的两个结果相加,结果是22原创 2016-03-05 15:49:19 · 611 阅读 · 0 评论 -
Single Number III & 数组中只出现一次的数字
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转载 2015-12-05 16:20:37 · 440 阅读 · 0 评论 -
191. Number of 1 Bits&二进制中1的个数
题目描述输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。解答错误的,可能引起死循环解法 先判断整数二进制表示中最右边一位是不是1。接着把输入的整数右移一位,此时原来处于右边起的第二位被移到最右边,再判断它是不是1。这样每次移动一位,直到整个整数变成0为止。 那么如何判断一个整数的最右边是不是1。只要把整数和1做按位与运算,看结果是否为0就可以判断。1除了最右边的一位以外,所有原创 2016-03-04 09:47:35 · 497 阅读 · 0 评论 -
位运算
手工位操作^表示XOR(异或)操作~表示非(取反)操作一个数与相乘,相当于这个数左移n位~0的值就是一串1,将它左移n位后的结果就是一串1后面跟n个0。x&(~0 << n)的操作会将x最右边的n位清零。左移运算符m << n表示把m左移n位。左移的时候,最左边的n位将被丢弃,同时在最右边补上n个0。右移运算符m>>n表示把m右移n位。右移n位的时候,最右边的n位将被丢弃。但右移处理时左原创 2016-03-04 09:21:00 · 405 阅读 · 0 评论 -
位运算
手工位操作^表示XOR(异或)操作~表示非(取反)操作一个数与相乘,相当于这个数左移n位一个比特与对它取反的值做异或操作,结果总是1,所以a^(~a)的结果是一串1。 -~0的值就是一串1,将它左移n位后的结果就是一串1后面跟n个0。 x&(~0位操作的原理与技巧以下示例中的“1s”和“0s”分别表示一串1和一串0x ^ 0s = x,因为异或操作,相同数字为0,不同才原创 2016-03-04 09:00:23 · 609 阅读 · 0 评论 -
268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm should ru原创 2016-01-11 22:25:47 · 487 阅读 · 0 评论 -
229.Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.Hint:How many majority elements could it poss原创 2015-07-01 08:53:07 · 1622 阅读 · 0 评论 -
187. Repeated DNA Sequences
ProblemAll DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.W原创 2016-05-09 17:02:06 · 461 阅读 · 0 评论