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

思路: Gray Code的意思就是n位的二进制序列,相邻两个只有一位不同。观察

000 - 0
001 - 1
011 - 3
010 - 2
110 - 6
111 - 7
101 - 5
100 - 4

第0位的序列为 01 10 01 这样不断重复

第1位的序列为 0011 1100

第2位的序列为 11110000

这样我们就找到了规律。

我最开始是通过判段每次是第几位变化,通过异或得到新值。 每次第k为变化时满足 i = 2k + n*2(k+1)

vector<int> grayCode(int n) {
vector<int> ans;
ans.push_back();
if(n == )
return ans; for(int i = ; i < ( << n); i++)
{
int bit_change = ;
for(int j = ; j < n; j++)
{
if(i % ( << (j + )) - ( << j) == )
{
bit_change = j; break;
}
}
int cur = ( << bit_change);
cur ^= ans.back();
ans.push_back(cur);
}
return ans;
}

后来看其他人的发现更简单的方法

vector<int> grayCode2(int n) {
vector<int> ans;
ans.push_back();
if(n == )
return ans; for(int i = ; i < n; i++)
{
int inc = << i;
for(int j = ans.size() - ; j >= ; j--) //每次等第i - 1位正反序都存完毕时,第i位起始为0的情况也存储完了, 只需存储第i位起始为1并且低位倒序输出
{
ans.push_back(ans[j] + inc);
}
}
return ans;
}

【leetcode】Gray Code (middle)的更多相关文章

  1. 【LeetCode】Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

  2. 【题解】【排列组合】【回溯】【Leetcode】Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  3. 【Leetcode】【Medium】Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  4. 【leetcode】Sort List (middle)

    Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...

  5. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  6. 【leetcode】Word Search (middle)

    今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  7. 【leetcode】 Permutation Sequence (middle)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. 【leetcode】 Generate Parentheses (middle)☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

随机推荐

  1. log4net--不可多得的开源日志记录组件

    log4net--不可多得的开源日志记录组件 1 前奏 一直在用log4net日志工具,却没时间写个日志给大家分享一下这个工具,趁最近比较空些,好好分享一下这个工具. 2 说明 Log4net介绍就不 ...

  2. EasyUI中datagrid控件的使用 设置多行表头(两行或多行)

    EasyUI中的datagrid控件十分强大,能生成各种复杂的报表,现在因为项目需要,需要生成一个表头两行的表,找了一些说明文档,以下用一个实例来说明一下: 第一种方法: $('#divData'). ...

  3. loadrunner-27796错误寻求解决办法

    Action.c(58): Error -27796: Failed to connect to server "www.baidu.com:80": [10048] Addres ...

  4. FineUI第十八天---表格之事件的处理

    表格之事件的处理: 1.事件参数: GridPageEventArgs:表格分页事件参数,对应onPageIndexChange事件. NewPageIndex:新页面的索引 GridSortEven ...

  5. Codeforces Round #340 Watering Flowers

    题目: http://www.codeforces.com/contest/617/problem/C 自己感觉是挺有新意的一个题目, 乍一看挺难得(= =). 其实比较容易想到的一个笨办法就是:分别 ...

  6. 转一篇简易易懂的android回调的实现--->(转的)

    回调机制在 Android 监听用户界面操作中的体现   本文讨论以下两个内容: 1. 回调函数 2. 回调机制在 Android框架 监听用户界面操作中的作用 一 回调函数 回调函数就是一个通过函数 ...

  7. Android应用性能优化

    整理自http://androidperformance.com的几篇博客 代码内存优化-Java篇 避免创建不必须的对象,虽然GC可以回收不用的对象,但为对象分配内存和回收它们同样是需要消耗资源的. ...

  8. 两轮自平衡小车双闭环PID控制设计

                                                                                            两轮自平衡小车的研究意义 ...

  9. php源码安全加密之PHP混淆算法.

    php源码安全加密的前世今生,本想发在教程区中.不知道怎么发,就写在这里面吧.PHP加密,解密是一直的话题,本人菜鸟,今天就简单向大家介绍一下并说说其中原理.提供一些加密的混淆算法.一\PHP的加密总 ...

  10. CodeVS 2845 排序的代价

    Description 给你一个数列使他递增,交换两个元素的代价为两个数的和,最小化代价. Sol 置换群+离散化. 使一个数列恢复递增顺序,那么,他和他要到达的位置的数需要交换,这样就形成了一个置换 ...