Gray Code [leetcode]

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

第一种方法是直接排列

以二进制为0值的格雷码为第零项,第一项改变最右边的位元,第二项改变右起第一个为1的位元的左边位元,第三、四项方法同第一、二项,如此反复,即可排列出n个位元的格雷码。

    vector<int> grayCode(int n) {
        vector<int> res;
        res.push_back(0);
        if (n == 0) return res;
        res.push_back(1);
        int total = 1 << n;
        for (int i = 2;i < total; i+=2)
        {
            int j = 1;
            while((res.back() & j) == 0)
                j <<= 1;
            j <<= 1;
            res.push_back(res.back() ^ j);
            res.push_back(res.back() ^ 1);
        }
        return res;
    }

第二种方法是镜射排列

    vector<int> grayCode(int n) {
        vector<int> res;
        if (n == 0)
        {
            res.push_back(0);
            return res;
        }
        res.push_back(0);
        res.push_back(1);
        for (int i = 2; i <= n; i++)
        {
            int s = 1 << (i-1);
            for (int j = res.size() - 1; j >= 0; j--)
            {
                res.push_back(res[j] + s);
            }
        }
        return res;
    }


Win10右键新建菜单丢了记事本?别慌!5分钟教你用注册表找回来(附权限设置避坑指南) 本文详细指导如何通过注册表编辑器恢复Win10右键新建菜单中丢失的记事本选项,包括重建键值结构、配置ShellNew项及设置权限等关键步骤。特别提供权限避坑指南,确保修改持久有效,帮助用户快速解决这一常见但困扰的问题。 阅读详情

相关推荐

T-Box功能详解及应用

T - BOX,其全称为 Telematics Box,作为远程 / 车载通信模块,是一款集车身网络与无线通讯功能于一体的智能终端设备。它充当了多媒体车机与 TSP(汽车远程服务提供商)以及互联网之间的无线路由器角色,为用户创造了远程访问的可能。用户借助T - BOX能够对车辆配备的各个 ECU(电子控制单元)进行远程操控,包括远程启动或停止发动机开启或关闭空调天窗后备箱门车门,以及实现闪灯鸣笛等功能。

wgl110110的博客 5640

leetcode腾讯精选练习50(26)——格雷编码

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

优快云zhwk的博客 384

SAP交货单过账避坑指南:序列号处理、Buffer清理与BAPI提交回滚的那些事儿

本文深入解析SAP交货单过账中的序列号处理、Buffer清理与BAPI提交回滚等关键问题,提供WS_DELIVERY_UPDATE和BAPI_OUTB_DELIVERY_CONFIRM_DEC的实战优化方案。通过详细的技术分析和最佳实践,帮助开发者避免常见陷阱,提升交货单处理的稳定性和效率。

weixin_31155097的博客 431

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

告别OLE!Delphi操作Excel的3种轻量级方案横向评测(FlexCel/XLSReadWriteII/LibXL)

本文深入评测了Delphi操作Excel的三种轻量级方案:FlexCel、XLSReadWriteII和LibXL。通过技术架构、API设计、性能测试和实战案例的全面对比,帮助开发者选择最适合的工具,提升Excel文件处理效率,告别传统OLE方案的依赖和性能问题。

onion的博客 929

51单片机AHT20-21源代码

传感器输出经过 标定的数字信号,标准 I 2 C 格式。AHT20 配有一个全新设计的 ASIC专用芯片、一 个经过改进的MEMS半导体电容式湿度传感元件和一个标准的片上温度传感元件,其 性能已经大大提升甚至超出了前一代传感器的可靠性水平,新一代温湿度传感器, 经过改进使其在恶劣环境下的性能更稳定。但是现在网上的大多数原码都是针对stm32等的这种多位单片机,而我们都知道51单片机是8位单片机,无法对浮点数进行运算,对32位的整数计算有时候也不是很准确,所以我对网上stm32的例程进行了修改。...

wd0710的博客 4547

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

最新Codex接入DeepSeek官方API教程(无需cc-switch)

最新Codex接入DeepSeek官方API教程(无需cc-switch)

猿小白的博客 939

