
Bit Manipulation
yuanhisn
这个作者很懒,什么都没留下…
展开
-
Find a duplicate element in an array
Question:Suppose you have an array of 1001 integers. The integers are in random order, but you know each of the integers is between 1 and 1000 (inclusive). In addition, each number appears only on...原创 2015-01-07 06:24:57 · 131 阅读 · 0 评论 -
Find two non-repeating elements in an array
一个int型数组里面所有数都出现两次,只有两个数出现一次。找出这两个数。Solution:public void findTwoUniqueNumbers(int[] A) { int x = 0; for(int i=0; i<A.length; i++) { x ^= A[i]; } int lsb = x & -x; int a = 0, ...原创 2015-02-14 11:52:32 · 87 阅读 · 0 评论 -
LeetCode 187 - 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-02-15 06:28:46 · 95 阅读 · 0 评论 -
Count the number of set bits in an integer
Question: Count the number of set bits in an 32 integer. Method 1:Count the bit one by one, 这显然不好:public int bitCount(int a) { int num = 0; for (int i=0; i<32; ++i) { ...原创 2015-03-13 00:19:10 · 130 阅读 · 0 评论 -
Google Interview - Valid UTF-8 Character
Write a function to validate whether the input is valid UTF-8. Input will be string or byte array, output should be true or false.UTF-8是一种变长的编码方式。它可以使用1~4个字节表示一个符号,根据不同的符号而变化字节长度。UTF-8的编码规则很简单...原创 2015-06-16 12:56:08 · 247 阅读 · 0 评论 -
Longest Consecutive 1s in Binary
Find the longest continuous subsequence of 1's in binary representation of an integer.Solution 1: public int countConsecutive1s(int n) { if(n == 0) return 0; int max = 0; int len = 0; ...原创 2015-07-31 03:23:31 · 112 阅读 · 0 评论