Jan 23 - Gray Code; BackTracking;

We can find the regular pattern in gray code, which is:

the first of the combinations of n-digit gray code is exactly the combinations of (n-1)-digit gray code, the second half is consist of 1<<(n-1) + each elements from the end to the start in (n-1)-digit gray code.

code:

public class Solution {
    public List<Integer> grayCode(int n) {
        List<Integer> list = new ArrayList<>();
        if(n == 0){
            list.add(0);
            return list;
        }
        if(n == 1){
            list.add(0);
            list.add(1);
            return list;
        }
        int i = 1 << (n-1);
        list = grayCode(n-1);
        for(int j = list.size() - 1; j >=0; j--) list.add(i+list.get(j));
        return list;
    }
}
时间: 2024-10-05 03:46:44

Jan 23 - Gray Code; BackTracking;的相关文章

[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

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.

89. Gray Code

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 be

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

89.Gray Code(回溯)

The gray code is a binary numeral system where two successive valuesdiffer in only one bit. Given a non-negative integer n representing the totalnumber of bits in the code, print the sequence of gray code. A gray codesequence must begin with 0. For e

leetcode_89题——Gray Code(回溯法)

Gray Code Total Accepted: 32175 Total Submissions: 98703My Submissions Question Solution 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

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 t

leetcode笔记:Gray Code(2016腾讯软件开发笔试题)

一.题目描述 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 wit

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