2017年5月1日 星期一

[LeetCode] 338. Counting Bits

轉自LeetCode

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 = 5 you should return [0,1,1,2,1,2].
Follow up:
  • It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
  • Space complexity should be O(n).
  • Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
<Solution>

[LeetCode] 342. Power of Four

轉自LeetCode

Given 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.
Follow up: Could you solve it without loops/recursion?
<Solution>

[LeetCode] 326. Power of Three

轉自LeetCode

Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
<Solution>

[LeetCode] 231. Power of Two

轉自LeetCode

Given an integer, write a function to determine if it is a power of two.

<Solution>

[LeetCode] 191. Number of 1 Bits

轉自LeetCode

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 00000000000000000000000000001011, so the function should return 3.
<Solution>

[LeetCode] 190. Reverse Bits

轉自LeetCode

Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
<Solution>

[LeetCode] 187. Repeated DNA Sequences

轉自LeetCode

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.
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
For example,
Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",

Return:
["AAAAACCCCC", "CCCCCAAAAA"].
<Solution>