当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

C language daily practice - day 22: Zero foundation learning dynamic planning

IC设计流程中需要使用到的文件

Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
Benefit a lot, Android interview questions

LeetCode-1279. Traffic light intersection

助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!

五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”

Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!

DaGAN论文解读

Take a look at how cabloyjs workflow engine implements activiti boundary events
随机推荐
驼峰式与下划线命名规则(Camel case With hungarian notation)
Don't miss this underestimated movie because of controversy!
[translation] a GPU approach to particle physics
In depth analysis, Android interview real problem analysis is popular all over the network
Tensorflow and torch code verify whether CUDA is successfully installed
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
About image reading and processing, etc
接雨水问题解析
Yyds dry goods inventory leetcode question set 751 - 760
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
Documents to be used in IC design process
C # use Marshall to manually create unmanaged memory in the heap and use
Druid 数据库连接池 详解
[pytorch] yolov5 train your own data set
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
RT-Thread 组件 FinSH 使用时遇到的问题