
Bit Manipulation
iteye_17352
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode - Divide Two Integers
[分析] 不能使用乘、除、取模运算,直接的思路当然是一次减一个除数,当然面试是没必要考这个的。如何提高效率呢?那就是要尽可能减少运算次数,能不能一次减去除数的若干倍呢?在pow(x,n)和sqrt(x)已指出数值计算题目通常两个思路,二分法和以2为基的位运算。左移一位相当于乘2,于是我们可以尝试每次减去除数的2^k倍,如何确定k取值?只要它满足:divs * 2^k > 31) == 1 ? ...原创 2015-08-11 09:00:31 · 91 阅读 · 0 评论 -
Leetcode - Pow(x, n)
[分析] 数值计算类型题目,二分法或者借助位运算,本题两种方法都可解。实现1是二分法思路,缺点是递归可能导致栈溢出。实现2是借助位运算,并进行了各种溢出检查和处理。实现3同样是位运算法,除了计算过程使用long类型的n外没有进行其他溢出保护,代码非常简洁,利于理解题目本身思路,实际应用中需要像实现2那样做溢出检查以保证程序的健壮性。 [ref] [url]http://www.cnblog...原创 2015-08-11 09:45:53 · 94 阅读 · 0 评论 -
Leetcode - Single Num II
[分析] 此题找到三种符合题意的解法,只理解了解法1,解法2有很详细的解析,理解中~ 解法1:对于32个数位,遍历数组统计每一位上 1 出现的次数,若 mod 3为1说明仅出现一次的数字该位为1。该解法可简单套用到变形题目:除1个数字外其余均出现k次。 注意此题可一般化为:[color=blue]给定一个数组,除了一个数出现 k 次外其他数字均出现 p 次,其中 p > 1, k >= 1,...原创 2015-08-16 20:55:03 · 138 阅读 · 0 评论 -
Leetcode - Power of Two
Given an integer, write a function to determine if it is a power of two. [code="java"] public class Solution { public boolean isPowerOfTwo1(int n) { if (n < 0) return false; ...原创 2015-08-16 21:48:13 · 76 阅读 · 0 评论 -
Leetcode - Bitwise AND of Number Range
Given a range [m, n] where 0原创 2015-08-17 09:41:10 · 104 阅读 · 0 评论 -
Leetcode - Repeated DNA Sequences
All 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. Wri...原创 2015-08-17 13:43:04 · 101 阅读 · 0 评论 -
Leetcode - 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 nu...原创 2015-08-17 20:24:25 · 94 阅读 · 0 评论 -
Leetcode - Palindrome Permutation
[分析] 思路2让我大开眼界,顺便学习下BitSet~ [ref] [url]https://leetcode.com/discuss/53180/1-4-lines-python-ruby-c-c-java[/url] [code="java"] public class Solution { // Method 2: https://leetcode.com/discu...原创 2015-08-22 16:24:14 · 125 阅读 · 0 评论