当前位置:网站首页>[daily training -- Tencent select 50] 89 Gray code (only after seeing the solution of the problem)
[daily training -- Tencent select 50] 89 Gray code (only after seeing the solution of the problem)
2022-07-05 21:08:00 【Puppet__】
subject
n Bit gray code sequence It's a by 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 appears in the sequence Not more than once
Each pair adjacent Binary representation of integers Just a different , And
first and the last one Binary representation of integers Just a 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
Code
package tencent50;
import java.util.ArrayList;
import java.util.List;
public class leetcode89 {
// I feel like reciting the answer ...
// The key is to understand the generation process of gray code G(i) = i ^ (i / 2)
public List<Integer> grayCode(int n) {
List<Integer> ansList = new ArrayList<>();
// The interval is [0,2^n)
for (int i = 0; i < 1<<n; i++){
ansList.add(i ^ i>>1);
}
return ansList;
}
public static void main(String[] args) {
leetcode89 obj = new leetcode89();
System.out.println(obj.grayCode(2));
}
}
边栏推荐
- @Validated basic parameter verification, grouping parameter verification and nested parameter verification
- selenium 获取dom内属性值的方法
- sql系列(基础)-第二章 限制和排序数据
- hdu2377Bus Pass(构建更复杂的图+spfa)
- CareerCup它1.8 串移包括问题
- Is it necessary for bazel to learn
- AITM2-0002 12s或60s垂直燃烧试验
- Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
- 显示屏DIN 4102-1 Class B1防火测试要求
- POJ 3414 pots (bfs+ clues)
猜你喜欢
【案例】元素的显示与隐藏的运用--元素遮罩
XML modeling
PVC plastic sheets BS 476-6 determination of flame propagation properties
How to send samples when applying for BS 476-7 display? Is it the same as the display??
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
教你自己训练的pytorch模型转caffe(三)
Talk about my fate with some programming languages
PVC 塑料片BS 476-6 火焰传播性能测定
Influence of oscilloscope probe on signal source impedance
秋招将临 如何准备算法面试、回答算法面试题
随机推荐
Clion-MinGW编译后的exe文件添加ico图标
selenium 查找b或p标签的内容
XML modeling
事项研发工作流全面优化|Erda 2.2 版本如“七”而至
Hdu2377bus pass (build more complex diagram +spfa)
示波器探头对测量带宽的影响
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
Sophomore personal development summary
Learning robots have no way to start? Let me show you the current hot research directions of robots
js常用方法封装
Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
Generics of TS
ClickHouse 复制粘贴多行sql语句报错
leetcode:1755. Sum of subsequences closest to the target value
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
大二下个人发展小结
Influence of oscilloscope probe on signal source impedance
systemd-resolved 开启 debug 日志
poj 3414 Pots (bfs+线索)
shell编程100例