The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer*n*representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
**Example 1:**
~~~
Input: 2
Output: [0,1,3,2]
Explanation:
00 - 0
01 - 1
11 - 3
10 - 2
For a given n, a gray code sequence may not be uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence.
00 - 0
10 - 2
11 - 3
01 - 1
~~~
**Example 2:**
~~~
Input: 0
Output: [0]
Explanation: We define the gray code sequence to begin with 0.
A gray code sequence of n has size = 2n,
which for n = 0 the size is 20 = 1.
Therefore, for n = 0 the gray code sequence is [0].
~~~
比如说我们输入3,结果是[0,1,3,2,6,7,5,4]
那么总数就是2的三次方=8;
我们把他分成两瓣,[0,1,3,2]和[6,7,5,4],
然后!我们把[0,1,3,2]复制一份,然后倒过来[2,3,1,0]。
这个[6,7,5,4]减去[2,3,1,0]等于[4,4,4,4]=2的n-1次方遍历4次!
![](https://img.kancloud.cn/c9/67/c9678bc0c5bcd5f0bc947198c5549364_858x358.png)
```
/**
* @param {number} n
* @return {number[]}
*/
var grayCode = function(n) {
var res = [0];
var right;
var i = 1;
for(;i<=n;i++ ){
right = [...res].reverse().map((item) => item + Math.pow(2, i - 1));
res = res.concat(right);
}
return res;
};
```
![](https://img.kancloud.cn/5c/a3/5ca386842d654e8230625c7e0699fee1_645x565.png)
- 说明
- 排序
- java实现
- 二分归并排序
- 快速排序
- js实现
- 冒泡排序 (Bubble Sort)
- 快速排序-js
- ES6-rest参数
- 比较排序
- 堆排序
- 插入排序
- 归并排序
- 选择排序
- shellSort
- 排序基础知识
- 其他
- Rabbit
- chickenProblem
- 汉诺塔
- 取出两个字符串中开头相同的部分
- 两个七进制输入,输出七进制
- 二维数组(矩阵)对角线输出
- 根据概率取值
- 最小编辑距离算法
- 列划分算法
- 数组去重
- 数组去重1
- 在一天里,钟表的时针和分针会重合多少次?
- 找第二大问题
- Raft算法
- B-tree
- Leetcode JAVA实现
- leetcode1. 2 Sum
- leetcode2. Add Two Numbers
- leetcode7. Reverse Integer
- leetcode8. String to Integer (atoi)
- leetcode12. Integer to Roman
- leetcode13. Roman to Integer
- leetcode15. 3Sum
- leetcode18. 4Sum
- leetcode20. Valid Parentheses
- leetcode26. Remove Duplicates from Sorted Array
- leetcode27. Remove Element
- leetcode58. Length of Last Word
- leetcode66. Plus One
- leetcode67. Add Binary
- leetcode80. Remove Duplicates from Sorted Array II
- leetcode 88. Merge Sorted Array
- leetcode101. Symmetric Tree
- leetcode104. Maximum Depth of Binary Tree
- leetcode110. Balanced Binary Tree
- leetcode118. Pascal's Triangle
- leetcode119. Pascal's Triangle II
- leetcode121. Best Time to Buy and Sell Stock
- leetcode125. Valid Palindrome
- leetcode136. Single Number
- leetcode144. Binary Tree Preorder Traversal
- leetcode145. Binary Tree Postorder Traversal
- leetcode155. Min Stack
- leetcode169. Majority Element
- leetcode171. Excel Sheet Column Number
- leetcode172. Factorial Trailing Zeroes
- leetcode189. Rotate Array
- leetcode191. Number of 1 Bits
- leetcode202. Happy Number
- leetcode203. Remove Linked List Elements
- leetcode204. Count Primes
- leetcode225. Implement Stack using Queues
- leetcode226. Invert Binary Tree
- leetcode231. Power of Two
- leetcode232. Implement Queue using Stacks
- leetcode234. Palindrome Linked List
- leetcode237. Delete Node in a Linked List
- leetcode242. Valid Anagram
- leetcode258. Add Digits
- leetcode263. Ugly Number
- leetcode283. Move Zeroes
- leetcode292. Nim Game
- leetcode344. Reverse String
- leetcode371. Sum of Two Integers
- leetcode804. Unique Morse Code Words
- Leetcode JS 实现
- Js-leetcode 1. Two Sum
- Js-leetcode 2. Add Two Numbers
- Js-leetcode 10. Regular Expression Matching
- Js-leetcode 14. Longest Common Prefix
- Js-leetcode 17.Letter Combinations of a Phone Number
- Js-leetcode 20. Valid Parentheses
- Js-leetcode 27. Remove Element
- Js-leetcode 30. Substring with Concatenation of All Words
- Js-leetcode 35. Search Insert Position
- Js-leetcode 41. First Missing Positive
- Js-leetcode 48. Rotate Image
- Js-leetcode 53. Maximum Subarray
- Js-leetcode 54. Spiral Matrix
- Js-leetcode 73. Set Matrix Zeroes
- Js-leetcode 75. Sort Colors
- Js-leetcode 85. Maximal Rectangle
- Js-leetcode 89. Gray Code
- Js-leetcode 93.Restore IP Addresses
- Js-leetcode 98. Validate Binary Search Tree
- Js-leetcode 104. Maximum Depth of Binary Tree
- Js-leetcode 118. Pascal's Triangle
- Js-leetcode 121. Best Time to Buy and Sell Stock
- Js-leetcode 136. Single Number
- Js-leetcode 164. Maximum Gap
- Js-leetcode 189. Rotate Array
- Js-leetcode 215. Kth Largest Element in an Array
- Js-leetcode 217. Contains Duplicate
- Js-leetcode 258. Add Digits
- Js-leetcode 263.Ugly Number
- Js-leetcode 283. Move Zeroes
- Js-leetcode 326. Power of Three
- Js-leetcode 434. Number of Segments in a String
- Js-leetcode 459. Repeated Substring Pattern
- Js-leetcode 507. Perfect Number
- Js-leetcode 537. Complex Number Multiplication
- Js-leetcode 541. Reverse String II
- Js-leetcode 551. Student Attendance Record I
- Js-leetcode 557. Reverse Words in a String III
- Js-leetcode 605. Can Place Flowers
- Js-leetcode 606. Construct String from Binary Tree
- Js-leetcode 621. Task Scheduler
- Js-leetcode 622. Design Circular Queue
- Js-leetcode 633. Sum of Square Numbers
- Js-leetcode 657. Robot Return to Origin
- Js-leetcode 682. Baseball Game
- Js-leetcode 686. Repeated String Match
- Js-leetcode 696. Count Binary Substrings
- Js-leetcode 709.To Lower Case.md
- Js-leetcode 771 Jewels and Stones
- Js-leetcode 804.Unique Morse Code Words
- Js-leetcode 867. Transpose Matrix
- Js-leetcode 905. Sort Array By Parity
- Js-leetcode 914. X of a Kind in a Deck of Cards
- Js-leetcode 922. Sort Array By Parity II
- Js-leetcode 1221. Split a String in Balanced Strings
- Js-leetcode 3. Longest substring without repeating characters
- document
- 刘宇波老师--基础知识
- 算法与数据结构
- 数据机构与算法介绍
- 二叉树
- 堆
- 链表
- 队列
- 并查集
- 最小生成树
- 图论
- 最短路径
- 总结
- 玩转数据结构
- 介绍
- 数组
- 栈和队列
- 玩转链表
- 链表与递归
- 二分搜索树
- 集合与映射
- 第8章 优先队列和堆
- 第9章 线段树
- 第10章 Trie
- 玩转算法面试 leetcode题库分门别类详细解析
- 第一章 介绍
- 第二章 时间复杂度和空间复杂度
- 第三章
- 深度实战玩转算法
- JS数据结构与算法
- 第一章.介绍
- 第二章.字符串
- 第三章.数组
- 第四章
- 第五章.排序
- 第六章.递归
- 第七章.栈
- 第八章.队列
- 第九章.链表
- 第十章.矩阵
- 第十一章.二叉树
- 第十二章.堆
- 极客时间
- 第01课丨数据结构与算法总览
- 第02课丨训练准备和复杂度分析
- 第03课丨数组、链表、跳表
- 第04课丨栈、队列、优先队列、双端队列
- 算法基础
- 哈希表(散列表)
- 异或运算
- leetcode
- KD树(K-Dimension Tree)
- Rtree
- B站-左神
- 一周刷爆LeetCode,算法大神左神(左程云)耗时100天打造算法与数据结构基础到高级全家桶教程,直击BTAJ等一线大厂必问算法面试题真题详解
- 简单的排序算法
- 认识Nlog(N)的算法
- 第三节:桶排序及排序算法总结
- 左程云算法课程
- 1 认识复杂度、对数器、二分法与异或运算
- 2 链表结构、栈、队列、递归行为、哈希表和有序表
- 10道BAT必问算法面试题
- 超级水王问题
- 78节
- 04链表