当前位置:网站首页>[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));
}
}
边栏推荐
- Which is the best online collaboration product? Microsoft loop, notion, flowus
- SQL series (basic) - Chapter 2 limiting and sorting data
- @Validated basic parameter verification, grouping parameter verification and nested parameter verification
- 研學旅遊實踐教育的開展助力文旅產業發展
- ts 之 属性的修饰符public、private、protect
- postgres 建立连接并删除记录
- Is Kai Niu 2980 useful? Is it safe to open an account
- Traps in the explode function in PHP
- JS common method encapsulation
- js常用方法封装
猜你喜欢
PVC plastic sheets BS 476-6 determination of flame propagation properties
Opérations de lecture et d'écriture pour easyexcel
MySQL 千万数据量深分页优化, 拒绝线上故障!
leetcode:1755. 最接近目标值的子序列和
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
【案例】元素的显示与隐藏的运用--元素遮罩
请查收.NET MAUI 的最新学习资源
PVC 塑料片BS 476-6 火焰传播性能测定
教你自己训练的pytorch模型转caffe(三)
随机推荐
100 cases of shell programming
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
实现浏览页面时校验用户是否已经完成登录的功能
XML modeling
模式-“里氏替换原则”
股票开户选择哪家证券公司比较好哪家平台更安全
示波器探头对信号源阻抗的影响
leetcode:1755. Sum of subsequences closest to the target value
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
教你自己训练的pytorch模型转caffe(二)
Deep merge object deep copy of vant source code parsing
Simple getting started example of Web Service
ts 之 属性的修饰符public、private、protect
Aitm2-0002 12s or 60s vertical combustion test
面试官:并发编程实战会吗?(线程控制操作详解)
postgis 安装地理信息扩展
Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
《SAS编程和数据挖掘商业案例》学习笔记# 19
字典树简单入门题(居然是蓝题?)
Material design component - use bottomsheet to show extended content (II)