当前位置:网站首页>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;
}
}
边栏推荐
- In depth analysis, Android interview real problem analysis is popular all over the network
- 史上超级详细,想找工作的你还不看这份资料就晚了
- 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
- [translation] a GPU approach to particle physics
- Mysql Information Schema 学习(一)--通用表
- R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
- 安装Mysql报错:Could not create or access the registry key needed for the...
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- Abstract classes and abstract methods
- Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
猜你喜欢
在解决了 2961 个用户反馈后,我做出了这样的改变...
Pytorch common loss function
In depth analysis, Android interview real problem analysis is popular all over the network
Druid database connection pool details
冒烟测试怎么做
时钟轮在 RPC 中的应用
[translation] a GPU approach to particle physics
A method of removing text blur based on pixel repair
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
An error occurs when installing MySQL: could not create or access the registry key needed for the
随机推荐
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
JDBC详解
DaGAN论文解读
C # - realize serialization with Marshall class
第五期个人能力认证考核通过名单公布
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
CCNP Part 11 BGP (III) (essence)
打家劫舍III[后序遍历与回溯+动态规划]
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
Abstract classes and abstract methods
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
史上超级详细,想找工作的你还不看这份资料就晚了
JDBC details
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
Leetcode topic [array] - 119 Yang Hui triangle II
R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
利用 clip-path 绘制不规则的图形
R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
A popular explanation will help you get started
Tensorflow and torch code verify whether CUDA is successfully installed