Skip to content

Commit 9d4edab

Browse files
committed
✨feat: Add 89
1 parent c985ff0 commit 9d4edab

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| [68. 文本左右对齐](https://leetcode-cn.com/problems/text-justification/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/text-justification/solution/gong-shui-san-xie-zi-fu-chuan-mo-ni-by-a-s3v7/) | 困难 | 🤩🤩🤩 |
2020
| [71. 简化路径](https://leetcode-cn.com/problems/simplify-path/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/simplify-path/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-w7xi/) | 中等 | 🤩🤩🤩🤩 |
2121
| [73. 矩阵置零](https://leetcode-cn.com/problems/set-matrix-zeroes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-matrix-zeroes/solution/xiang-jie-fen-san-bu-de-o1-kong-jian-jie-dbxd/) | 中等 | 🤩🤩🤩🤩 |
22+
| [89. 格雷编码](https://leetcode-cn.com/problems/gray-code/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/gray-code/solution/gong-shui-san-xie-dui-cheng-xing-gou-zao-9ap1/) | 中等 | 🤩🤩🤩🤩 |
2223
| [165. 比较版本号](https://leetcode-cn.com/problems/compare-version-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/compare-version-numbers/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-xsod/) | 中等 | 🤩🤩🤩🤩 |
2324
| [166. 分数到小数](https://leetcode-cn.com/problems/fraction-to-recurring-decimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/fraction-to-recurring-decimal/solution/gong-shui-san-xie-mo-ni-shu-shi-ji-suan-kq8c4/) | 中等 | 🤩🤩🤩🤩 |
2425
| [168. Excel表列名称](https://leetcode-cn.com/problems/excel-sheet-column-title/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/excel-sheet-column-title/solution/gong-shui-san-xie-cong-1-kai-shi-de-26-j-g2ur/) | 简单 | 🤩🤩🤩 |
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[89. 格雷编码](https://leetcode-cn.com/problems/gray-code/solution/gong-shui-san-xie-dui-cheng-xing-gou-zao-9ap1/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
$n$ 位格雷码序列 是一个由 $2^n$ 个整数组成的序列,其中:
10+
* 每个整数都在范围 $[0, 2^n - 1]$ 内(含 $0$ 和 $2^n - 1$)
11+
* 第一个整数是 0
12+
* 一个整数在序列中出现 不超过一次
13+
* 每对 相邻 整数的二进制表示 恰好一位不同 ,且
14+
* 第一个 和 最后一个 整数的二进制表示 恰好一位不同
15+
16+
给你一个整数 `n` ,返回任一有效的 `n` 位格雷码序列 。
17+
18+
示例 1:
19+
```
20+
输入:n = 2
21+
22+
输出:[0,1,3,2]
23+
24+
解释:
25+
[0,1,3,2] 的二进制表示是 [00,01,11,10] 。
26+
- 00 和 01 有一位不同
27+
- 01 和 11 有一位不同
28+
- 11 和 10 有一位不同
29+
- 10 和 00 有一位不同
30+
31+
[0,2,3,1] 也是一个有效的格雷码序列,其二进制表示是 [00,10,11,01] 。
32+
- 00 和 10 有一位不同
33+
- 10 和 11 有一位不同
34+
- 11 和 01 有一位不同
35+
- 01 和 00 有一位不同
36+
```
37+
示例 2:
38+
```
39+
输入:n = 1
40+
41+
输出:[0,1]
42+
```
43+
44+
提示:
45+
* `1 <= n <= 16`
46+
47+
---
48+
49+
### 对称性构造
50+
51+
根据格雷码的定义,我们需要构造一个合法序列,序列之间每两个数的二进制表示中只有一位不同,同时序列第一位和最后一位对应的二进制也只有一位不同。
52+
53+
我们知道 $k + 1$ 位的格雷码序列是 $k$ 位格雷码序列长度的两倍,利用合法 $k$ 位格雷码序列,我们可以「对称性」地构造出 $k + 1$ 位格雷码。
54+
55+
具体的,假定 $k$ 位格雷码序列长度为 $n$,我们将这 $k$ 位的格雷序列进行翻转,并追加到原有序列的尾部,得到长度为 $2 * n$ 的序列,此时新序列的前后两部分均为合法的格雷码。
56+
57+
考虑如何进行解决衔接点的合法性:我们可以对于序列的后半(翻转而来)的部分中的每个数进行「尾部」追加 $1$ 的操作,确保链接点的两个数只有有一位二进制位不同,同时并不影响前后两半部分的合法性。
58+
59+
而且由于后半部分本身是由前半部分翻转而来,序列中的第一个数和最后一个数原本为同一个值,经过追加 $1$ 的操作之后,首尾两个数的二进制表示只有一位不同,整个序列的合法性得以保证。
60+
61+
代码:
62+
```Java
63+
class Solution {
64+
public List<Integer> grayCode(int n) {
65+
List<Integer> ans = new ArrayList<>();
66+
ans.add(0);
67+
while (n-- > 0) {
68+
int m = ans.size();
69+
for (int i = m - 1; i >= 0; i--) {
70+
ans.set(i, ans.get(i) << 1);
71+
ans.add(ans.get(i) + 1);
72+
}
73+
}
74+
return ans;
75+
}
76+
}
77+
```
78+
* 时间复杂度:$O(2^n)$
79+
* 空间复杂度:$O(2^n)$
80+
81+
---
82+
83+
### 最后
84+
85+
这是我们「刷穿 LeetCode」系列文章的第 `No.89` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
86+
87+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
88+
89+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
90+
91+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
92+

0 commit comments

Comments
 (0)