- 浏览: 173147 次
- 性别:
- 来自: 济南
文章分类
最新评论
首先简单介绍一下什么是格雷码。在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code),另外由于最大数与最小数之间也仅一位数不同,即“首尾相连”,因此又称循环码或反射码。有关格雷码的详细讲解请大家查阅网上其它资料,这里主要讲解格雷码是如何编码的。
我们不妨从简单例子开始进一步看一下什么是格雷码,假设n代表格雷码的位数,当n=1时,我们只有两种选择(0, 1); 当 n =2 时,格雷码为(00, 01, 11, 10); 当n = 3时,格雷码为(000, 001, 011,010, 110, 111, 101, 100);我们发现了一个规律,就是每当增加一位,新的格雷码的前半段都是在之前的格雷码前面加上一个零,红色地方代表了格雷码的前半段。而后半段就是从后面开始在前面加上1。通过这种方式就可以写出任意位数的格雷码。
leetcode中有一道题目是关于格雷码的,在这里列举一下。给定一个整数n,输出它的格雷码序列。格雷码序列不唯一,输出一种就可以。按照上面的编码规则,我们不难写出代码,代码如下:
我们不妨从简单例子开始进一步看一下什么是格雷码,假设n代表格雷码的位数,当n=1时,我们只有两种选择(0, 1); 当 n =2 时,格雷码为(00, 01, 11, 10); 当n = 3时,格雷码为(000, 001, 011,010, 110, 111, 101, 100);我们发现了一个规律,就是每当增加一位,新的格雷码的前半段都是在之前的格雷码前面加上一个零,红色地方代表了格雷码的前半段。而后半段就是从后面开始在前面加上1。通过这种方式就可以写出任意位数的格雷码。
leetcode中有一道题目是关于格雷码的,在这里列举一下。给定一个整数n,输出它的格雷码序列。格雷码序列不唯一,输出一种就可以。按照上面的编码规则,我们不难写出代码,代码如下:
public class Solution { public List<Integer> grayCode(int n) { List<Integer> list = new ArrayList<Integer>(); if(n < 0) return list; list.add(0); for(int i = 1; i <= n; i++) for(int j = list.size() - 1; j >= 0; j--) { list.add(list.get(j) + (int)Math.pow(2, i - 1)); } return list; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 224Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 222You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 339Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 330Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 448Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 427Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 425The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 383Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 514Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 528Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 366All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 858Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 879Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 552Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 598Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 761Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 731You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 626For a undirected graph with tre ...
相关推荐
Gray code Time Limit:2000MS Memory Limit:65536K Total Submit:242 Accepted:121 Description Gray code is an interesting code sequence and has many applications in computer science.No matter you ...
Gray code is an interesting code sequence and has many applications in computer science.No matter you have known it before or not, here are some introductions about its features: (1) Gray code has 2^...
Gray Code是一个数列集合,每个数使用二进位来表示,假设使用n位元来表示每个数好了,任两个数之间只有一个位元值不同,例如以下为3位元的Gray Code: 000 001 011 010 110 111 101 100 由定义可以知道,Gray Code...
基于《Video Block Motion Estimation Based on Gray-Code Kernels》《The Gray-Code Filter Kernels》《Real-Time Pattern Matching Using Projection Kernels》这三篇论文的具体实现
本文件的目的在于生成格雷码,用途在于三维测量,可以自行修改相关周期
ACM Gray code 能accepted Description Gray code is an interesting code sequence and has many applications in computer science.No matter you have known it before or not, here are some introductions ...
模擬Coherent 8PSK 之 BER
1721_Gray code.cpp 参考代码
Grayp_graycode_源码.zip
二进制格雷码(23
将二进制数转化为格备码,4位并行。binary_input为二进制数输入, gray_output为格雷码输出。
两个bit的格雷码通过一个bit的格雷码序列产生:原始序列前面加上"0",然后把原始序列反序,前面加上"1",最后放在一起形成两个bit的格雷码;
格雷码(24,12),ECC(差错控制编码,林舒,第二版)中的码型,最小距离7
一个格雷码计数器,利用Verilog语言实现,一个初学者的好例子。
开发环境是FPGA开发工具,格雷码计数器的VHDL程序
格雷码(循环二进制单位距离码)是任意两个相邻数的代码只有一位二进制数不同的编码,它 与奇偶校验码同属可靠性编码。 从对应的 n 位二进制码字中直接得到 n 位格雷码码字,需要先对 n 位二进制的码字,从右到 ...
Can Gray code improve the performance of distributed video coding?
格雷码的实现方法,代码清晰,思路明确易懂。
格雷码(Gray Code)是一种在位置传感器中常用的编码,其特点是每两个相邻的数的格雷码只有一个二进制位是不同的。例如,0-7的格雷码编码如下: 0 000 1 001 2 011 3 010 4 110 5 111 6 101 7 100 普通二进制编码到...
Gray_Code.cpp