89. Gray Code (Bit)
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.
思路:
二进制-->格雷码:如果二进制码字的第i位和i+1位相同,则对应的格雷码的第i位为0,否则为1。最高为之前可以看作还有一位0,即最高位是和0异或。
*格雷码-->二进制:最左边一位不变,从左边第二位起,将每位与左边一位解码后的值异或,作为该位解码后的值。
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result;
int size = << n;
for (int i =; i< size; i++)
{
result.push_back(getGrayCode(i));
}
return result;
}
int getGrayCode(int binary)
{
return ((binary >> )^binary);
}
};
89. Gray Code (Bit)的更多相关文章
- 89. Gray Code - LeetCode
Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 10 11 n = 3 000 001 010 011 100 10 ...
- 89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 【LeetCode】89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 89. Gray Code(中等,了解啥是 gray code)
知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- 89. Gray Code返回位运算的所有生成值
[抄题]: The gray code is a binary numeral system where two successive values differ in only one bit. G ...
- LeetCode OJ 89. Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
- 89. Gray Code(公式题)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】89. Gray Code (2 solutions)
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
随机推荐
- 2018-2019 20165226 Exp5 MSF基础应用
2018-2019 20165226 Exp5 MSF基础应用 目录 一.实验内容说明及基础问题回答 二.实验过程 Task1 主动攻击实践 ms08_067 ms17_010 Task2 针对浏览器 ...
- 13.MD5对用户密码进行加密
MD5概述 用户名密码保存在客户端是一种十分危险的行为.所以需要进行加密后保存. 其中MD5就是一种比较常用的加密算法. 与其说MD5算法是一种加密算法,不如说是一种数据指纹(数据摘要)算法. 其特点 ...
- linux中convert用法
转: 强大的convert命令 convert命令可以用来转换图像的格式,支持JPG, BMP, PCX, GIF, PNG, TIFF, XPM和XWD等类型,下面举几个例子: convert ...
- python logging 日志模块的配置和使用
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(line ...
- vlc的应用之二:vlc的ActiveX及cab
请移步https://higoge.github.io/,所有下载资料在那个博客都能找到.谢谢. http://jeremiah.blog.51cto.com/ 2009-05-14补充:8. Act ...
- Python:23种Pandas核心操作
Pandas 是一个 Python 软件库,它提供了大量能使我们快速便捷地处理数据的函数和方法.一般而言,Pandas 是使 Python 成为强大而高效的数据分析环境的重要因素之一.在本文中,作者从 ...
- webpack(5)--Resolve
Resolve webpack在启动后会从配置的入口模块触发找出所有依赖的模块,Resolve配置webpack如何寻找模块对应的文件.webpack内置JavaScript模块化语法解析功能,默认会 ...
- 处理存在UNASSIGNED的主分片导致Elasticsearch集群状态为Red的问题
我们默认是开启了自动分配的,但仍然会因为服务器软硬件的原因导致分配分配失败,从而出现UNASSIGNED的分片,如果存在该状态的主分片则会导致集群为Red状态.此时我们可以通过reroute API进 ...
- django项目分页
测试版本 代码: # 测试分页users=[{'name':'alex{}'.format(i),'pwd':'aaa{}'.format(i)}for i in range(1,302)] def ...
- ubuntu16.04安装python3,numpy,pandas等量化计算库
ubunt安装python3 sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install ...