当前位置:网站首页>LeetCode_ Gray code_ Medium_ 89. Gray code
LeetCode_ Gray code_ Medium_ 89. Gray code
2022-07-06 19:29:00 【Old street of small town】
1. subject
n Bit gray code sequence is a sequence composed of 2n A sequence of integers , among :
① Every integer is in the range [0, 2n - 1] Inside ( contain 0 and 2n - 1)
② The first integer is 0
③ An integer does not appear more than once in the sequence
④ The binary representation of each pair of adjacent integers is exactly one bit different , And the binary representation of the first and last integers is exactly one bit different
Give you an integer n , Returns any valid n Bit gray code sequence .
Example 1:
Input :n = 2
Output :[0,1,3,2]
explain :
[0,1,3,2] The binary representation of is [00,01,11,10] .
- 00 and 01 There is a difference
- 01 and 11 There is a difference
- 11 and 10 There is a difference
- 10 and 00 There is a difference
[0,2,3,1] It is also an effective gray code sequence , Its binary representation is [00,10,11,01] .
- 00 and 10 There is a difference
- 10 and 11 There is a difference
- 11 and 01 There is a difference
- 01 and 00 There is a difference
Example 2:
Input :n = 1
Output :[0,1]
Tips :
1 <= n <= 16
source : Power button (LeetCode)
link :https://leetcode.cn/problems/gray-code
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Ideas
Train of thought reference Official solution to this problem .
(1) Symmetrically generated
(2) Binary to gray code
3. Code implementation (Java)
// Ideas 1———— Symmetrically generated
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;
}
}
// Ideas 2———— Binary to gray code
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;
}
}
边栏推荐
- R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
- Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
- 中缀表达式转后缀表达式详细思路及代码实现
- ACTF 2022圆满落幕,0ops战队二连冠!!
- Problems encountered in using RT thread component fish
- 倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
- 凤凰架构2——访问远程服务
- In 50W, what have I done right?
- CPU负载很低,loadavg很高处理方法
- Use of deg2rad and rad2deg functions in MATLAB
猜你喜欢

全套教学资料,阿里快手拼多多等7家大厂Android面试真题

Simple understanding of MySQL database

系统性详解Redis操作Hash类型数据(带源码分析及测试结果)

10 schemes to ensure interface data security

Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting

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

JDBC details

零基础入门PolarDB-X:搭建高可用系统并联动数据大屏

ACTF 2022圆满落幕,0ops战队二连冠!!

spark基础-scala
随机推荐
PMP practice once a day | don't get lost in the exam -7.6
Spark foundation -scala
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
ZABBIX proxy server and ZABBIX SNMP monitoring
Tensorflow2.0 自定义训练的方式求解函数系数
学习探索-使用伪元素清除浮动元素造成的高度坍塌
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
A popular explanation will help you get started
[pytorch] yolov5 train your own data set
DaGAN论文解读
Tensorflow and torch code verify whether CUDA is successfully installed
【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
Mysql Information Schema 學習(一)--通用錶
通俗的讲解,带你入门协程
Mysql Information Schema 学习(二)--Innodb表
深入分析,Android面试真题解析火爆全网
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
反射及在运用过程中出现的IllegalAccessException异常