当前位置:网站首页>LeetCode_ Gray code_ Medium_ 89. Gray code
LeetCode_ Gray code_ Medium_ 89. Gray code
2022-07-06 19:29:00 【Old street of small town】
1. subject
n Bit gray code sequence is a sequence composed of 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 does not appear more than once in the sequence
④ The binary representation of each pair of adjacent integers is exactly one bit different , And the binary representation of the first and last integers is exactly one bit 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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/gray-code
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Ideas
Train of thought reference Official solution to this problem .
(1) Symmetrically generated
(2) Binary to gray code
3. Code implementation (Java)
// Ideas 1———— Symmetrically generated
class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<Integer>();
res.add(0);
for (int i = 1; i <= n; i++) {
int m = res.size();
for (int j = m - 1; j >= 0; j--) {
res.add(res.get(j) | (1 << (i - 1)));
}
}
return res;
}
}
// Ideas 2———— Binary to gray code
class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<>();
for (int i = 0; i < (1 << n); i++) {
res.add((i >> 1) ^ i);
}
return res;
}
}
边栏推荐
- spark基础-scala
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
- 潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
- 冒烟测试怎么做
- Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
- Yyds dry goods inventory leetcode question set 751 - 760
- R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
- Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
- Mysql Information Schema 学习(二)--Innodb表
- An error occurs when installing MySQL: could not create or access the registry key needed for the
猜你喜欢

Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview

三面蚂蚁金服成功拿到offer,Android开发社招面试经验

MySQL information Schema Learning (i) - - General table
深入分析,Android面试真题解析火爆全网

Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
Benefit a lot, Android interview questions

思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理

Low CPU load and high loadavg processing method
![Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting](/img/dd/c3f4a9c38b156e3a9b9adfd6253773.gif)
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting

Cereals Mall - Distributed Advanced p129~p339 (end)
随机推荐
Cereals Mall - Distributed Advanced p129~p339 (end)
LeetCode_双指针_中等_61. 旋转链表
Tensorflow2.0 自定义训练的方式求解函数系数
冒烟测试怎么做
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
学习探索-函数防抖
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
short i =1; i=i+1与short i=1; i+=1的区别
深入分析,Android面试真题解析火爆全网
English topic assignment (25)
How to access localhost:8000 by mobile phone
Looting iii[post sequence traversal and backtracking + dynamic planning]
Test technology stack arrangement -- self cultivation of test development engineers
利用 clip-path 绘制不规则的图形
Detailed idea and code implementation of infix expression to suffix expression
Druid database connection pool details
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Camel case with Hungarian notation
CPU负载很低,loadavg很高处理方法
C # - realize serialization with Marshall class