LeetCode OJ 89. Gray Code

题目

The gray code is a binary numeral system where two successive values differ in only one bit.

Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2

Note:

For a given n, a gray code sequence is not uniquely defined.

For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

解答

这就是离散数学里面一个格雷码的快速推导算法。。。

比如2位格雷码是00 01 11 10,这个数列奇数项左移后先加0再加1,偶数项左移后先加1再加0,按顺序组成的数列就是3位格雷码000 001 011 010 110 111 101 100。原理很简单,因为本来就是两两相邻之间差一位,现在按照这样操作,左移扩展一位,而新加的这一位又是01100110,这样原来每个数都扩展为相邻差一位,而原来相邻差一位的两个数之间加的是同样的一位数。

下面是AC的代码:

class Solution {
public:
    vector<int> grayCode(int n) {
        vector<int> ans;
        vector<int> temp;
        ans.push_back(0);
        for(int i = 0; i < n; i++){
            int length = ans.size();
            temp.clear();
            for(int i = 0; i < length; i++){
                if(i % 2 == 0){
                    temp.push_back(ans[i] * 2 + 0);
                    temp.push_back(ans[i] * 2 + 1);
                }
                else{
                    temp.push_back(ans[i] * 2 + 1);
                    temp.push_back(ans[i] * 2 + 0);
                }
            }
            ans = temp;
        }
        return ans;
    }
};

119

原文地址:https://www.cnblogs.com/YuNanlong/p/8880839.html

时间: 2024-11-05 22:43:58

LeetCode OJ 89. Gray Code的相关文章

【一天一道LeetCode】#89. Gray Code

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of

【LeetCode】89. Gray Code

题目: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0

[LeedCode OJ]#89 Gray Code

[ 声明:版权所有,转载请标明出处,请勿用于商业用途.  联系信箱:[email protected]] 题目链接:https://leetcode.com/problems/gray-code/ 题意: 给出一个n,要你得出一个包括所有n位格雷码的数组 思路: 首先我们要知道格雷码是怎么来的 现在我们要得到6的格雷码 首先我们先得到6的二进制位110 格雷码是通过在这个二进制前面加一个0,得到0110,然后相邻两位进行异或,得到101,所以6的格雷码为5 那么我们现在怎么求1~n所有数对应的格

LeetCode --- 89. Gray Code

题目链接:Gray Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must b

89. Gray Code

89. Gray Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must be

leetcode 89 Gray Code ----- java

The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. Fo

leetCode 89.Gray Code (格雷码) 解题思路和方法

The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. Fo

[LeetCode] 89. Gray Code 格雷码

The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. Ex

leetcode笔记:Gray Code(2016腾讯软件开发笔试题)

一.题目描述 The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin wit