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;
    }
};
面试题-手撕NMS(非极大值抑制) 非极大值抑制(Non-Maximum Suppression,NMS)是一种常用于目标检测和计算机视觉中的算法,用于去除重叠的边界框,保留最可能是真实目标的边界框。其核心就是对一组检测框,找出其中属于同一个类别且分数最高的那个框,然后把和这个框的IOU值大于阈值的那些框都删掉。 阅读详情

相关推荐

yakit 通过codec 解决RSA加密

靶场就不过多叙述,具体的可以看yakit系列的前几篇文章。yakit 热加载进行 RSA 加密如何显示想要的payloads。

Arkmornings的博客 2686

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

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

暮雨凉初透 608

SAP开发实战:用FI_DOCUMENT_CHANGE BAPI批量修改FB03凭证抬头和行项目文本(附完整ABAP代码)

本文详细介绍了如何使用SAP的FI_DOCUMENT_CHANGE BAPI批量修改FB03凭证抬头和行项目文本,提供完整的ABAP代码实现方案。通过该BAPI,开发者可以高效完成财务凭证的批量修改,确保数据一致性和审计合规性,显著提升SAP财务模块的运维效率。

weixin_29224589的博客 380

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

ubuntu18.04安装Nvidia显卡驱动后黑屏及网络、蓝牙驱动消失的解决方案

ubuntu18.04安装Nvidia显卡驱动后黑屏及网络、蓝牙驱动消失的解决方案

weixin_40735291的博客 6617

OCR开发实战:如何用PaddleOCR C++版实现本地文字识别(附模型文件处理技巧)

本文详细介绍了如何使用PaddleOCR C++版实现本地文字识别,包括环境搭建、工程编译、运行时优化等全流程。重点讲解了在VS2019/VS2022环境下配置PaddleOCR的CPU推理性能优化技巧,以及模型文件处理的实际应用方法,帮助开发者高效完成OCR本地化部署。

devops8pract的博客 334

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 ...

coordinate的博客 2082

从网格划分到结果解读:一次完整的子弹侵彻LS-DYNA仿真避坑指南

本文详细解析了子弹侵彻LS-DYNA仿真中的关键技术与避坑指南,涵盖网格优化、对称边界条件设置、沙漏控制参数调整及结果可信度验证。通过HyperMesh网格划分和LS-DYNA高级参数配置,帮助工程师提升仿真精度与效率,确保计算结果物理可信。

weixin_29816689的博客 406

Leetcode 89

回溯写到自闭;不想就删了; class Solution { public: vector<int> grayCode(int n) { vector<vector<int>> res; vector<int> add(n,0); DFS(res,add,n,0); ...

weixin_30629653的博客 157

【项目实战】TARA威胁分析和风险评估

TARA是信息安全领域中的一种风险评估方法,在汽车信息安全的上下文中,TARA分析特别重要,因为它可以帮助识别和管理与智能网联车辆相关的各种威胁,包括数据泄露、未经授权的访问、远程操控等。通过TARA分析,企业可以制定出针对性的安全目标、产品要求和产品安全声明,以确保汽车及其零部件符合相关的信息安全标准和法规。此外,TARA分析还可以与数字化建模系统结合,以更系统化和高效的方式进行风险评估和管理。

奶爸的编程之路,也就一周冷个三天 2709

VSCode PlantUML 插件超详细教程:从安装配置到实战绘图全流程

VSCode PlantUML 插件超详细教程:从安装配置到实战绘图全流程

weixin_69237381的博客 909

使用VS Code 来编写提交调试LeetCode或者力扣的代码(Java版本)

这里描述使用VS code 中的java的来调试,提交LeetCode或者力扣的代码。 主要是Code来调试,能够更轻量级,没有intellij 或者eclipse这里那么繁重。 总体三个步骤; 1,安装VS Code 2, 一键安装java 的开发环境 3,安装leetCode 插件和Debuger for java 插件 # 安装VS Code 安装最新版的VS Code 来,下载地址:htt...

fruitful1989的博客 1万+

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

leetcode 89 Gray Code

leetcode 89 Gray Code Python C++

快递小可的博客 679

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

LeetCode89——Gray Code

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

上善若水 977

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

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 810

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 1082

【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

陈善亮的博客 563

[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、付费专栏及课程。

余额充值