代码改变世界

leetcode - Gray Code

2013-04-19 10:16  张汉生  阅读(152)  评论(0编辑  收藏  举报

题目描述:点击此处

 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     vector<int> rlt;
 7     rlt.push_back(0);
 8     if (n<=0)
 9       return rlt;
10     int i;
11     for (i=0; i<n; i++){
12       int addNum = 1<<i;
13       vector<int>::iterator ii = rlt.end()-1;
14       vector<int> tmpVec;
15       int size = rlt.size();
16       while (size-- > 0){
17         tmpVec.push_back(*ii + addNum);
18         ii--;
19       }
20       size = rlt.size();
21       ii = tmpVec.begin();
22       while(size-- >0 )
23         rlt.push_back(*(ii++));
24     }
25     return rlt;
26   }
27 };