Generate Gray code-C ++ implementation

The problem is described
in the encoding of a set of numbers. If any two adjacent codes differ by only one binary number, the encoding is called Gray Code. Please write a function and use the recursive method to generate N bits. Gray code.

Given an integer n, please return n-bit Gray code, starting from 0.
Test sample:
1
Return: ["0", "1"]

Analysis
1, n = 1, 0,1 return to
two, when n = 2, return 00,01,10,11
3, n = 3, return 000,001,010,011,100,101,110,111
Gray code returned by observing 1,2,3 n = It can be found that such a rule:
1. The gray code of n = 2 is the gray code of n = 1, and 0 is added to the front in sequence
. The length of the gray code at n is the length of the gray code at n-1. Twice

Algorithm implementation:

#include <iostream>
#include<vector>
using namespace std;

vector<string> getGray(int n) {
        // write code here
    vector<string> result;
    if(n == 1){
      result.push_back("0");
      result.push_back("1");
       return result;
    }else{
        result = getGray(n-1);
        int currentsize = (int)result.size();
        for(int i = 0; i < currentsize; i++){
            result.push_back(result.at(i));
        }
        for(int i = 0; i < currentsize; i++){
                 result.at(i) = "0" + result.at(i);
        }
        for(int i = currentsize; i < (int)result.size(); i++){
            result.at(i) = "1" + result.at(i);
        }
         return result;
        }
    }
int main(){
    vector<string> v;
    v = getGray(2);
    for(int i = 0; i < v.size(); i++){
        cout << v.at(i) << " ";
    }
    cout << endl;
    return 0;
}

The question on
Niuke.com found that it was not possible to submit a passing case because the test case result was n = 2, it returned 00,01,11,10 instead of 00,01,10,11
n = 3, it returned 000 001 011 010 110 111 101 100

Taking n-2 as an example, when inserting an array, the insertion is performed in the reverse order of the original array, so as long as the first for loop is slightly modified, you can access!
Code:

#include <iostream>
#include<vector>
using namespace std;

vector<string> getGray(int n) {
        // write code here
    vector<string> result;
    if(n == 1){
      result.push_back("0");
      result.push_back("1");
       return result;
    }else{
        result = getGray(n-1);
        int currentsize = (int)result.size();
        for(int i = currentsize - 1; i >= 0; i--){
            result.push_back(result.at(i));
        }
        for(int i = 0; i < currentsize; i++){
                 result.at(i) = "0" + result.at(i);
        }
        for(int i = currentsize; i < (int)result.size(); i++){
            result.at(i) = "1" + result.at(i);
        }
         return result;
        }
    }
int main(){
    vector<string> v;
    v = getGray(2);
    for(int i = 0; i < v.size(); i++){
        cout << v.at(i) << " ";
    }
    cout << endl;
    return 0;
}
Published 21 original articles · Likes0 · Visits 163

Guess you like

Origin blog.csdn.net/qq_45227330/article/details/105025054