当前位置:网站首页>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;
}
}
边栏推荐
- How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
- 思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
- CPU负载很低,loadavg很高处理方法
- How to type multiple spaces when editing CSDN articles
- 时钟轮在 RPC 中的应用
- Yyds dry goods inventory leetcode question set 751 - 760
- 凤凰架构2——访问远程服务
- Spark foundation -scala
- PMP practice once a day | don't get lost in the exam -7.6
- Php+redis realizes the function of canceling orders over time
猜你喜欢
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
Looting iii[post sequence traversal and backtracking + dynamic planning]
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
Reflection and illegalaccessexception exception during application
【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
史上超级详细,想找工作的你还不看这份资料就晚了
Php+redis realizes the function of canceling orders over time
随机推荐
Abstract classes and abstract methods
Dark horse -- redis
关于图像的读取及处理等
The list of people who passed the fifth phase of personal ability certification assessment was published
An error occurs when installing MySQL: could not create or access the registry key needed for the
Swagger2 reports an error illegal DefaultValue null for parameter type integer
LeetCode_格雷编码_中等_89.格雷编码
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
Benefit a lot, Android interview questions
学习探索-无缝轮播图
保证接口数据安全的10种方案
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
凤凰架构2——访问远程服务
Low CPU load and high loadavg processing method
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
LeetCode_双指针_中等_61. 旋转链表
Black Horse - - Redis Chapter