Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita27dd2d

Browse files
committed
89
1 parentc2e57ba commita27dd2d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

‎Medium/89-grayCode.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Key
3+
* n = 0, return [0]
4+
* n = 1, return [0, 1]
5+
* n = 2, return [00, 01, 11, 10]
6+
* we can see that first n elements (first half) is from n - 1 results,
7+
* in the second half of results array, first thing is to flip the highest bit to 1
8+
* the rest numbers of part two is symmetric of first half part after first bit.
9+
*
10+
*@param {number} n
11+
*@return {number[]}
12+
*/
13+
vargrayCode=function(n){
14+
varresults=[0];
15+
for(vari=0;i<n;i++){
16+
varsize=results.length;
17+
for(varj=size-1;j>=0;j--){
18+
varval=results[j];
19+
results.push(val|(1<<i));
20+
}
21+
}
22+
23+
returnresults;
24+
};

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
*[64.Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) -[Solution](./Medium/64-minimumPathSum.js)
7777
*[75. Sort Colors](https://leetcode.com/problems/sort-colors/) -[Solution](./Medium/75-sortColors.js)
7878
*[77.Combinations](https://leetcode.com/problems/combinations/) -[Solution](./Medium/77-combinations.js)
79+
*[89. Gray Code](https://leetcode.com/problems/gray-code/) -[Solution](./Medium/89-grayCode.js)
7980
*[94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) -[Solution](./Medium/94-binaryTreeInorder.js)
8081
*[96. Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/) -[Solution](./Medium/96-uniqueBinarySearchTrees.js)
8182
*[108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) -[Solution](./Medium/108-convertSortedArraytoBST.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp