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

做法,先给result赋初始值为0,并给出一个k,k在每次外循环都会左移一位,然后在内循环中 和result中的每个元素(逆序,为什么要逆序?因为我们通过观察格雷码可以发现,n位格雷码,除了最左位,一头一尾是对称的,可以参考本文最下方的链接看别人的分析)作一个异或运算,并将结果放到result中。

   public static List<Integer> grayCode(int n) {
        List<Integer> result = new LinkedList<>();
        if (n >= 0) {      
            result.add(0);   
            int k = 1;        
            for (int i = 0; i < n; i++) {
                for (int j = result.size() - 1; j >= 0; j--) {  //逆序异或
                    result.add(result.get(j) ^ k);   // ^ <==> 异或   同为0, 异为1
                }   
                k <<= 1;
            }
        }
        return result;
    }

本题的思想参考于:http://blog.youkuaiyun.com/beiyeqingteng/article/details/7044471

上面的方法如果觉得太难想,还是用普通的方法

public String[] GrayCode(int n) {  

    // produce 2^n grade codes  
    String[] graycode = new String[(int) Math.pow(2, n)];  

    if (n == 1) {  
        graycode[0] = "0";  
        graycode[1] = "1";  
        return graycode;  
    }  

    String[] last = GrayCode(n - 1);  

    for (int i = 0; i < last.length; i++) {  
        graycode[i] = "0" + last[i];  
        graycode[graycode.length - 1 - i] = "1" + last[i];  
    }  

    return graycode;  
}  
光学相干断层扫描(OCT)在 妇科诊断 中的革命性应用 尽管OCT在妇科的应用提供了许多潜在的优势,但它仍然是一个相对新的技术,并不是所有医疗机构都有能力提供这种类型的成像服务。随着技术的发展和医生对OCT的熟悉程度的提高,预计OCT将在妇科和其他医学领域中扮演越来越重要的角色。早期发现和准确的分类是提高治疗成功率的关键。虽然这是一个相对较新的应用领域,但OCT的非侵入性和高分辨率成像能力可能提供有价值的信息,以提高辅助生殖技术的成功率。:光学相干断层扫描(OCT)在阴道和外阴组织的评估中的应用正在逐渐展开,提供了一种新的方法来诊断和监测各种妇科疾病。 阅读详情

相关推荐

WEBGL Shader零基础教程(四):片元着色器核心逻辑

本文系统介绍了WebGL渲染管线中片元着色器的核心概念与应用。主要内容包括:1)片元着色器在渲染管线中的定位与作用,作为逐像素处理单元接收插值数据并计算最终颜色;2)输入数据类型与使用规则,重点讲解varying变量和uniform变量的应用场景;3)颜色计算的核心逻辑,涵盖基础色、渐变、纹理采样和颜色混合;4)进阶特效实现与性能优化技巧,包括交互效果、内置函数组合和GPU计算优化;5)通过综合案例演示了纹理、波纹与交互效果的整合实现。文章提供了完整的代码示例和调试建议,为后续学习光照模型、后期处理等高级渲

分享知识,拥抱变化 1374

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

ANPC三电平拓扑:两种调制方式解析

ANPC结构是基于“I”型NPC结构(下面讲NPC如不特殊说明,均指的是“I”型结构NPC)发展出来的一种改进型。我们在讲NPC结构的时候会谈到他的一个缺点就是损耗分布不均匀,会导致温升不均衡,进而限制模块的功率输出能力。而ANPC结构由于多了两个开关管以及相应的两个中性点换流回路(如下图),可以更加灵活任意的配置开关管可以使损耗没有那么的不均衡,从而能一定程度上提高模块的输出功率。ANPC拓扑结构图以及增加的环流回路。

IOTBOT的博客 9482

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

立创EDA手动拼板实战:当自带功能不够用,如何用‘复制粘贴’搞定异形拼版与特殊布局

