两种思路,详情请参考新博文地址:[leetcode]Gray Code
http://oj.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.
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.
格雷码, 提示中称:[0,2,3,1]也是一组合法的格雷码,把我彻底搞糊涂了,既然如此,为什么要返回[0,1,3,2]而不能返回[0,2,3,1]呢?后来手贱去百度了一下格雷码。尼玛....
看下格雷码的特征:
n == 1时,[0,1]
n == 2时,[00,01,11,10]
n == 3时,[000,001,011,010,110,111,101,100]
1位格雷码有两个码字
(n+1)位格雷码中的前2n个码字等于n位格雷码的码字,按顺序书写,加前缀0
(n+1)位格雷码中的后2n个码字等于n位格雷码的码字,按逆序书写,加前缀1
(n+1)位格雷码中的前2n个码字等于n位格雷码的码字,按顺序书写,加前缀0
(n+1)位格雷码中的后2n个码字等于n位格雷码的码字,按逆序书写,加前缀1
前后两部分的后n - 1位是对称的。
知道定义之后,代码就很简单了
public ArrayList<Integer> grayCode(int n) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0); if (n <= 0) { if(n == 0)return list; else return new ArrayList<Integer>(); } list.add(1); for (int i = 1; i < n; i++) { ArrayList<Integer> newList = new ArrayList<Integer>(); for (int j = 0; j < list.size(); j++) { newList.add(list.get(j)); } for (int k = list.size() - 1; k >= 0; k--) { newList.add((int) Math.pow(2, i) + list.get(k)); } list = newList; } return list; }
相关推荐
leetcode-codelist 食用指南(持续更新中...) 文件名说明:leetcode-题号-题目类型 目前更新重点-每日一题 tips:闲暇时间刷刷题,面试之前不用慌。 常见思路整理 前缀和 327 区间和的个数 差分 动态规划 845 最长...
leetcode题库 my-leetcode-code leetcode代码管理 此代码列表为本人在leetcode目前已解决题库代码 代码环境为python3
LeetCode高尔夫球 LeetCode遇到CodeGolf
leetcode题库 Leetcode_code_exercise 我的代码练习题库
:page_facing_up: :chart_increasing: LeetCode刷题的源代码文件 个人LeetCode主页 原始结构 所有刷过的例题的二进制都放在了源目录下面,文本文件是代码测试文件夹。 以_结尾的表示没有做出来 数字表示题号 中文...
上班时间刷leetcode LeetCode 记录自己的刷题思路,js代码不在维护,以后以py代码为主。 规范如下: 文件名格式[题号][题目名字],文件内容:题目描述、关键思路、解题代码、tag、复杂度 刷题相关思考 刷题开始于...
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
vs code LeetCode 插件
反转单向链表(com.spring.leetcode.Code32) 2020/3/3 合并排序的数组(com.spring.leetcode.Code33) 2020/3/4 腐烂的橘子(com.spring.leetcode.Code34) 2020/3/5 分糖果(com.spring.leetcode.Code35) 2020/3/...
LeetCode_code
蓝桥杯leetcode CodeJam Code Jam related Q&A Online Judge: 在线的判题系统。用户可以在线提交程序多种程序(如 C、C++、Pascal)源代码,系统对源代码进行编译和执行,并通过预先设计的测试数据来检验程序源代码的...
Leetcode-code project 本项目包含 Leetcode 所有题目,目前项目处于更新阶段!欢迎关注 Tips: 本文档所有题目为力扣中国站点的题目,与国外力扣题目可能有一点分歧! 如果您需要使用,请使用力扣中国官网 刷题网站...
leetcode题库 LeetCode习题代码 本仓库为在LeetCode网站所做的习题代码记录
颜色分类leetcode 格雷码结构光重建 使用投影仪和相机重建 3D 场景 这个 repo 使用投影仪和相机来扫描场景并创建 3D 模型。 为了实现这一点,结构光图案被投射到场景上。 这是用相机捕获以确定密集对应关系。 该存储...
华为leetcode 动机:找工作 该项目是为我在 NUAA 学习期间的工作面试而创建的,并为 2017-2018 年的考试和学习而创建。 介绍: 这是我在 NUAA 时为面试和学习而创建的项目; 内容 现在编码器 公司面试 leetcode 学 ...
leetcode中文版
leetcode卡 Code #纯属leetcode打卡
大佬的leetcode刷题笔记(c++版本)
Leetcode-Bot 用于编程公会以跟踪其编码统计数据的不和谐机器人。 !lc 寄存器!lc 寄存器!lc 发布 <leetcode> !lc 发布!lc 统计!lc 帖子!lc 自我统计 去做 用户应该能够自己注册,他们可以提供用户名或机器人从他们的...
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip