LeetCode89. 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... 阅读详情

题目:

https://leetcode.com/problems/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 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

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

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

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

Subscribe to see which companies asked this question.

思路:

嗯,腾讯的笔试题好像也出过。
题目是在一组数的编码中,若任意不相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code),请编写一个函数,使用递归的方法生成N位的格雷码。给定一个整数n,请返回n位的格雷码,顺序从0开始。
以3为例
0 0 0
0 0 1
0 1 1
0 1 0 ↑
1 1 0 ↓
1 1 1
1 0 1
1 0 0
从中我们可以发现规律,从分界处上下两部分不看第一位,是对称的。
而上面那部分是n-1的格雷码。下半部分是上半部分结果逆序再在最高位加1的结果。

代码:

class Solution {
public:
    vector<int> grayCode(int n) {
       if(n==0){
           vector<int> res;
           res.push_back(0);
           return res;
       }
       vector<int> preRes=grayCode(n-1);
       vector<int> res(preRes);
       int cur;
       for(int i=preRes.size()-1;i>=0;i--){
           cur=preRes[i]+pow(2,n-1);
           res.push_back(cur);
       }
       return res;
    }
};
WorkBuddy数据与隐私设置全解析:从缓存目录到训练授权 AI助手的数据安全与隐私保护是用户必须关注的核心问题。数据存储位置、传输链路加密、对话历史保留周期以及训练数据授权等环节,共同决定了信息的安全边界。合理配置本地缓存目录、关闭训练数据授权、设置对话历史保留周期,能有效降低数据泄露风险。在团队协作场景中,权限隔离与第三方服务授权管理同样关键。本文以WorkBuddy为例,系统梳理数据流转链路与隐私设置项,提供从零配置稳妥隐私方案的实操指南,帮助用户在个人使用与企业部署中守住安全底线。 阅读详情

相关推荐

STM32F407实现LwIP的MQTT客户端并连接到服务器

这个是用LwIP协议栈中自带的MQTT客户端API写成的,代码注释很详细,附带有服务端的展示。LwIP版本是2.1.2.

wait_for_STM32的博客 6416

【LeetCode】89. 格雷编码 结题报告 (C++)

原题地址:https://leetcode-cn.com/problems/gray-code/description/ 题目描述: 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。格雷编码序列必须以 0 开头。 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 ...

暮雨凉初透 608

S32K144后门密钥实战:如何用JLINK脚本绕过复位陷阱更新加密固件

本文详细介绍了在S32K144微控制器上,如何利用JLINK脚本绕过调试器默认复位陷阱,实现通过后门密钥安全更新加密固件的实战方案。针对汽车电子现场维护中常见的“解锁-复位-锁定”循环问题,文章提供了完整的JLinkScript定制方法、批处理工具及故障排除指南,帮助工程师高效完成固件升级。

weixin_29175469的博客 160

LeetCode 89 格雷编码

题目描述 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。 格雷编码序列必须以 0 开头。 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - 2 对于给定的 n,其格雷编码序列并不唯一。 例如,[0,2,3,1] 也是一个有效的格雷编码序列。 00 - 0 10 - 2 11 - 3 01 - 1 示例 2: 输入:

yangyangcome的博客 191

人大金仓的‘影子表’sys_user:搞懂Schema优先级,告别字段不存在的报错

本文深入解析人大金仓数据库中‘影子表’sys_user的Schema优先级问题,帮助开发者解决字段不存在的报错。通过分析Schema搜索路径机制,提供四种解决方案,包括连接时指定Schema、动态调整搜索路径、永久修改用户设置和使用完全限定表名,确保查询准确无误。

weixin_30329623的博客 608

基因家族快速鉴定指南:Windows平台下的HMMER实战解析

本文详细解析了在Windows平台下使用HMMER工具进行基因家族快速鉴定的完整实战流程。通过手把手指导,从软件获取、数据准备到模型构建与序列搜索,帮助生物信息学新手无需Linux环境,即可高效完成bZIP转录因子等家族的鉴定工作,大幅降低分析门槛。

weixin_29255731的博客 468

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的专栏 3706

Unity雷达图效果实现

unity雷达图源码

a604263644的博客 1032

LeetCode 89. 格雷编码

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

Ailecccc的博客 236

Eclipse邂逅Deepseek:本地化部署全攻略

通过前面一系列的步骤,我们成功地在 Eclipse 中完成了 Deepseek 的本地化部署,并且对部署后的功能进行了测试和优化,还解决了可能遇到的一些常见问题。这一过程不仅让我们体验到了将强大的大语言模型与高效的开发环境相结合的魅力,也为我们在软件开发过程中带来了诸多便利。本地化部署 Deepseek 到 Eclipse,使得我们在代码编写过程中能够获得智能的代码补全和精准的问答支持,大大提高了开发效率。同时,本地化部署保障了数据的安全性和隐私性,避免了数据在传输过程中可能面临的风险。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 4569

【LLC快速上手系列】如何在LLC谐振里优雅地调死区时间?

用示波器死盯Vds波形,找到ZVS临界点;别听“经验值”,亲自调才准;死区时间并非越短越好,也不是越长越稳,而是要“刚刚好”;用热成像看MOSFET温升变化,ZVS调好了,温度马上掉2~5℃;轻载难ZVS?适当提升频率 or 降低Lr可破!

weixin_43199439的博客 509

LeetCode(89)GrayCode

题目如下: 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 sequenc

BUILD 2721

【Simulink教程案例9】基于simulink的BPSK,QPSK调制解调通信系统建模与仿真——使用simulink自带模块设计

BPSK (Binary Phase Shift Keying)-------二进制相移键控。是把模拟信号转换成数据值的转换方式之一,利用偏离相位的复数波浪组合来表现信息键控移相方式。BPSK使用了基准的正弦波和相位反转的波浪,使一方为0,另一方为1,从而可以同时传送接受2值(1比特)的信息。BPSK的映射关系如下所示:BPSK调制过程将输入的1010二进制数据中的0都转化为-1,使得非极性码变为极性码。

FPGA/MATLAB学习教程/源码/项目合作开发 3490

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

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

leetcode 89 Gray Code

leetcode 89 Gray Code Python C++

快递小可的博客 681

LeetCode89——Gray Code

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

上善若水 979

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的博客 436

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

(ง •̀_•́)ง 606

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 1227

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 1084

【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
上一篇: C/C++知识点整理(3)
下一篇: LeetCode210. Course Schedule II
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址