[转载]LeetCode: 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.
二进制码->格雷码(编码):从最右边一位起,依次将每一位与左边一位异或(XOR),作为对应格雷码该位的值,最左边一位不变(相当于左边是0);
格雷码->二进制码(解码):从左边第二位起,将每位与左边一位解码后的值异或,作为该位解码后的值(最左边一位依然不变)。
Gray Code 0 = 0, 下一项是toggle最右边的bit(LSB), 再下一项是toggle最右边值为 “1” bit的左边一个bit,然后重复。直到最右边值为 “1” 的bit在最左边了,结束。
- class Solution {
- public:
- vector<int> grayCode(int n) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- vector<int> result;
- int nSize = 1 << n;
- for (int i = 0; i < nSize; ++i)
- {
- result.push_back((i>>1)^i);
- }
- return result;
- }
- };
[转载]LeetCode: Gray Code的更多相关文章
- [LeetCode] 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]Gray Code @ Python
原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
- LeetCode: Gray Code [089]
[题目] The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
- LeetCode: Gray Code 解题报告
Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit ...
- Leetcode Gray Code
题目的意思就是将十进制转换成格雷码 首先将二进制转换成格雷码 根据此图可以看出二进制的第i和第i+1位异或为格雷码的第i+1位,对于给定的十进制数x,其(x>>1)相当于二进制向右移动一位 ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- [LeetCode]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
随机推荐
- Python之numpy基本指令
https://blog.csdn.net/mmm305658979/article/details/78745637 # -*- coding: utf-8 -*- 多加练习才是真 import n ...
- hive引入jar包--HIVE.AUX.JARS.PATH和hive.aux.jars.path
hive需要引入包时?该怎么引入? 一.hive-site.xml中的hive.aux.jars.path 此配置项对于hive server有效,但是是不会作用到hive shell.也就是说即使你 ...
- Shape of passed values is (3490, 21), indices imply (3469, 21)
背景 处理DataFrame数据时,抛了这个错误:Shape of passed values is (3490, 21), indices imply (3469, 21) 解决 数据出现重复,导致 ...
- Navicat连接服务器上的Mysql数据库
- 2014年百度之星程序设计大赛 - 资格赛 1001 Energy Conversion
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/sr19930829/article/details/26003661 Energy Conversi ...
- 102-advanced-代码分割
1.Bundling 大多数React应用程序将使用Webpack或Browserify等工具“捆绑”文件.捆绑是跟踪导入的文件并将它们合并到单个文件中的过程:“捆绑”.然后,该包可以包含在网页中以一 ...
- 微软官方推出的win10安装或者创建安装u盘的工具
https://www.microsoft.com/zh-cn/software-download/windows10 下载安装后,可根据提示,一步步的安装win10或者创建安装u盘
- 表单(中)-EasyUI Combogrid 组合网格、EasyUI Numberbox 数字框、EasyUI Datebox 日期框、EasyUI Datetimebox 日期时间框、EasyUI Calendar 日历
EasyUI Combogrid 组合网格 扩展自 $.fn.combo.defaults 和 $.fn.datagrid.defaults.通过 $.fn.combogrid.defaults 重写 ...
- 数据块加密模式以及IV的意思
(本文资料主要来自:http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation) 目前流行的加密和数字认证算法,都是采用块加密(block ...
- cocos代码研究(19)Widget子类ImageView学习笔记
理论基础 显示图片的小控件,继承自 Widget . 代码实践 static ImageView * create()创建一个空的ImageView static ImageView * create ...