Leetcode-89 Gray Code

C++版 - Leetcode 89: Gray Code解题报告 89. Gray Code 提交网址:https://leetcode.com/problems/gray-code/ Total Accepted:58554Total Submissions:161869Difficulty:Medium ACrate:36.2% The gray code is a binary numeral system wh... 阅读详情

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, print the sequence of gray code. A gray code sequence must begin with 0.

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

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

题意:给定一个bit位数返回一组灰度码,灰度码的意思是每两个相邻的码之间有且只有一位不同,比如00010,00110,这对码之间只有第三位不同。

分析:我们看到的这题的tag是backtracking,但是鄙人才疏学浅没有想到合适写法来写出这个回溯来,于是乎选择了最笨的办法,从最简单的1位开始想,

首先看当n = 1时的情况,非常简单的就可以得出来0,1;

当n = 2时,

           00

           01

           11

           10(此时还没有特别明显的规律);

当n = 3时,

  三位         后两位         十进制

          000             00                 0

          001             01                 1

          011             11                 3

          010             10                 2

          110             10                 2

          111             11                 3

          101             01                 1

          100             00                 0

此时我们可以看后两位,可以看出来,n=3时的灰度码有8个,前四个换算成十进制跟n=2时是一样的,而后四个的最后两位,换算成十进制就正好等于n=2时的倒序,其中的道理也很简单,我们既然得到了n=2的时候的灰度码序,那么再倒过来一遍,然后第一位补上1,就可以得出n=3的灰度码序。

代码实现:

 public class Solution
    {
        public List<int> GrayCode(int n)
        {
            List<int> ret = new List<int>();
            ret.Add(0);
            if (n == 0)
                return ret;
            ret.Add(1);
            for (int i = 2; i <= n; i++)
            {
                int tmp = (int)Math.Pow(2, i - 1);
                for (int j = tmp - 1; j >= 0; j--)
                {
                    ret.Add(ret[j] + tmp);
                }
            }
            return ret;
        }
    }


Label Studio保姆级教程:从零搭建医疗影像标注平台(DICOM格式支持) 本文提供基于Label Studio从零搭建医疗影像标注平台的保姆级教程。针对DICOM格式支持,详细阐述了环境部署、DICOM深度集成与预处理、团队协作权限管理以及质量审核闭环等核心环节,旨在构建一个高效、可落地的医疗AI数据标注解决方案,助力模型落地。 阅读详情

相关推荐

手把手教你为RK3588开发板适配RTL8211F网卡:设备树配置与PHY驱动匹配全流程

本文详细介绍了在RK3588开发板上适配RTL8211F千兆以太网PHY芯片的全流程,包括硬件原理图确认、设备树配置与PHY驱动匹配。通过实战案例和调试技巧,帮助开发者解决常见问题并优化网络性能,适用于嵌入式Linux开发中的Ethernet网络功能实现。

weixin_29184075的博客 533

agc031 C Differ by 1 Bit

传送门: https://atcoder.jp/contests/agc031/tasks/agc031_c 题解: 考虑把x xor y,问题变成从0->x xor y 因为2n−12^n-12n−1是奇数,所有x xor yx~xor~yx xor y有偶数个1无解。 由于我们任意交换一下位是没有关系的,所以考虑就是要构造出: 000…−&...

Cold_Chair的博客 495

配置oem 13c以监控管理数据库

使用oem13c监控数据库

雅冰石的专栏 1817

LeetCode89 Gray Code

LeetCode89 Gray Code 问题来源LeetCode89 问题描述 Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.

yanqinghe123的专栏 303

哨兵1号回波数据(L0级)包格式解析与成像参数提取

提取哨兵1号数据包的辅助数据、回波数据、定标数据。matlab代码在文末

3145

【ROS机器人系统】自主导航+YOLO目标检测+语音播报

完成自主导航功能,并在小车移动至指定地点的过程中,通过机器人的摄像头检测导航途中所见物体,识别出物体的种类(框选出不同物体并标注识别结果),结合科大讯飞语音合成模块对识别结果进行语音播报。..............................

南京大学软件工程硕士在读 2万+

Leetcode 89 gray code

