当前位置:网站首页>LeetCode_格雷编码_中等_89.格雷编码
LeetCode_格雷编码_中等_89.格雷编码
2022-07-06 11:32:00 【小城老街】
1.题目
n 位格雷码序列是一个由 2n 个整数组成的序列,其中:
① 每个整数都在范围 [0, 2n - 1] 内(含 0 和 2n - 1)
② 第一个整数是 0
③ 一个整数在序列中出现不超过一次
④ 每对相邻整数的二进制表示恰好一位不同 ,且第一个和最后一个整数的二进制表示恰好一位不同
给你一个整数 n ,返回任一有效的 n 位格雷码序列 。
示例 1:
输入:n = 2
输出:[0,1,3,2]
解释:
[0,1,3,2] 的二进制表示是 [00,01,11,10] 。
- 00 和 01 有一位不同
- 01 和 11 有一位不同
- 11 和 10 有一位不同
- 10 和 00 有一位不同
[0,2,3,1] 也是一个有效的格雷码序列,其二进制表示是 [00,10,11,01] 。
- 00 和 10 有一位不同
- 10 和 11 有一位不同
- 11 和 01 有一位不同
- 01 和 00 有一位不同
示例 2:
输入:n = 1
输出:[0,1]
提示:
1 <= n <= 16
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/gray-code
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2.思路
思路参考本题官方题解。
(1)对称生成
(2)二进制数转格雷码
3.代码实现(Java)
//思路1————对称生成
class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<Integer>();
res.add(0);
for (int i = 1; i <= n; i++) {
int m = res.size();
for (int j = m - 1; j >= 0; j--) {
res.add(res.get(j) | (1 << (i - 1)));
}
}
return res;
}
}
//思路2————二进制数转格雷码
class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<>();
for (int i = 0; i < (1 << n); i++) {
res.add((i >> 1) ^ i);
}
return res;
}
}
边栏推荐
- Druid database connection pool details
- Synchronous development of business and application: strategic suggestions for application modernization
- Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
- tensorflow和torch代码验证cuda是否安装成功
- RT-Thread 组件 FinSH 使用时遇到的问题
- [translation] a GPU approach to particle physics
- R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
- ROS自定义消息发布订阅示例
- R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
- English topic assignment (25)
猜你喜欢

How to improve website weight

学习探索-使用伪元素清除浮动元素造成的高度坍塌

Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease

Detailed idea and code implementation of infix expression to suffix expression

Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars

思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理

Digital "new" operation and maintenance of energy industry
Benefit a lot, Android interview questions

RT-Thread 组件 FinSH 使用时遇到的问题

A method of removing text blur based on pixel repair
随机推荐
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
It's super detailed in history. It's too late for you to read this information if you want to find a job
冒烟测试怎么做
Interface test tool - postman
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
C language daily practice - day 22: Zero foundation learning dynamic planning
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
Multithreading Basics: basic concepts of threads and creation of threads
USB host driver - UVC swap
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
受益匪浅,安卓面试问题
R语言使用dt函数生成t分布密度函数数据、使用plot函数可视化t分布密度函数数据(t Distribution)
Word如何显示修改痕迹
R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
通俗的讲解,带你入门协程
Precautions for binding shortcut keys of QPushButton
Looting iii[post sequence traversal and backtracking + dynamic planning]
Pytorch common loss function
pytorch常见损失函数
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up