[LintCode] Gray Code 格雷码

简介:

The gray code is a binary numeral system where two successive values differ in only one bit.

Given a non-negative integer n representing the total number of bits in the code, find the sequence of gray code. A gray code sequence must begin with 0 and with cover all 2nintegers.

 Notice

For a given n, a gray code sequence is not uniquely defined.

[0,2,3,1] is also a valid gray code sequence according to the above definition.

Example

Given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2
Challenge 

O(2n) time.

LeetCode上的原题,请参见我之前的博客Gray Code

解法一:

class Solution {
public:
    /**
     * @param n a number
     * @return Gray code
     */
    vector<int> grayCode(int n) {
        vector<int> res;
        for (int i = 0; i < pow(2, n); ++i) {
            res.push_back((i >> 1) ^ i);
        }
        return res;
    }
};

解法二:

class Solution {
public:
    /**
     * @param n a number
     * @return Gray code
     */
    vector<int> grayCode(int n) {
        vector<int> res{0};
        for (int i = 0; i < n; ++i) {
            int size = res.size();
            for (int j = size - 1; j >= 0; --j) {
                res.push_back(res[j] | (1 << i));
            }
        }
        return res;
    }
};

解法三:

class Solution {
public:
    /**
     * @param n a number
     * @return Gray code
     */
    vector<int> grayCode(int n) {
        vector<int> res{0};
        int len = pow(2, n);
        for (int i = 1; i < len; ++i) {
            int pre = res.back();
            if (i % 2 == 1) {
                pre = (pre & (len - 2)) | (~pre & 1);
            } else {
                int cnt = 1, t = pre;
                while ((t & 1) != 1) {
                    ++cnt; t >>= 1;
                }
                if ((pre & (1 << cnt)) == 0) pre |= (1 << cnt);
                else pre &= ~(1 << cnt);
            }
            res.push_back(pre);
        }
        return res;
    }
};

解法四:

class Solution {
public:
    /**
     * @param n a number
     * @return Gray code
     */
    vector<int> grayCode(int n) {
        vector<int> res{0};
        unordered_set<int> s;
        stack<int> st;
        s.insert(0);
        st.push(0);
        while (!st.empty()) {
            int t = st.top(); st.pop();
            for (int i = 0; i < n; ++i) {
                int k = t;
                if ((k & (1 << i)) == 0) k |= (1 << i);
                else k &= ~(1 << i);
                if (s.count(k)) continue;
                s.insert(k);
                st.push(k);
                res.push_back(k);
                break;
            }
        }
        return res;
    }
};

解法五:

class Solution {
public:
    /**
     * @param n a number
     * @return Gray code
     */
    vector<int> grayCode(int n) {
        vector<int> res;
        unordered_set<int> s;
        helper(n, s, 0, res);
        return res;
    }
    void helper(int n, set<int>& s, int out, vector<int>& res) {
        if (!s.count(out)) {
            s.insert(out);
            res.push_back(out);
        }
        for (int i = 0; i < n; ++i) {
            int t = out;
            if ((t & (1 << i)) == 0) t |= (1 << i);
            else t &= ~(1 << i);
            if (s.count(t)) continue;
            helper(n, s, t, res);
            break;
        }
    }
};

本文转自博客园Grandyang的博客,原文链接:格雷码[LintCode] Gray Code ,如需转载请自行联系原博主。

相关文章
|
存储 弹性计算 缓存
ecs负载评估
ECS负载评估基于资源综合性能得分,衡量CPU、内存、磁盘I/O、网络和系统负载等指标。得分0-5为低负载,5-80正常,80-100高负载。高负载可能需优化或扩容。根据负载级别,可调整资源配置、优化性能或使用自动伸缩服务,确保服务稳定和高效。
485 2
|
机器学习/深度学习 人工智能
可控图像生成最新综述!北邮开源20页249篇文献,包揽Text-to-Image Diffusion领域各种条件
【2月更文挑战第29天】北京邮电大学研究人员发表了一篇关于文本到图像扩散模型的综述论文,探讨了该技术在可控图像生成方面的最新进展。论文介绍了DDPMs基础理论,并详述了如何通过引入条件来提升生成图像的精确控制。研究者提出条件生成的三种类别,分析了核心理论机制,并创建了一个包含249篇相关文献的GitHub仓库,促进学术交流。尽管取得显著成就,但模型仍面临语义一致性、处理复杂文本描述和效率提升等挑战。论文链接:https://arxiv.org/abs/2403.04279
392 1
可控图像生成最新综述!北邮开源20页249篇文献,包揽Text-to-Image Diffusion领域各种条件
|
存储 安全 物联网
计算机网络的类型
本文介绍了网络的分类,涵盖按覆盖范围(PAN、LAN、MAN、WAN)、使用场景(公网、外网、内网)、传输介质(有线、无线)、特殊类型(VLAN、SAN、网络桥接、接入网)及拓扑结构(总线型、星型、树型、环型、网状型)和交换方式(电路交换、报文交换、分组交换)等,详细阐述了各类网络的特点和技术。
1071 2
|
安全 数据安全/隐私保护
什么是权限管理
什么是权限管理
409 0
什么是权限管理
|
云安全 安全 网络安全
Windows安全:构建稳固的防线,守护数字世界
随着数据保护法规的不断加强,Windows系统需要更好地满足法规遵从和合规性要求。未来,Windows系统将更加注重用户隐私保护和数据安全合规性方面的功能提升
376 54
|
机器学习/深度学习 数据采集 算法
图像识别中的局限性
【10月更文挑战第1天】
1198 0
|
存储 资源调度 Kubernetes
最新干货!如何深入集群调度与管理?
云时代的集群调度与管理怎么做?《深入集群:大型数据中心调度与管理》来支招!阿里云技术专家李雨前结合自己在云上集群调度与管理的多年实战经验,匠心发表此书,带你避坑、少踩雷。
最新干货!如何深入集群调度与管理?
|
安全 算法 Shell
ssh远程登录协议
ssh远程登录协议
|
算法 C语言
深度探索C语言中的do-while语句:理解、应用与性能优化
深度探索C语言中的do-while语句:理解、应用与性能优化
417 1
|
存储 编译器 C语言
C语言浮点型详解
C语言浮点型详解
550 0