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.

解题思路:

这道题相当于"穷举"n个0/1排列组合,涉及到排列组合,通常可以先考虑回溯法。

设ret表示返回的数字列表,ret初始为[0,],有两种思路:

1、循环遍历ret列表n次,每次对ret中每一个元素使用"<<"左移运算,并将左移运算后的结果加1,加入列表当中;

  即{0}->{0, 1}->{00, 10, 01, 11}->{000, 100, 010, 110, 101, 011, 111}->...

  即{0}->{0, 1}->{0, 2, 1, 3}->{0, 4, 2, 6, 1, 5, 3, 7} ...

2、取出ret中已有元素的值,并将其增加,将新值插入ret中,循环n次:

  {0}           取出每个元素加入1,并将结果加入列表     ->{0, 1}                ->{0, 1}

  {0, 1}       取出每个元素加入10,并将结果加入列表    ->{0, 1, 10, 11}            ->{0, 1, 2, 3}

  {10, 11}   取出每个元素加入100,并将结果加入列表   ->{0, 1, 10, 11, 100, 101, 110, 111} ->{0, 1, 2, 3, 4, 5, 6, 7}

但是这个该死的题目要求必须要按照Leetcode给定的顺序排列数字,就是必须{0, 1, 3, 2};

因此只能使用第二种方法,并且倒序取出已有元素;

代码:

 class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result(, );
int inc = ; while (n--) {
int s = result.size();
for (int j = s - ; j >= ; --j)
result.push_back(result[j] + inc);
inc <<= ;
} return result;
}
};

【Leetcode】【Medium】Gray Code的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  6. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  7. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  8. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  9. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  10. 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion

    [Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...

随机推荐

  1. ubuntu 16 .04常见指令整理

    删除类指令   sudo rm -rf 文件名  //该指令为直接删除指令 -------------------------------------------------------------- ...

  2. (转)PXE+kickstart无人值守安装CentOS 7

    kickstart+cobbler系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 本文是PXE+kickstart无人值守安装CentOS ...

  3. flyway-Maven插件-configuration节点配置详解

    <configuration> <driver>org.hsqldb.jdbcDriver</driver> <url>jdbc:hsqldb:file ...

  4. MySql的数据查询

    SELECT语句是最常用的查询语句,它的使用方式有些复杂,但功能却相当强大.SELECT语句的基本语法如下: select selection_list//要查询的内容,选择哪些列 from数据表名/ ...

  5. 解决WORD2013输入时光标老跳的问题

    Word2013有一个非常影响使用的bug.就是在编辑文档时,光标会乱跑,影响输入.微软给出了一个kb2863845 160多MB的补丁包,安装完成后就可以解决这个问题.  补丁下载链接: 链接:ht ...

  6. MySQL 字段全部转换成小写

    原因: 因为框架某些字段大写有时候不被正确识别,所以字段都修改成小写; 特别说明:因为这里只有表,没有视图,存储过程等等其它所以我可以直接这么写; 步骤: 1.导出结构语句 2. 执行C# 脚本,替换 ...

  7. C3P0数据库连接池的java实现

    1.配置准备 导入jar包 c3p0-0.9.2-pre1.jar mchange-commons-0.2.jar 数据库驱动包,如:mysql-connector-java-5.1.28-bin.j ...

  8. CSS学习(二)

    display :   block    inline-block    inline block此元素将显示为块级元素,此元素前后会带有换行符. inline默认.此元素会被显示为内联元素,元素前后 ...

  9. xcode8 打开的 xib 踩坑

    之前开发都不敢工测试版的开发,一直用正式版的,xcode7.3.1的模糊匹配让我很蛋疼,自定义的类,类名不提示,每次都粘贴复制,8号苹果发布了 xcode8GM 版,迫不及待的从苹果开发者官网下了一个 ...

  10. hibernate cascade的真正含义

    hibernate cascade 是 @OneToOne @OneToMany @ManyToOne @ManyToMany等注解的属性,表示级联操作. /** * (Optional) The o ...