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

Note:

For a given n, a gray code sequence is not uniquely defined.

For example, [0,2,3,1] is also a valid gray code sequence according to the
above definition.

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

此题不算难,主要是要掌握什么是格雷码。然后才干做这道题,详细格雷码概念參考百度就可以,详细解法是遍历,逐一试探就可以。

代码例如以下:

public class Solution {
public List<Integer> grayCode(int n) { List<Integer> list = new ArrayList<Integer>();
//去除反复
Set<String> set = new HashSet<String>(); //表示二进制数的数组
char[] c = new char[n];
Arrays.fill(c, '0');//所有为0
set.add(new String(c));
list.add(0);
int i = 1;//从1開始
//格雷码长度
int len = (int) Math.pow(2, n);
while(i++ < len){
//逐个尝试
for(int j = c.length - 1; j >= 0; j--){
c[j] = c[j] == '1' ? '0':'1';//每一位均异或
String b = new String(c);
//成功则加入结果集。并结束循环
if(set.add(b)){
//二进制转换为十进制
list.add(Integer.valueOf(b, 2));
break;
}else{
//假设不是则数值变换回来
c[j] = c[j] == '1' ? '0':'1';
}
}
}
return list;
}
}

leetCode 89.Gray Code (格雷码) 解题思路和方法的更多相关文章

  1. [LeetCode] 89. Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  2. [LeetCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  3. gray code 格雷码 递归

    格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...

  4. [LintCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  5. HDU 5375 Gray code 格雷码(水题)

    题意:给一个二进制数(包含3种符号:'0'  '1'  '?'  ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...

  6. Gray Code - 格雷码

    基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...

  7. Leetcode#89 Gray Code

    原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...

  8. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  9. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. C#和Java在语法上的差异(原创,持续更新中)

    1.switch  C#一直支持String类型 Java直到1.7才支持 2.C#里String有Length属性 Java里是Length方法 3.C#中修饰class的sealed效果与Java ...

  2. jQuery四叶草菜单效果,跟360杀毒软件差不多

    首先,我们要在js,css文件夹中创建js跟css,然后在body中写入html代码 <main><!--标签是 HTML 5 中的新标签. 素中的内容对于文档来说应当是唯一的.它不 ...

  3. Android 仿微信调用第三方应用导航(百度,高德、腾讯)

    实现目标 先来一张微信功能截图看看要做什么  其实就是有一个目的地,点击目的地的时候弹出可选择的应用进行导航. 大脑动一下,要实现这个功能应该大体分成两步: 底部弹出可选的地图菜单进行展示 点击具体菜 ...

  4. [Windows Server 2008] MySQL单数据库迁移方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:MySQL ...

  5. oracle 入门笔记---分区表的分区交换

    本文参考来自作者:蓝紫 详细内容请阅读原文 : http://www.cnblogs.com/lanzi/archive/2013/01/24/2875838.html 在oracle 11.2环境下 ...

  6. Java学习3_一些基础3_16.5.7

    字符串的一些常用方法: int length() String replace(CharSequence oldString,CharSequence newString) 用新字符串代替原字符串,返 ...

  7. php切换版本之后 redis 安装使用

    一:redis安装Download, extract and compile Redis with: $ wget http://download.redis.io/releases/redis-3. ...

  8. eBPF监控工具bcc系列五工具funccount

    eBPF监控工具bcc系列五工具funccount funccount函数可以通过匹配来跟踪函数,tracepoints 或USDT探针.例如所有以vfs_ 开头的内核函数. ./funccount ...

  9. JAVA基础——构造函数方法总结(有参构造和无参构造)

    使用构造器时需要记住: 1.构造器必须与类同名(如果一个源文件中有多个类,那么构造器必须与公共类同名) 2.每个类可以有一个以上的构造器 3.构造器可以有0个.1个或1个以上的参数 4.构造器没有返回 ...

  10. 题解 NOI2018 归程

    题解 NOI2018 归程 题意 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 n 个节点.m 条边的无向连通图(节点的编号从 1 至 n).我们依次用 l, ...