[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 ...
随机推荐
- MERGE引擎 分表后 快速查询所有数据
MERGE存储引擎把一组MyISAM数据表当做一个逻辑单元来对待,让我们可以同时对他们进行查询.构成一个MERGE数据表结构的各成员MyISAM数据表必须具有完全一样的结构.每一个成员数据表的数据列必 ...
- [Codeforces 1242B]0-1 MST
Description 题库链接 给你一张 \(n\) 个点的完全图,其中有 \(m\) 条边长度为 \(1\),其余全为 \(0\).问你这张图的最小生成树为多少. \(1\leq n\leq 10 ...
- P3232 [HNOI2013]游走——无向连通图&&高斯消元
题意 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这条边的编 ...
- ARDUIN人体检测模块
http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-hc-sr501-motion-senso ...
- 2019.12.11 java练习
class Demo01 { public static void main(String[] args) { //数组求最大值 int[] arr={1,2,3,4,5,6,7,8,9}; int ...
- vue-cli3 ios10白屏问题解决思路
在出现了这个问题之后先不要盲目的去瞎试,根据网上的方法试了个遍也没解决问题 先看报的是什么错,再针对的解决问题 首先出现的报错是 SyntaxError: Unexpected token '*' ...
- Theano入门笔记2:scan函数等
1.Theano中的scan函数 目前先弱弱的认为:相当于symbolic的for循环吧,或者说计算图上的for循环,也可以用来替代repeat-until. 与scan相比,scan_checkpo ...
- 【2019.12.04】SDN上机第6次作业
实验拓扑 通过图形化界面建立拓扑 先清除网络拓扑 sudo mn -c 生成Python语句 #!/usr/bin/python from mininet.net import Mininet fro ...
- js注册表单中实现地区选择效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 再见,Eclipse。
阅读本文大概需要 5 分钟. 来源:cnblogs.com/ouyida3/p/9901312.html 最近,改用了 IDEA,同事都说我投敌了.当然,这些同事都是和我一样的“老”程序员.不说毕业生 ...