Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Leetcode 题目解析之 Gray Code

Leetcode 题目解析之 Gray Code

原创
作者头像
ruochen
发布于 2022-03-08 02:24:12
发布于 2022-03-08 02:24:12
1.5K00
代码可运行
举报
运行总次数:0
代码可运行

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:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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.

将数字i,转换成格雷码的公式是:

image.png
image.png
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    public List<Integer> grayCode(int n) {
        List<Integer> rt = new ArrayList<Integer>();
        if (n < 0) {
            return rt;
        }
        for (int i = 0; i < Math.pow(2, n); i++) {
            rt.add((i >> 1) ^ i);
        }
        return rt;
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
​LeetCode刷题实战89:格雷编码
https://leetcode-cn.com/problems/gray-code/
程序员小猿
2021/01/19
4120
leetcode: 89. Gray Code
Problem # 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
JNingWei
2018/09/27
3650
LeetCode笔记:89. Gray Code
格雷码是很经典的问题,规则其实很简单,在二进制形式下,任何响铃的两个值的二进制表示形式只有一位是不同的,我们可以找找规律。
Cloudox
2021/11/23
1510
C++版 - Leetcode 89: Gray Code解题报告
提交网址: https://leetcode.com/problems/gray-code/
Enjoy233
2019/03/05
7610
C++版 - Leetcode 89: Gray Code解题报告
LeetCode 0089 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.
Reck Zhang
2021/08/11
2090
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 gray code sequence must begin with 0. For exam
triplebee
2018/01/12
4970
Leetcode: Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit.
卡尔曼和玻尔兹曼谁曼
2019/01/22
4020
打卡群刷题总结0730——格雷编码
1、每次新增的数num2[j] = 2^(i-1) + num[-j-1],其中,i为二进制数字的位数,j为数组的第几个数。
木又AI帮
2020/08/04
3460
Leetcode 题目解析之 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
ruochen
2022/01/10
1.3K0
Leetcode 题目解析之 Plus One
Given a non-negative number represented as an array of digits, plus one to the number.
ruochen
2022/01/09
1.3K0
Leetcode 题目解析之 Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are+, - and *.
ruochen
2022/02/14
1.3K0
Leetcode 题目解析之 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
ruochen
2022/01/10
1.3K0
Leetcode 题目解析之 House Robber II
Note: This is an extension of House Robber.
ruochen
2022/02/15
1.4K0
Leetcode 题目解析之 Basic Calculator
Implement a basic calculator to evaluate a simple expression string.
ruochen
2022/01/15
1.2K0
镜像反转重新定义动态规划转移方程--格雷编码|Java 刷题打卡
Gn0+Cn1+Cn2+⋯+Cn1G_{n}^{0}+C_{n}^{1}+C_{n}^{2}+\cdots+C_{n}^{1}Gn0+Cn1+Cn2+⋯+Cn1
啵啵肠
2023/11/29
1650
LeetCode 题目解答——Medium 部分(上)
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
6370
LeetCode 题目解答——Medium 部分(上)
C#版 - Leetcode 89. 格雷编码 - 题解
在线提交: https://leetcode-cn.com/problems/gray-code/
Enjoy233
2019/03/05
7400
C#版 - Leetcode 89. 格雷编码 - 题解
LintCode 格雷编码题目分析代码
给定一个非负整数 n ,表示该代码中所有二进制的总数,请找出其格雷编码顺序。一个格雷编码顺序必须以 0 开始,并覆盖所有的 2n 个整数。
desperate633
2018/08/22
3100
LeetCode 89. 格雷编码
给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。格雷编码序列必须以 0 开头。
Michael阿明
2021/02/20
2450
LeetCode 89. 格雷编码
☆打卡算法☆LeetCode 89、格雷编码 算法解析
链接:89. 格雷编码 - 力扣(LeetCode) (leetcode-cn.com)
恬静的小魔龙
2022/08/07
4330
☆打卡算法☆LeetCode 89、格雷编码  算法解析
相关推荐
​LeetCode刷题实战89:格雷编码
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验