[转载]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 ...
随机推荐
- Scanline Fill Algorithm
https://www.sccs.swarthmore.edu/users/02/jill/graphics/hw3/hw3.html http://web.cs.ucdavis.edu/~ma/EC ...
- Antenna Placement---poj3020(最大匹配)
题目链接:http://poj.org/problem?id=3020 题意:每个 ‘*’都需要一个1*2的东西覆盖,问最少需要多少个1*2的东西来覆盖这些‘*’ 和Oil Skimming的题解几乎 ...
- 【Javascript】Windows下Node.js与npm的安装与配置
1:先下载Node.js,网站https://nodejs.org/en/,左侧为稳定版,右侧为最新版,推荐稳定版 2:Node.js安装,运行下载后的.msi文件,一路下一步就可以了,我选择的安 ...
- java8工具类使用
1:map的使用 computeIfPresent ,如果键已经存在,将键和值作为参数传到函数式中,计算返回新的值 import java.util.HashMap; import java.util ...
- soapUi下载
http://dl.eviware.com/list_soapui2.html http://smartbearsoftware.com/repository/eviware/jars/
- samba安装测试
1.检查是否系统有自带的samba安装包 2.关闭防火墙 Iptables -F Systemctl disable firewalld Systemctl stop firewalld System ...
- 探索C++虚函数在g++中的实现
本文是我在追查一个诡异core问题的过程中收获的一点心得,把公司项目相关的背景和特定条件去掉后,仅取其中通用的C++虚函数实现部分知识记录于此. 在开始之前,原谅我先借用一张图黑一下C++: “无敌” ...
- Linux 笔记 #01# 搭建 Python 环境 & vim 代码高亮
日常收集 vim editor: How do I enable and disable vim syntax highlighting? 搭建 Python 环境 vim editor: How d ...
- Mysql性能调优工具Explain结合语句讲解
Explain简称执行计划,可以模拟SQL语句,来分析查询语句或者表结构是否有性能瓶颈.Explain的作用有哪些,可以看到哪些?可以看到表的读取顺序,数据读取操作的操作类型,哪些索引可以使用,哪些索 ...
- 2017 java期末上机练习
仅供参考! 一.最大值.最小值.平均数 package examination; import java.util.Arrays; import java.util.Scanner; /** * 1. ...