Algorithm leetcode|89. Gray coding (rust hits hard)



89. Gray coding:

n-bit Gray code sequence is a sequence composed of 2n integers, where:

  • each integer is in the range [0, 2n - 1] (including 0 sum 2n - 1)
  • The first integer is0
  • An integer appears in the sequence no more than once
  • Each pair of adjacent binary representations of integersexactly one bit different, and
  • First and Last Binary representation of integersExactly one difference

Give you an integer n and return any valid n-bit Gray code sequence.

Example 1:

输入:
	
	n = 2
	
输出:
	
	[0,1,3,2]
	
解释:

	[0,1,3,2] 的二进制表示是 [00,01,11,10] 。
	- 00 和 01 有一位不同
	- 01 和 11 有一位不同
	- 11 和 10 有一位不同
	- 10 和 00 有一位不同
	[0,2,3,1] 也是一个有效的格雷码序列,其二进制表示是 [00,10,11,01] 。
	- 00 和 10 有一位不同
	- 10 和 11 有一位不同
	- 11 和 01 有一位不同
	- 01 和 00 有一位不同

Example 2:

输入:
	
	n = 1
	
输出:
	
	[0,1]

hint:

  • 1 <= n <= 16

analyze:

  • Facing this algorithm question, the second master once again fell into deep thought.
  • Let’s first look at what Gray code is:

The typical Binary Gray Code (Binary Gray Code), referred to as Gray code, is named after Frank Gray (18870913-19690523) patent "Pulse Code Communication" published in 1953. It was originally used for communication. Now it is commonly used in analog-to-digital conversion and position-to-digital conversion. The Baudot code used by French telecommunications engineer Jean-Maurice-Émile Baudot (18450911-19030328) in 1880 is equivalent to a variant of it. In 1941, George Stibitz designed an 8-element binary mechanical counter that exactly conforms to the counting rules of the Gray code counter.
In the encoding of a set of numbers, if any two adjacent codes have only one binary number that is different, then this encoding is called Gray Code. In addition, since the maximum number is The smallest numbers only differ by one digit, that is, "connected end to end", so they are also called cyclic codes or reflective codes.
In number systems, codes are often required to change in a certain order. For example, if counting by natural number increment, if 8421 code is used, all four bits will change when the number 0111 changes to 1000. However, in the actual circuit, the change of 4 bits cannot occur absolutely at the same time, and other short-term codes may appear in the counting. (1100, 1111, etc.). Under certain circumstances this may result in incorrect circuit status or input errors. This error can be avoided by using Gray code. Gray code has many encoding forms.
Gray Code has been called Gray Code, Gray Code, Gray Code, Golay Code, Cyclic Code, Reflected Binary Code, Minimum Error Code and other names. Some of them are wrong. , some are easily confused with other names, it is recommended not to use these former names.

  • The key point is that adjacent codes can only differ by one bit.
  • Let me say that this question is an experience question or a brain teaser. Just simulate it.
  • Predecessors gave the formula gi​ = i ^ (i / 2) according to the rules, which can easily ensure that two adjacent The numbers differ only by one digit.

answer:

rust:

impl Solution {
    
    
    pub fn gray_code(n: i32) -> Vec<i32> {
    
    
        (0..1 << n).map(|i| {
    
    
            (i >> 1) ^ i
        }).collect()
    }
}

go:

func grayCode(n int) []int {
    
    
    ans := make([]int, 1<<n)
	for i := range ans {
    
    
		ans[i] = (i >> 1) ^ i
	}
	return ans
}

c++:

class Solution {
    
    
public:
    vector<int> grayCode(int n) {
    
    
        vector<int> ans(1 << n);
        for (int i = 0; i < ans.size(); ++i) {
    
    
            ans[i] = (i >> 1) ^ i;
        }
        return ans;
    }
};

python:

class Solution:
    def grayCode(self, n: int) -> List[int]:
        ans = [0] * (1 << n)
        for i in range(1 << n):
            ans[i] = (i >> 1) ^ i
        return ans


java:

class Solution {
    
    
    public List<Integer> grayCode(int n) {
    
    
        List<Integer> ans = new ArrayList<Integer>();
        for (int i = 0; i < 1 << n; ++i) {
    
    
            ans.add((i >> 1) ^ i);
        }
        return ans;
    }
}

Thank you very much for reading this article~
Welcome [Like] [Collect] [Comment] Three times in a row~
It is not difficult to give up. But perseverance must be cool~
I hope we can all make a little progress every day~
This article is written by the white hat of the second boss: https://le-yi.blog.csdn.net/ Original blog~


Guess you like

Origin blog.csdn.net/leyi520/article/details/134502052