My C++ solution


  • 0
    R
    vector<int> grayCode(int n) {
    vector<int> res;
    if(n<0)
        return res;
    if(n==0){
        res.push_back(0);
        return res;
    }
    res.push_back(0);
    res.push_back(1);
    for(int i=1;i<n;i++){
        vector<int> temp(res);
        int add = 1<<i;
        int a=0,b=temp.size()-1;
        for(b;b>=a;b--){
        	temp.push_back(temp[b]+add);
    	}
        res = temp;
    }
    return res;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.