Bit Manipulation
konsy_dong
Java,C++,Python,linux
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode 461. Hamming Distance
题目:The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note:原创 2017-03-16 18:27:01 · 568 阅读 · 0 评论 -
LeetCode 389. Find the Difference
题目: Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was原创 2017-04-19 10:41:51 · 308 阅读 · 0 评论 -
《剑指Offer》 不用加减乘除做加法
题目描述: 写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。思路: 首先看十进制是如何做的: 5+7=12,三步走 第一步:相加各位的值,不算进位,得到2。 第二步:计算进位值,得到10. 如果这一步的进位值为0,那么第一步得到的值就是最终结果。第三步:重复上述两步,只是相加的值变成上述两步的得到的结果2和10,得到12。同样我们可以用三步走的方式计算二进制值相原创 2017-04-08 15:00:28 · 322 阅读 · 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.For example, Given nums = [0, 1, 3] return 2.Note: Your algorithm should run in原创 2017-04-06 16:47:46 · 266 阅读 · 0 评论 -
LeetCode 231. Power of Two
题目: Given an integer, write a function to determine if it is a power of two.思路: 判断一个数是否是2的幂 2的N次幂的特点:仅有首位为1,其余各位都为0 代码:class Solution {public: bool isPowerOfTwo(int n) { return (n>0)原创 2017-04-04 16:23:32 · 418 阅读 · 0 评论 -
LeetCode 136. Single Number
题目:Given an array of integers, every eleme:nt appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without u原创 2017-03-17 11:41:45 · 438 阅读 · 0 评论 -
LeetCode 169. Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority原创 2017-03-17 20:36:54 · 298 阅读 · 0 评论 -
《剑指Offer》 二进制中1的个数
题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?思路: 这题和前面的那道跳台阶是一模一样的题,也是动态规划,这里的2*1的小矩形横着放就是跳2阶,竖着放就是跳1阶,当大矩形长度小于等于2时,方法数量为长度本身;当大于2时,即为d[i]=d[i-1]+d[i-2],但是递归调用太耗时,就用迭代。代码:cla原创 2017-04-06 20:28:18 · 283 阅读 · 0 评论 -
LeetCode 476. Number Complement
题目: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Note: 1. The given integer is guaranteed to fit within the range o原创 2017-04-04 16:29:45 · 436 阅读 · 0 评论 -
LeetCode 371. Sum of Two Integers
题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.思路: 其实就是模拟进位 1.首先最低位相加是要进一的 2.然后在做第二位的运算 3.以此类推,一直到没有进位为止。代码:class Solution {public: int get原创 2017-04-04 16:28:32 · 638 阅读 · 0 评论 -
LeetCode 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 0000000原创 2017-04-04 16:21:45 · 419 阅读 · 0 评论 -
LeetCode 338. Counting Bits
题目: Given 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 num = ...原创 2018-03-23 02:12:59 · 269 阅读 · 0 评论
分享