当前位置:网站首页>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;
}
}
边栏推荐
- Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
- AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
- 应用使用Druid连接池经常性断链问题分析
- R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
- Wx applet learning notes day01
- C # use Marshall to manually create unmanaged memory in the heap and use
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- 10 schemes to ensure interface data security
- 五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
- Based on butterfly species recognition
猜你喜欢

Take a look at how cabloyjs workflow engine implements activiti boundary events
![[translation] a GPU approach to particle physics](/img/07/57036c925155cab36678c696e89440.jpg)
[translation] a GPU approach to particle physics

MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
Interview assault 63: how to remove duplication in MySQL?
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic

业务与应用同步发展:应用现代化的策略建议

Druid 数据库连接池 详解

凤凰架构3——事务处理

如何提高网站权重

黑馬--Redis篇
随机推荐
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
IC设计流程中需要使用到的文件
How to improve website weight
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
First day of rhcsa study
Synchronous development of business and application: strategic suggestions for application modernization
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
Multithreading Basics: basic concepts of threads and creation of threads
冒烟测试怎么做
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
ACTF 2022圆满落幕,0ops战队二连冠!!
10 schemes to ensure interface data security
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
The nearest library of Qinglong panel
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Documents to be used in IC design process
R语言使用dt函数生成t分布密度函数数据、使用plot函数可视化t分布密度函数数据(t Distribution)
学习探索-无缝轮播图