[LeetCode] 89. 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.
Example 1:
Input: 2
Output:[0,1,3,2]
Explanation:
00 - 0
01 - 1
11 - 3
10 - 2 For a given n, a gray code sequence may not be uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence. 00 - 0
10 - 2
11 - 3
01 - 1
Example 2:
Input: 0
Output:[0]
Explanation: We define the gray code sequence to begin with 0.
A gray sequence of n has size = 2n, which for n = 0 the size is 20 = 1.
Therefore, for n = 0 the gray code sequence is [0].
首先要清楚什么是格雷码,然后用位操作来处理。
Java:
public List<Integer> grayCode(int n) {
List<Integer> result = new LinkedList<>();
for (int i = 0; i < 1<<n; i++) result.add(i ^ i>>1);
return result;
}
Java:
public List<Integer> grayCode(int n) {
List<Integer> rs=new ArrayList<Integer>();
rs.add(0);
for(int i=0;i<n;i++){
int size=rs.size();
for(int k=size-1;k>=0;k--)
rs.add(rs.get(k) | 1<<i);
}
return rs;
}
Python:
class Solution(object):
def grayCode(self, n):
"""
:type n: int
:rtype: List[int]
"""
result = [0]
for i in xrange(n):
for n in reversed(result):
result.append(1 << i | n)
return result
Python:
# Proof of closed form formula could be found here:
# http://math.stackexchange.com/questions/425894/proof-of-closed-form-formula-to-convert-a-binary-number-to-its-gray-code
class Solution2(object):
def grayCode(self, n):
"""
:type n: int
:rtype: List[int]
"""
return [i >> 1 ^ i for i in xrange(1 << n)]
C++:
// Time: (2^n)
// Space: O(1)
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result = {0};
for (int i = 0; i < n; ++i) {
for (int j = result.size() - 1; j >= 0; --j) {
result.emplace_back(1 << i | result[j]);
}
}
return result;
}
};
C++:
// Time: (2^n)
// Space: O(1)
// Proof of closed form formula could be found here:
// http://math.stackexchange.com/questions/425894/proof-of-closed-form-formula-to-convert-a-binary-number-to-its-gray-code
class Solution2 {
public:
vector<int> grayCode(int n) {
vector<int> result;
for (int i = 0; i < 1 << n; ++i) {
result.emplace_back(i >> 1 ^ i);
}
return result;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 89. Gray Code 格雷码的更多相关文章
- leetCode 89.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. Given a ...
- gray code 格雷码 递归
格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- Gray Code - 格雷码
基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...
- Leetcode#89 Gray Code
原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...
- HDU 5375 Gray code 格雷码(水题)
题意:给一个二进制数(包含3种符号:'0' '1' '?' ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...
- 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 ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
随机推荐
- Vue和微信小程序区别
一.生命周期 先贴两张图: vue生命周期 小程序生命周期 相比之下,小程序的钩子函数要简单得多. vue的钩子函数在跳转新页面时,钩子函数都会触发,但是小程序的钩子函数,页面不同的跳转方式,触发的钩 ...
- js插件---videojs的使用
js插件---videojs的使用 一.总结 一句话总结: 网上有各种细致的现成的代码可以拿来用,没必要自己死专 1.video.js有两种初始化方式? 一种是在video的html标签之中 一种是使 ...
- 在linux中使用Sqlplus命令登录MySQL,查看表并设置行数和宽度,使其正常显示
在linux中使用sqlplus命令进入MySQL,设置行数和行宽 1) 查看目前的pagesize,默认是14: 1. show pagesize; 2. set pa ...
- 不用VS调试.Net
将来,任何开发人员都将需要调试应用程序,并且将无法访问Visual Studio,在某些情况下甚至无法访问源代码.例如,在生产web或应用服务器上调试问题时,我真的不想安装Visual Studio并 ...
- pgloader 方便的数据迁移工具
pgloader 是一个支持多种数据源迁移到pg 数据库的工具,高性能,使用灵活同时作者 也提供了docker 版本的镜像,今年3月份使用此工具的时候,发现好久都没更新了,但是 最近作者有了新版本的发 ...
- Collecting metrics with the PostgreSQL and TimescaleDB output plugin for Telegraf
转自:https://docs.timescale.com/v1.3/tutorials/telegraf-output-plugin 文章演示了如何使用pg output 插件 以及Telegraf ...
- Linux 系统管理——服务器RAID及配置实战
RAID称为廉价磁盘冗余阵列.RAID的基本想法是把多个便宜的小磁盘组合在一起.成为一个磁盘组,使性能达到或超过一个容量巨大.价格昂贵的磁盘. 2.级别介绍 RAID 0连续以位或字节为单位分割数据, ...
- hhhhh我进步啦!
不要相信上面的标题,其实我是 一直都很强的 蒟蒻中的蒟蒻(卑微☹) 今天早上刷完水题后突然发现自己竟然进了一本通网站“排名”的首页(第 100 名欸),不禁 嘚瑟 开森不已. ☟☟☟ 继续A题↖(^ω ...
- HDU-5215 Cycle(边双/判奇偶环)
题目 HDU-5215 Cycle 网上那个啥dfs的垃圾做法随便弄组数据已经hack掉了 做法 纯奇环偶环通过dfs树上,染色判断(由于偶环可能有两个奇环,通过一点相交,dfs树上并不能判完) 两环 ...
- 【BIEE】报表导出数据只显示500行,如何解决?
BIEE报表展示的时候每页只显示500行,进而导致导出的时候也只能导出500行,客户抱怨:每次只能导出500行,导出后还得自己合并! 解决思路: 1.找到路径$BIEE_HOME\instances\ ...