grey code

 1 class Solution {
 2 public:
 3     vector<int> grayCode(int n) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         
 7         
 8         vector<int> vec(pow(2.0,n),0);
 9         for(int i=1;i<=n;i++)
10         {
11             for(int j=pow(2.0,i)-1;j>=pow(2.0,i-1); j-- )
12             {
13                 vec[j] = vec[pow(2.0,i)-j-1] + pow(2.0,i-1);
14             }
15         }
16         return vec;
17         
18     }
19 };

 

posted on 2013-09-03 15:38  jumping_grass  阅读(122)  评论(0)    收藏  举报

导航