Total Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Now your job is to find the total Hamming distance between all pairs of the given numbers.

Example:

Input: 4, 14, 2 
Output: 6
Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case). So the answer will be: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.

Note:

  1. Elements of the given array are in the range of to 10^9
  2. Length of the array will not exceed 10^4.
Continue reading

Using bit manipulation to achieve operators

1)计算负数的逆元

def negative(n):
    return bit_add(~n, 1)

2)加法计算

def bit_add(a, b):
    b &= (2 ** 32 - 1)
    while b:
        tmp = a
        sum = tmp ^ b
        carry = (tmp & b) << 1
    return a

3)减法计算

def bit_sub(a, b):
    return bit_add(a, negative(b))

4)乘法计算

def bit_mul(a, b):
    result = 0

    while b:
        if b & 0x1:
            result = bit_add(result, a)
        b >>= 1
        a <<= 1

    return result

乘法模拟手工计算乘法的流程,将乘数b的二进制表示从左往右移动,当最低位为1时 (b & 0x1 == 1),把被乘数a加到result中,同时将被乘数a向左移动一位。

5)除法计算

def bit_div(x, y):
    result = 0
    for i in range(32)[::-1]:
        if (x >> i) >= y:
            result += (1 << i)
            x -= (y << i)

    return result

考虑32位整数表示 (64位改一下 i 就可以了)。从最大的倍数开始遍历,如果被除数x >> i 大于除数y,说明该位商为1,将1<<i加到结果中,并缩小x。

Find medians from a slide window

实现一个一维的中位数滤波器。

形式化地来说,给出一个长度为n的数列a_1,a_2,a_3,…,a_n,# 求一个数列b_1,b_2,…,b_{n-k+1},使得b_i是子列(a_i,a_{i+1},…,a_{i+k-1})的中位数。

可以理解为一个长度为k的滑窗在长度为n的数列上滑动,每滑一次输出滑窗里面的数的中位数。

a = [1,2,3,12,-5,33]
k = 3
b = [2,3,3,12]b
Continue reading

Maximum XOR of Two Numbers in an Array

Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.

Find the maximum result of ai XOR aj, where 0 ≤ ij < n.

Could you do this in O(n) runtime?

Example:

Input: [3, 10, 5, 25, 2, 8] 
Output: 28
Explanation: The maximum result is 5 ^ 25 = 28.

这道题是在刷Explore的Trie tree topic里面看到的。首先一拿到题目,下意识的反应就是这道题没那么简单。最暴力的方法就是穷举遍历了,这样的话time complexity就会达到O(n^2)。

之后联想Trie tree和Binary之间的关系,可以画出上面这个图。这样问题就被转换为了找到最高的具有1节点的父节点,然后向下尽量找a^b = 1的分支 (我感觉我没有说清楚。。。)

之后看了讨论版里一位大佬的题解,真的是颇为震撼。他用了位运算的方法省去了很多不必要的步骤。 其中比较不好理解的是:

result += any(result ^ 1 ^ p in prefixes for p in prefixes)

为了理解这句话,首先我们得知道:a^b^a=b.

在这里result^1 == a^b, p = a, 所以result^1^p = b。如果在prefixes中有任意两个数a,b可以得到result^1, 而且这两个数a,b构成了previous result,那么新的result就可以是result += 1了,不然只能是result <<= 1(末位为0)。

class Solution:
    def findMaximumXOR(self, nums: List[int]) -> int:
        result = 0
        
        for i in range(32)[::-1]:
            result <<= 1
            prefixes = {num >> i for num in nums}
            result += any(result ^ 1 ^ p in prefixes for p in prefixes)
            
        return result
    

Flatten a Multilevel Doubly Linked List

You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below.

Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.

Example:

Input:  1---2---3---4---5---6--NULL          
|
7---8---9---10--NULL
|
11--12--NULL
Output: 1-2-3-7-8-11-12-9-10-4-5-6-NULL
"""
# Definition for a Node.
class Node:
    def __init__(self, val, prev, next, child):
        self.val = val
        self.prev = prev
        self.next = next
        self.child = child
"""

class Solution:
    def flatten(self, head: 'Node') -> 'Node':
        if not head:
            return None
        
        stack = list()
        cur = head
        
        while cur:
            if cur.child:
                if cur.next:
                    stack += [cur.next]
                cur.next = cur.child
                cur.next.prev = cur
                cur.child = None
           
            if not cur.next and stack:
                temp = stack.pop()
                cur.next = temp
                temp.prev = cur
                
            cur = cur.next
        
        return head

Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.

For example, given the following tree:

    1    
/ \
2 5
/ \ \
3 4 6

The flattened tree should look like:

1  
\
2
\
3
\
4
\
5
\
6
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def flatten(self, root: TreeNode) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        node, stack = root, []
        
        while node:
            if node.right:
                stack.append(node.right)
            node.right = node.left
            node.left = None
            
            if not node.right and stack:
                temp = stack.pop()
                node.right = temp
            
            node = node.right