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.
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 (格雷码) 解题思路和方法的更多相关文章
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- gray code 格雷码 递归
格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- HDU 5375 Gray code 格雷码(水题)
题意:给一个二进制数(包含3种符号:'0' '1' '?' ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...
- Gray Code - 格雷码
基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...
- Leetcode#89 Gray Code
原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...
- 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 ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- JDBC使用游标实现分页查询的方法
本文实例讲述了JDBC使用游标实现分页查询的方法.分享给大家供大家参考,具体如下: /** * 一次只从数据库中查询最大maxCount条记录 * @param sql 传入的sql语句 * @par ...
- 后台接收不到postman发送的xml参数的解决办法
首先在body下复制需要传的xml: 然后点击url右边的Params,添加key和value.value和body下的xml是一样的: 最后点击send,后台就能接收到参数了.
- Win7 下 PB (PowerBuilder) Insert Control 崩溃的解决办法
环境: WIN7 x86 PB8.0, x64系统目录不同,不过也可以试试 Insert -> OLE... -> Insert Control - 崩溃 如果网上提供的办法解决不了你 ...
- [转]汇编语言:MOVSB,MOVSW,MOVSD
汇编语言:MOVSB,MOVSW,MOVSD 转自: http://blog.csdn.net/zhenyongyuan123/article/details/8364011 目前80386系列的 ...
- STL_map的使用
转自:http://www.kuqin.com/cpluspluslib/20071231/3265.html Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在m ...
- CAD如何设置系统变量
主要用到函数说明: MxDrawXCustomFunction::Mx_SetSysVar 设置系统变量.详细说明如下: 参数 说明 CString sVarName 系统变量名 Value 需要设置 ...
- Eclipse报错:Setting property 'source' to 'org.eclipse.jst.jee.server:xx' did not find a matching property
Shell代码 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to ' ...
- ThinkPHP---案例2--部门管理功能
[一]部门列表展示 分析: ①控制器DeptController.class.php ②方法showList(不要使用list方法,因为list是关键词) ③模板文件:showList.html 下面 ...
- HDU多校Round 1
Solved:5 rank:172 A.Maximum Multiple #include <stdio.h> #include <algorithm> #include &l ...
- LTTng
Waiting for dig... http://frederic-wou.net/lttng/