当前位置:网站首页>[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));
}
}
边栏推荐
- Display DIN 4102-1 Class B1 fire test requirements
- LeetCode: Distinct Subsequences [115]
- AITM2-0002 12s或60s垂直燃烧试验
- Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
- Learning notes of SAS programming and data mining business case 19
- ArcGIS栅格重采样方法介绍
- 示波器探头对信号源阻抗的影响
- 基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
- leetcode:1755. 最接近目标值的子序列和
- shell编程100例
猜你喜欢
Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
Realize the function of verifying whether the user has completed login when browsing the page
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
实现浏览页面时校验用户是否已经完成登录的功能
2022-07-03-CKA-粉丝反馈最新情况
随机推荐
Traps in the explode function in PHP
Which securities company is better and which platform is safer for stock account opening
MYSQL IFNULL使用功能
Is Kai Niu 2980 useful? Is it safe to open an account
shell编程100例
EasyExcel的读写操作
R语言【数据管理】
Simple getting started example of Web Service
PHP deserialization +md5 collision
MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
ArcGIS栅格重采样方法介绍
[case] Application of element display and hiding -- element mask
实现浏览页面时校验用户是否已经完成登录的功能
Clion-MinGW编译后的exe文件添加ico图标
ts 之 泛型
校招期间 准备面试算法岗位 该怎么做?
字典树简单入门题(居然是蓝题?)
ViewRootImpl和WindowManagerService笔记
Sequence alignment
示波器探头对测量带宽的影响