[Leetcode] bit operation explanations

[This article is continuously updated]

78. child Collection

Title: Given a set of integer array nums no repeating element, which returns an array of all possible subsets (power set).
Example:

输入: nums = [1,2,3]
输出:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

This question has done several times already, before using a backtracking, this idea of ​​using bit operation.

Bit operation Solution: The \ (the nums \) each location as a \ ( 'bit \) , if a \ (' bit \) is 1, indicates \ (nums [i] \) necessary to add a subset, or not added. Obviously, each subset corresponds to a bit string from the bit string \ (0 ... 0 \) to \ (1 1 ... \) changes (from a different angle also demonstrated a number of subsets of \ ( the n-^ 2 \) ).

Exemplified below, when the time $ $ nums = [1,2]:

Bit A corresponding subset
0 0 [ ]
0 1 [2]
1 0 [1]
1 1 [1,2]

Bit operation code (C ++):

class Solution
{
public:
    vector<vector<int>> subsets(vector<int> &nums)
    {
        vector<vector<int>> vv;
        int total = 1 << nums.size();
        for (int i = 0; i < total; i++)
        {
            vv.push_back(getItem(i, nums));
        }

        return vv;
    }

    vector<int> getItem(int n, const vector<int> &nums)
    {
        vector<int> v;
        for (int i = 0; i < nums.size(); i++)
        {
            if ((n >> i) & 0x1)
                v.push_back(nums[i]);
        }
        return v;
    }
};

Backtracking Code (python):

class Solution:
    def __init__(self):
        self.ans = list()
        self.ans.append([])

    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        cur = []
        self.helper(cur, nums)
        return self.ans

    def helper(self, cur: list, nums: list):
        for i in range(0, len(nums)):
            cur.append(nums[i])
            self.ans.append(list(cur))
            self.helper(cur, nums[(i + 1):])
            cur.pop()

136. The number appears only once

Title: Given a non-empty array of integers, in addition to an element appears only once, the rest of each element appears twice. To find out that only appears once in the elements.
Example:

输入: [2,2,1]
输出: 1

输入: [4,1,2,1,2]
输出: 4

Before problem solving, first of all need to be familiar three conclusions:

  • \(0 \oplus x = x\)
  • \(x \oplus x = 0\)
  • \ ((X \ oplus s) \ oplus z = x \ oplus (s \ oplus z) \)

This is the exclusive OR of three properties. Back to this question, asked to identify only appear 1 (or odd number of times, this question method is still applicable). Obviously, whenever a \ (k \) appears even number of times, all the \ (k \) XOR result is 0. For odd numbers appear \ (x \) , all of \ (x \) XOR result is \ (x \) .

Once you understand the above three conclusions, direct look at the code:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int x = 0;
        for (auto k:nums)
        {
            x^=k;
        }
        return x;
    }
};

Guess you like

Origin www.cnblogs.com/sinkinben/p/12323784.html