Gray Code LeetCode 89

题目: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.

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2
这道题其实就是产生格雷码(简直废话~),格雷码是有一定规律的,数电课上学过一点格雷码的规律,但是对于编程解决问题没啥用啊- -,我们还是来找规律吧。4位的,我手写最多写4位000000010011011001110101010011001101111111101010101110011000按列来看,从左往右数第0列(从0列开始),前半部分为0,后半部分为1。第1列,等分成上下两部分,第一部分的前半部分为0,后半部分为1。第二部分的前半部分为1,后半部分为0。后面继续看,可以看出规律了。把第i列分成2^i部分,奇数部分的前半部分为0,后半部分为1,偶数部分的前半部分为1,后半部分为0。或者用另外一种表述方法,我们可以把本次的划分看作前一次的划分的一个部分,如果是上次划分的前半部分,则符合前面的奇数部分。后半部分则符合前面的后半部分。代码如下:
 1 /*
 2 使用分治法,如果本次属于前一次二分的前半部分,则本次的前半部分第n位填0,后半部分第n位填1
 3             如果本次属于前一次二分的后半部分,则本次的前半部分第n位填1,后半部分第n位填0
 4 第一次填写,设为前半部分。代码中left代表前半部分-_-。
 5 */
 6
 7 void setGray(int *num, int start, int end, bool left, int n)
 8 {
 9     int lbit, rbit, i = 0;
10     int mid = (start + end) >> 1;
11
12     if (left)
13     {
14         lbit = 0;
15         rbit = 1 << n;
16     }
17     else
18     {
19         lbit = 1 << n;
20         rbit = 0;
21     }
22
23     for (i = start; i <= mid; ++i)
24         num[i] = num[i] | lbit;
25
26     for (i = mid + 1; i <= end; ++i)
27         num[i] = num[i] | rbit;
28
29     if (n > 0)
30     {
31         setGray(num, start, mid, true, n - 1);
32         setGray(num, mid + 1, end, false, n - 1);
33     }
34 }
35
36
37 int *grayCode(int n, int *returnSize)
38 {
39     *returnSize = 1 << n;
40     int *ans = (int *)malloc(*returnSize * sizeof(int));
41     int i = 0;
42     for (i = 0; i < *returnSize; ++i)
43     {
44         ans[i] = 0;
45     }
46     if (n > 0) setGray(ans, 0, *returnSize - 1, true, n - 1);
47     return ans;
48 }
时间: 2024-12-21 15:20:37

Gray Code LeetCode 89的相关文章

Gray Code leetcode java

题目: 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

Gray Code [leetcode]

第一种方法是直接排列 以二进制为0值的格雷码为第零项,第一项改变最右边的位元,第二项改变右起第一个为1的位元的左边位元,第三.四项方法同第一.二项,如此反复,即可排列出n个位元的格雷码. vector<int> grayCode(int n) { vector<int> res; res.push_back(0); if (n == 0) return res; res.push_back(1); int total = 1 << n; for (int i = 2;i

LeetCode --- 89. Gray Code

题目链接:Gray Code 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 b

【一天一道LeetCode】#89. Gray Code

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

leetcode 89 Gray Code ----- java

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. Fo

【LeetCode】89. Gray Code

题目: 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

leetcode || 89、Gray Code

problem: 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 w

leetCode 89.Gray Code (格雷码) 解题思路和方法

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. Fo

LeetCode OJ 89. Gray Code

题目 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.