Leetcode Gray Code

Leetcode Gray Code,本题主要使用回溯法进行解决,通过一定的规则对二叉树进行遍历,并把值添加入结果集合中,就可以达到目的。要关代友与测试如下:#include<iostream> #include<vector>/** * We used the backtrack method to solve this problem. This problem can be * consi

Ttssxuan 486

OpenClaw实战系列04:Jira/Confluence深度集成实战指南

汇总 Confluence 空间 TEAM 下所有关于“性能优化”的文档,输出要点自然语言创建和查询 Jira 任务,无需离开聊天工具基于 Confluence 的私有知识库问答,让 AI 真正理解团队文档Jira 与 Confluence 联动,打通项目管理与知识管理定时同步与通知,自动化团队信息流转至此,你的 OpenClaw AI 助理已经不仅是一个聊天机器人,而是真正融入团队工作流的“智能项目管理助手”。下一期我们将带来OpenClaw 自定义技能开发实战。

PeterPan的博客 572

Floyd (弗洛伊德)算法简述

Floyd在1962年由Robert Floyd以其当前公认的形式出版。算法作为三个嵌套for循环的现代公式首先由Peter Ingerman在1962年描述。Floyd 算法是解决图论问题的比较经典的算法,是解决给定的加权图中顶点间的最短路径的一种算法,可以正确处理有向图的最短路径问题。

慕羽★的博客 2万+

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

【Bluedroid】BLE连接过程详解

Bluedroid BLE的链接过程包括扫描、连接和通信三个基本步骤。通过这些步骤,中央设备和外围设备之间可以建立起稳定、高效的通信链路,实现数据传输和控制操作。

byte轻骑兵的技术小窝 1876

[LeetCode]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 se

cjllife的专栏 493

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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

LeetCode题目 Gray Code 的解法与疑问

vector grayCode(int n) { vector Bits(1,0); for(int GrayDigit = 0;GrayDigit { int count = Bits.size(); while(count!=0) Bits.push_back(Bits[--count]|(1 /* vector::iterator iter = Bits.end();

catsanddogsman的专栏 731

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

Leetcode089 Gray Code(构造)

题目连接:Leetcode089 Gray Code解题思路:假设n-1的序列为a,b,c,d,那么n的序列即为a,b,c,d,d+2^n,c+2^n,b+2^n,a+2^n。class Solution { public: vector&lt;int&gt; grayCode(int n) { vector&lt;int&gt; ans; ans.push_back(0); ...

不慌不忙、不急不躁 218

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

LeetCode Gray Code

格雷码表示在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同。现给定二进制码的位数,要求打印出格雷码序列。

DRFish 1147

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

qq_35455503的博客 173

leetcode笔记:Gray Code(2016腾讯软件开发笔试题)

格雷码是一种数字编码,其中的所有相邻整数在它们的二进制数字表示中只有一个数字不同。现要求打印出二进制比特数是n的所有格雷码。例如,n=2,所有的格雷码是0,1,3,2(对应二进制表示是00,01,11,10)。

liyuefeilong的专栏 3708

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——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 cod

白马笑西风的专栏 619

LeetCode: 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 o

Tingmei的专栏 5548

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

给永远比拿愉快 1417

LeetCode 89. Gray Code (Java版; Medium)

welcome to my blog LeetCode Top Interview Questions 89. Gray Code (Java版; Medium) 题目描述 The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negativ...

littlehaes的博客 233

[Leetcode]Gray_code

问题:产生n位元的所有格雷码。 格雷码(Gray Code)是一个数列集合,每个数使用二进位来表示,假设使用n位元来表示每个数字,任两个数之间只有一个位元值不同。 例如以下为3位元的格雷码: 000 001 011 010 110 111 101 100 。 如果要产生n位元的格雷码,那么格雷码的个数为2^n. 假设原始的值从0开始,格雷码产生的规律是:第一步,改变最右边的

499
上一篇: Recover Binary Search Tree [leetcode]
下一篇: Binary Tree Inorder Traversal [leetcode] 非递归的三种解法
圆形毕露
博客等级 码龄17年 20粉丝 103原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值