LeetCode OJ - Gray Code

这道题就是找规律啊!!!

想想啊,11和10是可以连续的,那么10和11也是可以连续的。

下面是AC代码:


 1  /**
2 * The gray code is a binary numeral system where two successive values differ in only one bit.
3 * 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.
4 * For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
5 * 00 - 0
6 * 01 - 1
7 * 11 - 3
8 * 10 - 2
9 * Note:
10 * For a given n, a gray code sequence is not uniquely defined.
11 * For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.
12 * For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.
13 * Solution : find the rule!!!!
14 * @param n
15 * @return
16 */
17 public ArrayList<Integer> grayCode(int n) {
18 ArrayList<Integer> r = new ArrayList<Integer>();
19 r.add(0);
20 if(n == 0)
21 return r;
22 r.add(1);
23 if(n == 1)
24 return r;
25 for(int m = 1;m<n;m++){
26 int cur_size = r.size();
27 int base = (int) Math.pow(2, m);
28 for(int i = cur_size - 1;i>=0;i--){
29 r.add(r.get(i)+base);
30 }
31 }
32 return r;
33 }

时间: 2024-07-29 07:36:05

LeetCode OJ - Gray Code的相关文章

LeetCode:Gray Code

1.题目名称 Gray Code(格雷码) 2.题目地址 https://leetcode.com/problems/gray-code/ 3.题目内容 英文: 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

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][JavaScript]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 begin

【leetcode】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 begin

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[88] Gray Code

题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: For example, given n = 2, return [0,1,3,2]. Its gray code sequence is: 00 - 0 01 - 1 11 - 3 10 - 2 左边一列是格雷码,右边一列是对应的需要输出的十进制. 我们来观察一下格雷码的规律.如果是1那么是 0 1

Java for LeetCode 089 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: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

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