当前位置:网站首页>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;
}
}
边栏推荐
- Take a look at how cabloyjs workflow engine implements activiti boundary events
- 10 schemes to ensure interface data security
- 安装Mysql报错:Could not create or access the registry key needed for the...
- CPU负载很低,loadavg很高处理方法
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
- 谷粒商城--分布式高级篇P129~P339(完结)
- usb host 驱动 - UVC 掉包
- 如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
- 史上超级详细,想找工作的你还不看这份资料就晚了
猜你喜欢

Detailed idea and code implementation of infix expression to suffix expression

中缀表达式转后缀表达式详细思路及代码实现

【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
Interview assault 63: how to remove duplication in MySQL?
![Looting iii[post sequence traversal and backtracking + dynamic planning]](/img/9b/e9eeed138e46afdeed340bf2629ee1.png)
Looting iii[post sequence traversal and backtracking + dynamic planning]

Spark foundation -scala

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

在解决了 2961 个用户反馈后,我做出了这样的改变...

史上超级详细,想找工作的你还不看这份资料就晚了

凤凰架构3——事务处理
随机推荐
ModuleNotFoundError: No module named ‘PIL‘解决方法
Cereals Mall - Distributed Advanced p129~p339 (end)
CPU负载很低,loadavg很高处理方法
Benefit a lot, Android interview questions
Unbalance balance (dynamic programming, DP)
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
ACTF 2022圆满落幕,0ops战队二连冠!!
[玩转Linux] [Docker] MySQL安装和配置
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
JDBC details
数学知识——高斯消元(初等行变换解方程组)代码实现
Leetcode 30. 串联所有单词的子串
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
[pytorch] yolov5 train your own data set
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
MySQL information Schema Learning (i) - - General table
In 50W, what have I done right?