当前位置:网站首页>LeetCode_格雷编码_中等_89.格雷编码
LeetCode_格雷编码_中等_89.格雷编码
2022-07-06 11:32:00 【小城老街】
1.题目
n 位格雷码序列是一个由 2n 个整数组成的序列,其中:
① 每个整数都在范围 [0, 2n - 1] 内(含 0 和 2n - 1)
② 第一个整数是 0
③ 一个整数在序列中出现不超过一次
④ 每对相邻整数的二进制表示恰好一位不同 ,且第一个和最后一个整数的二进制表示恰好一位不同
给你一个整数 n ,返回任一有效的 n 位格雷码序列 。
示例 1:
输入:n = 2
输出:[0,1,3,2]
解释:
[0,1,3,2] 的二进制表示是 [00,01,11,10] 。
- 00 和 01 有一位不同
- 01 和 11 有一位不同
- 11 和 10 有一位不同
- 10 和 00 有一位不同
[0,2,3,1] 也是一个有效的格雷码序列,其二进制表示是 [00,10,11,01] 。
- 00 和 10 有一位不同
- 10 和 11 有一位不同
- 11 和 01 有一位不同
- 01 和 00 有一位不同
示例 2:
输入:n = 1
输出:[0,1]
提示:
1 <= n <= 16
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/gray-code
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2.思路
思路参考本题官方题解。
(1)对称生成
(2)二进制数转格雷码
3.代码实现(Java)
//思路1————对称生成
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;
}
}
//思路2————二进制数转格雷码
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语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
- The nearest library of Qinglong panel
- 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
- 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
- swagger2报错Illegal DefaultValue null for parameter type integer
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
- Multithreading Basics: basic concepts of threads and creation of threads
- Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
- Interview assault 63: how to remove duplication in MySQL?
- Based on butterfly species recognition
猜你喜欢

CCNP Part 11 BGP (III) (essence)

Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller

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

Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up

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

Documents to be used in IC design process

AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】

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

pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法

An error occurs when installing MySQL: could not create or access the registry key needed for the
随机推荐
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
打家劫舍III[后序遍历与回溯+动态规划]
Benefit a lot, Android interview questions
通俗的讲解,带你入门协程
关于静态类型、动态类型、id、instancetype
USB host driver - UVC swap
Looting iii[post sequence traversal and backtracking + dynamic planning]
IC设计流程中需要使用到的文件
spark基础-scala
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
C language daily practice - day 22: Zero foundation learning dynamic planning
Multithreading Basics: basic concepts of threads and creation of threads
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
学习探索-无缝轮播图
Use of deg2rad and rad2deg functions in MATLAB
AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)