当前位置:网站首页>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;
}
}
边栏推荐
- How word displays modification traces
- LeetCode-1279. 红绿灯路口
- R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
- tensorflow和torch代码验证cuda是否安装成功
- Interview assault 63: how to remove duplication in MySQL?
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- It's super detailed in history. It's too late for you to read this information if you want to find a job
- Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
- 学习探索-使用伪元素清除浮动元素造成的高度坍塌
- Reflection and illegalaccessexception exception during application
猜你喜欢

Abstract classes and abstract methods

zabbix 代理服务器 与 zabbix-snmp 监控

About static type, dynamic type, ID, instancetype

Pytorch common loss function

应用使用Druid连接池经常性断链问题分析

Php+redis realizes the function of canceling orders over time

JDBC详解

DaGAN论文解读

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

An error occurs when installing MySQL: could not create or access the registry key needed for the
随机推荐
The nearest library of Qinglong panel
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
IC设计流程中需要使用到的文件
Analysis of frequent chain breaks in applications using Druid connection pools
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
About static type, dynamic type, ID, instancetype
数学知识——高斯消元(初等行变换解方程组)代码实现
Excel 中VBA脚本的简单应用
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
php+redis实现超时取消订单功能
swagger2报错Illegal DefaultValue null for parameter type integer
How word displays modification traces
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
zabbix 代理服务器 与 zabbix-snmp 监控
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
凤凰架构2——访问远程服务
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
史上超级详细,想找工作的你还不看这份资料就晚了
Mysql Information Schema 学习(一)--通用表