本文详细介绍了立创EDA手动拼板技术在异形拼版与特殊布局中的应用实战。通过CTRL+SHIFT+V等核心操作技巧,解决混合尺寸拼板、特殊间距需求等复杂场景,提升PCB设计效率与灵活性,特别适合需要高精度控制的射频电路和医疗设备项目。

weixin_31967485的博客 386

单细胞RNA测序数据分析与可视化:从基础原理到高级应用

单细胞RNA测序(scRNA-seq)技术通过解析单个细胞的基因表达谱,显著提升了我们对细胞异质性的理解。本文详细介绍了scRNA-seq数据分析的完整流程,包括数据预处理、质量控制、降维、聚类、差异表达分析、细胞类型注释、细胞周期分析、轨迹分析、RNA速率分析、基因集富集分析、细胞通讯分析、批次效应校正以及空间转录组学整合。文章还展示了scrna_toolkit包在简化分析流程和增强数据可视化方面的应用。通过这一工具,研究者可以更高效地处理单细胞数据,揭示复杂的生物学信息。未来,该工具将进一步整合多组学分

2301_80338712的博客 4129

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

RAW、RGB、YUV图像格式的采样与存储原理详解

本文详细解析了RAW、RGB和YUV三种核心图像格式的采样与存储原理。RAW是传感器原始数据,信息完整但需后期处理;RGB直接对应显示,直观但数据量大;YUV通过亮色分离与色度下采样,在保证观感的前提下大幅提升传输与存储效率,是视频编码与流媒体的主流格式。理解其差异是图像处理与视频开发的基础。

e4f5g6h7的博客 516

leetcode 89 Gray Code

leetcode 89 Gray Code Python C++

快递小可的博客 679

第9集:AI 驱动的智能日志分析!用 RAG 实现秒级日志检索与异常检测

文章摘要:AI驱动的智能日志分析系统 本文介绍了一个基于RAG(检索增强生成)技术的智能日志分析解决方案,旨在解决传统日志系统中的三大痛点:机械的关键词搜索、缺乏语义理解、告警与日志割裂。系统通过以下步骤实现: 环境准备:安装必要的Python依赖库,包括日志处理、Elasticsearch/Loki连接和时序异常检测工具。 核心设计:构建日志处理流水线,分为日志采集、向量化存储和AI检索分析三个阶段,创建专门的日志分析Agent。 技术实现:使用ChromaDB存储日志向量,通过HuggingFace E

qq_2937655951的博客 79

移位寄存器——用Verilog实现串入串出、串并和并串转换并进行Modelsim仿真

用Verilog实现串并转换 首先我们先要了并串和串并转换的原理 并串转换:先将八位数据暂存于一个八位寄存器器中,然后左移输出到一位输出端口,这里可以通过一个“移位”来实现,相当于实现了移位寄存器的功能。 串并转换:新输入的位值成为原来数据的最低位,将原来数据的最高位舍去,这里可以通过一个简单的“连接符”来实现。 我们这里采用两种方式来进行串并转换 一:lsb优先 Least Significant Bit,最低比特,最低位优先 二:msb优先 Most Significant Bit,最高比特,

H19981118的博客 2万+

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

基于YOLOv10的机器人抓取引导系统:从物体检测到精准抓取

python# 数据标注工具简化版import os"""启动标注工具""""""标注单张图像"""print(f"标注图像: {os.path.basename(img_path)}")print("请在图像上绘制抓取矩形框...")# 这里应该实现交互式标注界面# 简化版本直接返回示例标注# 示例标注数据"""保存标注结果"""# 保存为YOLO格式# 转换为YOLO格式。

YOLO 377

LeetCode89——Gray Code

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

上善若水 977

jre9下载 jdk9

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

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

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

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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

android 蓝牙通讯demo

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

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

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

【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

斗code、年华 416

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

杨领well的专栏 342
上一篇: 大整数相乘/相加
下一篇: 不用加减乘除实现加法
sipherhern
博客等级 码龄11年 2粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值