Skip to content

Commit 8fb90cd

Browse files
committed
二刷89
1 parent 8ead5e6 commit 8fb90cd

File tree

4 files changed

+55
-42
lines changed

4 files changed

+55
-42
lines changed

docs/0089-gray-code.adoc

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,53 +55,33 @@ image::images/0089-3.png[{image_attr}]
5555

5656
这个题的解法跟 xref:0338-counting-bits.adoc[338. Counting Bits] 有异曲同工之妙!
5757

58-
== 参考资料
59-
60-
. https://leetcode-cn.com/problems/gray-code/solution/gray-code-jing-xiang-fan-she-fa-by-jyd/[Gray Code (镜像反射法,图解) - 格雷编码 - 力扣(LeetCode)^]
61-
. https://en.wikipedia.org/wiki/Gray_code[Gray code - Wikipedia^]
62-
. http://mathworld.wolfram.com/GrayCode.html[Gray Code -- from Wolfram MathWorld^]
63-
64-
65-
The gray code is a binary numeral system where two successive values differ in only one bit.
66-
67-
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.
68-
69-
*Example 1:*
7058

71-
[subs="verbatim,quotes,macros"]
59+
[[src-0089]]
60+
[tabs]
61+
====
62+
一刷::
63+
+
64+
--
65+
[{java_src_attr}]
7266
----
73-
*Input:* 2
74-
*Output:* `[0,1,3,2]`
75-
*Explanation:*
76-
00 - 0
77-
01 - 1
78-
11 - 3
79-
10 - 2
80-
81-
For a given _n_, a gray code sequence may not be uniquely defined.
82-
For example, [0,2,3,1] is also a valid gray code sequence.
83-
84-
00 - 0
85-
10 - 2
86-
11 - 3
87-
01 - 1
67+
include::{sourcedir}/_0089_GrayCode.java[tag=answer]
8868
----
69+
--
8970
90-
*Example 2:*
91-
92-
[subs="verbatim,quotes,macros"]
71+
二刷::
72+
+
73+
--
74+
[{java_src_attr}]
9375
----
94-
*Input:* 0
95-
*Output:* `[0]
96-
*Explanation:* We define the gray code sequence to begin with 0.
97-
A gray code sequence of _n_ has size = 2^n^, which for _n_ = 0 the size is 2^0^ = 1.
98-
Therefore, for _n_ = 0 the gray code sequence is [0].`
76+
include::{sourcedir}/_0089_GrayCode_2.java[tag=answer]
9977
----
78+
--
79+
====
10080

81+
== 参考资料
82+
83+
. https://leetcode.cn/problems/gray-code/solutions/13637/gray-code-jing-xiang-fan-she-fa-by-jyd/?envType=study-plan-v2&envId=selected-coding-interview[89. 格雷编码 - 清晰图解^]
84+
. https://en.wikipedia.org/wiki/Gray_code[Gray code - Wikipedia^]
85+
. http://mathworld.wolfram.com/GrayCode.html[Gray Code -- from Wolfram MathWorld^]
10186

102-
[[src-0089]]
103-
[{java_src_attr}]
104-
----
105-
include::{sourcedir}/_0089_GrayCode.java[tag=answer]
106-
----
10787

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,11 @@
660660
|{doc_base_url}/0371-sum-of-two-integers.adoc[题解]
661661
|❌位运算
662662

663+
|{counter:codes}
664+
|{leetcode_base_url}/gray-code/[89. Gray Code^]
665+
|{doc_base_url}/0089-gray-code.adoc[题解]
666+
|❌位运算
667+
663668
|===
664669

665670
截止目前,本轮练习一共完成 {codes} 道题。

src/main/java/com/diguage/algo/leetcode/_0089_GrayCode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
* @since 2020-02-05 19:36
1414
*/
1515
public class _0089_GrayCode {
16-
// tag::answer[]
16+
// tag::answer[]
1717
/**
1818
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Gray Code.
1919
* Memory Usage: 37 MB, less than 8.00% of Java online submissions for Gray Code.
2020
*
2121
* Copy from: https://leetcode-cn.com/problems/gray-code/solution/gray-code-jing-xiang-fan-she-fa-by-jyd/[Gray Code (镜像反射法,图解) - 格雷编码 - 力扣(LeetCode)]
22+
*
23+
* @author D瓜哥 · https://www.diguage.com
24+
* @since 2020-02-05 19:36
2225
*/
2326
public List<Integer> grayCode(int n) {
2427
List<Integer> result = new ArrayList<>((int) Math.pow(2, n));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _0089_GrayCode_2 {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2024-09-16 20:43:16
11+
*/
12+
public List<Integer> grayCode(int n) {
13+
List<Integer> result = new ArrayList<>((int) Math.pow(2, n));
14+
result.add(0);
15+
int head = 1;
16+
for (int i = 0; i < n; i++) {
17+
for (int j = result.size() - 1; j >= 0; j--) {
18+
result.add(head + result.get(j));
19+
}
20+
head <<= 1;
21+
}
22+
return result;
23+
}
24+
// end::answer[]
25+
}

0 commit comments

Comments
 (0)