(2) count how many 1s in z's binary representation in a loop.
169
Majority Element
The idea is to iterate each bit of all the numbers and try to get the statistics of each bit. If a given bit has more ones than zeros then set the result bit to 1, otherwise set it to zero.
Use
a hash map to count each 10 character string frequency. In the second
loop, loop each map element to check f its frequency is > 1, put it
in the result array. Return the result array in the end.
No need to calculate the hash key as binary number using bit manipulation.
return n <= 0 ? false : (n ^ (n - 1)) == 0;
Add one extra check based on 231: (n & 0x55555555) != 0
Loop and count each 1 bit (n & (0x1 << 1)( in the 32-bit binary representation
476
Number Complement
I basically check every bit of number by XOR'ing it with appropriate power of 2 which leads to its invertion.
For example:
Entered: 4=>100
100 ^ 001 = 101
101 ^ 010 = 111
111 ^ 100 = 011
Out: 011=>3
371 Sum of Two Integers
Single Number
We could use hash map to solve this in O(N) time and O(N) space. But an even better
way is to use bit manipulation. Since we know there is only one number appears once,
all the others appears twice. If we xor all the numbers. The result will be the number
just appeared once.
One sentence summary:
xor all the numbers in a loop. (vector array version xor)
Similar to
389 Find the Difference
Exactly the same idea with 136. (string version xor)
268 Missing Number
Also similar to 136, 389. Just a little bit effort to figure out that we need to also
xor the array index. Define res = 0. and Then for loop each number in the array from 0 to n - 1. do
res ^= i ^ nums[i]. Then after the loop, res ^= n; return res.
693 Binary Number with Alternating Bits
Smart solution. Using the fact that if n is a alternating number then
n + (n >> 1) is all 1s, and n + (n >> 1) + 1 should be power of 2. And
since we know, any number that is a power of two, have an attribute which
is n & (n - 1) is 0. We just need to check that.
Loop each number from L to R inclusively. Count how many 1s are there in their
binary representation. Check whether the number is prime. For prime check we can
hard code all the primes into a hash set and use count(n) to check whether n is a
primer number.
Reverse Bits
lg(N) in place alrogithm:
n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1); // swap odd even bits
n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2); // swap odd even 2-bit
n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4); // swap odd even 4-bit
n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8); // swap odd even 8-bit
n = ((n & 0xFFFF0000) >> 16) | ((n & 0x0000FFFF) << 16); // swap odd even 16-bit
return n;
Pyramid Transition Matrix
The idea is to use a string to set hash map to save all the possible transition map.
And then use a DFS helper to construct the possible pyramid.
bool DFS(string cur, string above, unordered_map<string, unordered_set<char> >& mp)
(1) Base case: if cur.size() == 2 && above.size() == 1, return true
(2) Recursion formula: if (cur.size() == above.size() + 1), recursively call
DFS(above, "", mp), since that means the current level is successfully constructed.
(3) Otherwise when cur.size() > above.size() + 1, in the middle of building.
get the base string: base = cur.substr(above.size(), 2); If base exist in the map
then for loop each possible char that could be used to build the next level to see
whether if any could be used to build the above level successfully. If any, return
true.
(4) If no early exit, in the end, return false meaning couldn't build the pyramid.
O(2^N) time
UTF-8 Validation
Define a count variable to denote how many remaining bytes still left for the current
valid UTF-8 character and initialize it to zero
(1) For loop each char in the data and do the following
(2) When count == 0. Do the following check. if high 3 bits are 0b110, set count to 1.
Else if high 4 bits are 0b1110, set count to 2. Else if high 5 bits are 0b11110, set
count to 3. Else if MSB is not 0, return false (invalid). Implicitly, we known MSB is
0, we just loop next byte in the data.
(3) If count != 0, meaning it is the 2/3/4 byte in the character. Check whether the
byte begin with 0b10, if not return false. Otherwise, count--.
(4) In the end, if count == 0, then it is valid, otherwise, invalid.
338 Counting Bits
Loop from MSB to LSB. And save the prefix of all the numbers in a set. And assume the
current bit in the final result max can be set to 1. Then try to find whether max ^ pre
is in the prefix set. If so update max = max ^ pre. Keep doing this for every bit from
MSB to LSB. In the end return max.
Binary Watch
Idea: For loop each hour and minutes combination to count how many bits are set in the
hour and minutes. If the number is equal to the given number of LED lights, then push
it into the result string. In the end return the result string.
Gray Code
Comments
Post a Comment