Leetcode 89 gray code 题目描述 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。格雷编码序列必须以 0 开头。 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - 2 对于给定的 n,其格雷编码序列并不唯一。 例如,[0,2,...

qq_43271202的博客 216

QGis软件 —— 2、QGis加载在线地图两种方式(谷歌地图、天地图)

QGis软件 —— 2、QGis加载在线地图两种方式(谷歌地图)

1万+

LeetCode 89. Gray Code(格雷码生成)

题目来源:https://leetcode.com/problems/gray-code/ 问题描述 89.Gray Code Medium The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integern...

da_kao_la的博客 474

《多模态融合改进》目录一览 | 专栏介绍 :全网 第一份 完整的多模态改进教程,提供《多模态模型改进完整项目包》-开箱即用

在大家订阅专栏后,便可获得多模态模型改进完整项目包-开箱即用,方便简单

Limiiiing的博客 1万+

信号处理算法:自适应滤波_(8).自适应陷波器

自适应陷波器是一种强大的工具,广泛应用于电力系统、通信系统和生物医学信号处理等领域。通过自适应调整滤波器的参数,自适应陷波器可以在特定频率处形成陷波,从而有效地抑制噪声或干扰信号。常见的自适应算法包括LMS和RLS,它们各有优缺点,可以根据实际需求选择合适的算法。

2401_87715305的博客 1376

leetcode 89 Gray Code

leetcode 89 Gray Code Python C++

快递小可的博客 680

Python遥感图像处理应用篇(二十三):Python+GDAL 批量拼接图像

遥感影像拼接分多种不同的情况,比如比较常用的是两幅影像拼接或者多个图像合并为一个图像。GDAL中实现影像拼接的方式也有多种,比如常用可以通过numpy读取影像数组,并计算各个图像的numpy数组范围,合并多个numpy数组为一个最终的结果,并将其重新输出为一幅新的图像,即可实现图像的拼接。另外比较方便的一种方法便是使用GDAL.warp()函数方法。这个函数的功能很多,可以实现多种不同的目的,比如用于坐标系转换、投影变换、图像合并与镶嵌、地理范围裁剪、更改分辨率、矢量裁剪等多种功能。这些功能可以通过设置Op

soderayer的博客 4650

LeetCode --- 89. Gray Code

题目链接: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, print th

makuiyu的专栏 3704

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LeetCode89——Gray Code

通过二进制和递归两种方式求解n位生成格雷码(十进制表示)

上善若水 978

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Leetcode算法学习日志-89 Gray Code

Leetcode 89 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

Zarlove的博客 435

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Leetcode 89 Gray Code

Gray code 是指一个二进制数字系列,在这样的系列中,两个连续的数字有且只有一个二进制位是不同的。 给出一个非负整数n,代表二进制码的总位数,打印出gray code序列。Gray code的序列以0开始。 例如:给出n = 2,返回[0, 1, 3, 2]。其gray code 序列是: 00 - 0 01 - 1 11 - 3 10 - 2

(ง •̀_•́)ง 604

LeetCode 89: 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, print the sequence of gray code. A

SunliyMonkey 1226

LeetCode //C - 89. Gray Code

【代码】LeetCode //C - 89. Gray Code。

Made in Code 814

leetcode-89. Gray Code

题目描述(中等难度) 生成 n 位格雷码,所谓格雷码,就是连续的两个数字,只有一个 bit 位不同。 解法一 动态规划 按照动态规划或者说递归的思路去想,也就是解决了小问题,怎么解决大问题。 我们假设我们有了 n = 2 的解,然后考虑怎么得到 n = 3 的解。 n = 2 的解 00 - 0 10 - 2 11 - 3 01 - 1 如果再增加一位,无非是在最高位增加 0 或者 1,考虑先...

猫敷雪 456

leetcode No89. Gray Code

Question: 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, print the

Dufre 1083

【leetcode】89. Gray Code【java】

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, print the sequence of

陈善亮的博客 565

[LeetCode] 89. Gray Code

The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integernrepresenting the total number of bits in the code, print the sequence ...

dey8091的博客 105

【格雷码】LeetCode 89. Gray Code

LeetCode 89. Gray Code Solution1:我的答案 穷举法,比较笨 注意:牢记vector中find()函数的用法 class Solution { public: vector&amp;amp;amp;lt;int&amp;amp;amp;gt; grayCode(int n) { vector&amp;amp;amp;lt;int&amp;amp;amp;gt; res; res.push_back(

Allenlzcoder的博客 393

Gray Code LeetCode 89

题目: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, print the sequen...

baiyongjianai6584的博客 183

LeetCode89: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, print the sequence of gray

3207
上一篇: Leetcode-357- Count Numbers with Unique Digits C#
下一篇: VM安装CentOs7虚拟机后无法上网之解决方法
Lukia2015
博客等级 码龄13年 8粉丝 31原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值