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
算法分析:首先要明白什么是格林码。给定n,格林码就有2^n个,前面一半是n-1的格林码,后面一半是n-1的格林码的逆序加上1<<(n-1)得到的相邻格林码二进制位只差一位。利用递归即可求解。
1位格雷码有两个码字
(n+1)位格雷码中的前2^n个码字等于n位格雷码的码字,按顺序书写,加前缀0
(n+1)位格雷码中的后2^n个码字等于n位格雷码的码字,按逆序书写,加前缀1。
由于是二进制,在最高位加0跟原来的数本质没有改变,所以取得上一位算出的格雷码结果,再加上逆序添1的方法就是当前这位格雷码的结果了。
n = 0时,[0]
n = 1时,[0,1]
n = 2时,[00,01,11,10]
n = 3时,[000,001,011,010,110,111,101,100]
public class GrayCode
{
public List<Integer> grayCode(int n)
{
if(n == 0)
{
List<Integer> res = new ArrayList<>();
res.add(0);
return res;
} List<Integer> res = grayCode(n-1);
int addNumber = 1 << (n-1);//得到二进制数的十进制
for(int i = res.size() - 1; i >=0; i --)
{
res.add(res.get(i)+addNumber);
}
return res;
}
}
Gray Code,求格林码的更多相关文章
- LeetCode OJ: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. Gi ...
- leetCode 89.Gray Code (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- c++实现gray code(格雷码)
今天别人问的一道题,强调用分治法实现 =.= 百度了一下格雷码,然后写了一下. 关于格雷码大家看百度的吧,特别详细,贴个图: 代码如下(header_file.h是我自己写的一个头文件,包括常见的ve ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 格雷码Gray Code详解
格雷码简介 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code),另外由于最大数与最小数之间也仅一位数不同,即“首尾相连”,因此又称循环码或反射码.格 ...
- 格雷码(Gray code)仿真
作者:桂. 时间:2018-05-12 16:25:02 链接:http://www.cnblogs.com/xingshansi/p/9029081.html 前言 FIFO中的计数用的是格雷码, ...
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- QAM格雷码映射的规则(Gray Code Mapping in QAM)
高阶调制(QAM,MQAM)信号中做基带映射,格雷码作为一种规范的映射规则,加上I,Q方向上相邻两个星座点对应的Bit_Cluster中只有一个Bit不同,所以有方便统一的特性. 以16QAM为例,先 ...
随机推荐
- style2paints、deepcolor、sketchkeras项目
数据集不够怎么办? 1 一些传统的边缘提取算法可以提取图像边缘. 2 这里我们有一个使用神经网络提取线稿图的项目——sketchkeras 源码:https://github.com/lllyasvi ...
- leetcode 去除单链表倒数第k个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 借助 Django 的 smart_str 和 smart_unicode 进行编码转换(转)
原文:http://www.dirk.sh/diary/using-django-smart_str-smart_unicode/ Django 为字符编码的转换提供了非常简洁的方法: 1.djang ...
- geo实现方案
1.数据库内在支持GIS(地理信息系统) MySQL: 目前只有MyISAM引擎是支持GIS的,Innodb在5.7版本中才支持空间索引.MyISAM这个引擎不支持事务.外键,而且是表锁.适合读为主, ...
- MySQL存储引擎与事务
1.作用 和磁盘的数据打交道 2.简介 MySQL 基于存储引擎管理 表空间数据数据文件 3.种类 Innodb存储引擎ibd:存储表的数据行和索引frm:表基本结构信息Myisam存储引擎frmm ...
- Java集合—List(转载)
本篇文章将集中介绍了List集合相比Collection接口增加的一些重要功能以及List集合的两个重要子类ArrayList及LinkedList. 一.List集合 List作为Collectio ...
- EXPORT_SYMBOL使用
EXPORT_SYMBOL只出现在2.6内核中,在2.4内核默认的非static 函数和变量都会自动导入到kernel 空间的, 都不用EXPORT_SYMBOL() 做标记的.2.6就必须用EXPO ...
- Parallel Programming-实现并行操作的流水线(生产者、消费者)
本文介绍如何使用C#实现并行执行的流水线(生产者消费者): 1.流水线示意图 2.实现并行流水线 一.流水线示意图 上图演示了流水线,action1接收input,然后产生结果保存在buffer1中, ...
- Linux服务器access_log日志分析及配置详解(二)
默认nginx / Linux日志在哪个文件夹? 一般在 xxx.xxx.xxxx.com/home/admin 路径下面的error.log文件和access.log文件error_log logs ...
- 接管radiobutton onclick 事件
You can nil the OnClick event handler while changing a radiobutton state programmatically: procedure ...