4/18/2014

Leetcode -- Gray Code

Go get a sequence of gray code, take down the most significant bit, then xor every two bits
class Solution {  
 public:  
   vector<int> grayCode(int n) {  
     vector<int> result;  
     int total = pow(2,n);  
     for(int i = 0; i < total; ++i) {  
       int code = (i >> 1) ^ i;  
       result.emplace_back(code);  
     }  
     return result;  
   }  
 };  

No comments: