leetcode Gray Code

来源:互联网 发布:山西建筑网络快报 编辑:程序博客网 时间:2024/06/06 10:04

此题将 n  的格雷码求出,首先要知道格雷码的含义,即相邻位之间只有一位不同

代码

class Solution {public:    vector<int> grayCode(int n) {        // 共有 2^n个格雷码        int size = 1<<n;        vector<int> result;        for(int i = 0; i < size; ++i)        {            result.push_back((i>>1)^i);                    }                return result;    }};


参考http://fisherlei.blogspot.com/2012/12/leetcode-gray-code.html





0 0
原创粉丝点击