标签:
https://leetcode.com/problems/gray-code/
1 class Solution { 2 public: 3 vector<int> grayCode(int n) { 4 vector<int> res; 5 if(n==0) 6 { 7 res.push_back(0); 8 } 9 else if(n==1) 10 { 11 res.push_back(0); 12 res.push_back(1); 13 } 14 else 15 { 16 vector<int> res1=grayCode(n-1); 17 int size1=res1.size(); 18 res.insert(res.begin(),res1.begin(),res1.end()); 19 20 21 int addNumber=1<<(n-1); 22 for(int i=size1-1;i>=0;i--) 23 { 24 res.push_back(addNumber+res.at(i)); 25 } 26 } 27 return res; 28 29 30 } 31 };
标签:
原文地址:http://www.cnblogs.com/aguai1992/p/4642939.html