当前位置:网站首页>[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));
}
}
边栏推荐
- 树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
- MySQL InnoDB架构原理
- MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
- EasyExcel的讀寫操作
- 最长摆动序列[贪心练习]
- Pytoch practice -- MNIST dataset handwritten digit recognition
- PHP deserialization +md5 collision
- systemd-resolved 开启 debug 日志
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- poj 3414 Pots (bfs+线索)
猜你喜欢

ArcGIS栅格重采样方法介绍

Enclosed please find. Net Maui's latest learning resources

【案例】元素的显示与隐藏的运用--元素遮罩

请查收.NET MAUI 的最新学习资源

珍爱网微服务底层框架演进从开源组件封装到自研

Reading and writing operations of easyexcel

Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture

研學旅遊實踐教育的開展助力文旅產業發展

PVC plastic sheets BS 476-6 determination of flame propagation properties

haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
随机推荐
Generics of TS
AITM2-0002 12s或60s垂直燃烧试验
@Validated基础参数校验、分组参数验证和嵌套参数验证
Influence of oscilloscope probe on signal source impedance
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
Get JS of the previous day (timestamp conversion)
Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
终端安全能力验证环境搭建和渗透测试记录
面试官:并发编程实战会吗?(线程控制操作详解)
leetcode:1139. The largest square bounded by 1
Is Kai Niu 2980 useful? Is it safe to open an account
Influence of oscilloscope probe on measurement bandwidth
ts 之 泛型
PVC 塑料片BS 476-6 火焰传播性能测定
What are the requirements of UL 2043 test for drive housing in the United States?
CLion配置visual studio(msvc)和JOM多核编译
Establishment of terminal security capability verification environment and penetration test records
selenium 查找b或p标签的内容
Longest swing sequence [greedy practice]