![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
位运算
涛涛酱
北邮研究僧在读
展开
-
【LeetCode 50】Pow(x, n)
题目描述 Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 Example 3: Input: 2.00000, -2 O...原创 2020-02-08 15:02:58 · 130 阅读 · 0 评论 -
【LeetCode 29】 Divide Two Integers
题目描述 Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. Return the quotient after dividing dividend by divisor. The integer division ...原创 2020-02-08 01:32:37 · 68 阅读 · 0 评论 -
【剑指 offer】二进制中1的个数
题目描述: 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。 思路: 先想到的是对当前整数不断的和1做按&运算,可以判断最右边位置是不是1,然后整数右移,重复操作。 误区: 输入数字为负的时候,右移时在左边补充的1,一直右移,最后数字会变成0xFFFFFFFF而陷入死循环。 那,然后想到把1左移,不断和输入数字按位&,这样循环32次,可以判断完成。 注意应该是uin...原创 2019-03-08 16:32:10 · 71 阅读 · 0 评论 -
【剑指 offer】不用加减乘除做加法
题目描述: 写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。 思路: 按位操作,不考虑进位时,结果为按位异或,单独考虑进位,结果为按位与左移一位。重复直到进位为0。 代码: class Solution { public: int Add(int num1, int num2) { int sum = num1 ^ num2; ...原创 2019-03-12 16:19:15 · 67 阅读 · 0 评论 -
【剑指 offer】数组中只出现一次的数字
题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。 思路: 如果数组中只有一个只出现一次的数字,所有数字的异或结果就是要找的数字。现在有两个只出现一次的数字,所有数字的异或结果其实就是这两个数字的异或。按照结果的第一个数字1的位置把所有数字分成两个数组。分别异或,即找到两个数字。 代码: class Solution { public: ...原创 2019-03-27 16:07:17 · 81 阅读 · 0 评论 -
【LeetCode 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. Example 1: Input: [3,0,1] Output: 2 Example 2: Input: [9,6,4,2,3,5,7,0,1] Outp...原创 2019-05-07 10:02:27 · 69 阅读 · 0 评论