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 ...
随机推荐
- redis+mysql读写方案
前言:在web服务端开发的过程中,redis+mysql是最常用的存储解决方案,mysql存储着所有的业务数据,根据业务规模会采用相应的分库分表.读写分离.主备容灾.数据库集群等手段.但是由于mysq ...
- SpringMvc下的文件上传
首先是springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...
- Android Programming 3D Graphics with OpenGL ES (Including Nehe's Port)
https://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html
- Angular——MVC模式开发实战
创建项目 创建工作目录 使用bower下载需要插件 git init.add.commit之后得到分支master,再创建developer分支,然后再此分支上进行具体功能开发 MVC架构 之前小项目 ...
- Assembly之instruction之JC
JC Jump if carry setJHS Jump if higher or same Syntax JC label JHS label Operation If C = 1: PC + 2 ...
- arx移植 及预处理器
来源:http://bbs.mjtd.com/thread-102486-1-1.html 另,ObjectARX编程参考:http://bbs.mjtd.com/forum-14-1.html 如果 ...
- CAD从二进流加载数据(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::ReadBinStreamEx 从二进流加载数据,详细说明如下: 参数 说明 IMxDrawBinStream* pBinStream ...
- 牛客多校Round 8
Solved:2 rank:164 签了两个oeis,但这样真的开心嘛
- Bat 脚本(常用命令)
Bat 批处理脚本 (常用) Bat 批处理脚本 === Content === 1. Rem 和 :: Rem 为注释命令,能回显. :: 为符号注释,不能回显. %行内注释内容% ===== (不 ...
- Mac安装virtualwrapper时报错No module named virtualenvwrapper
1. 前言 我在使用mac安装virtualwrapper的时候遇到了问题,搞了好长时间,才弄好,在这里总结一下分享出来,供遇到相同的问题的朋友使用,少走些弯路. 2. 问题说明 Mac默认系统的py ...