当前位置:网站首页>[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));
}
}
边栏推荐
- MYSQL IFNULL使用功能
- EasyExcel的讀寫操作
- php中explode函数存在的陷阱
- Écrire une interface basée sur flask
- Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
- Realize the function of verifying whether the user has completed login when browsing the page
- 基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
- 示波器探头对信号源阻抗的影响
- CLion配置visual studio(msvc)和JOM多核编译
- JS common method encapsulation
猜你喜欢

MySQL deep paging optimization with tens of millions of data, and online failure is rejected!

中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097

How to send samples when applying for BS 476-7 display? Is it the same as the display??

Clickhouse copy paste multi line SQL statement error

Reading and writing operations of easyexcel

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

面试官:并发编程实战会吗?(线程控制操作详解)

Learning robots have no way to start? Let me show you the current hot research directions of robots

EN 438-7 laminated sheet products for building covering decoration - CE certification

Using webassembly to operate excel on the browser side
随机推荐
Clion-MinGW编译后的exe文件添加ico图标
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
@Validated basic parameter verification, grouping parameter verification and nested parameter verification
leetcode:1139. The largest square bounded by 1
EasyExcel的读写操作
LeetCode: Distinct Subsequences [115]
教你自己训练的pytorch模型转caffe(三)
Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w
Pytorch实战——MNIST数据集手写数字识别
Which is the best online collaboration product? Microsoft loop, notion, flowus
Longest swing sequence [greedy practice]
Determine the best implementation of horizontal and vertical screens
Simple getting started example of Web Service
JS common method encapsulation
Is Kai Niu 2980 useful? Is it safe to open an account
【日常训练】729. 我的日程安排表 I
终端安全能力验证环境搭建和渗透测试记录
Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
Sophomore personal development summary
Learning notes of SAS programming and data mining business case 19