当前位置:网站首页>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;
}
}
边栏推荐
- 深入分析,Android面试真题解析火爆全网
- Computer network: sorting out common network interview questions (I)
- Excel 中VBA脚本的简单应用
- 学习探索-函数防抖
- DaGAN论文解读
- Use of deg2rad and rad2deg functions in MATLAB
- ModuleNotFoundError: No module named ‘PIL‘解决方法
- R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
- Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
- 打家劫舍III[后序遍历与回溯+动态规划]
猜你喜欢
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
ROS custom message publishing subscription example
Detailed idea and code implementation of infix expression to suffix expression
应用使用Druid连接池经常性断链问题分析
The list of people who passed the fifth phase of personal ability certification assessment was published
Mysql Information Schema 学习(二)--Innodb表
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
zabbix 代理服务器 与 zabbix-snmp 监控
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
Simple understanding of MySQL database
随机推荐
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
ModuleNotFoundError: No module named ‘PIL‘解决方法
English topic assignment (25)
LeetCode-1279. Traffic light intersection
学习探索-无缝轮播图
Benefit a lot, Android interview questions
Pytorch common loss function
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
打家劫舍III[后序遍历与回溯+动态规划]
Mysql Information Schema 学习(一)--通用表
tensorflow和torch代码验证cuda是否安装成功
An error occurs when installing MySQL: could not create or access the registry key needed for the
USB host driver - UVC swap
Php+redis realizes the function of canceling orders over time
Based on butterfly species recognition
关于静态类型、动态类型、id、instancetype
php+redis实现超时取消订单功能
Characteristic colleges and universities, jointly build Netease Industrial College
How word displays modification traces
凤凰架构2——访问远程服务