第一眼看到就是枚举,回溯法。

n位的ans就是在n-1的ans的基础上,每一个在首位加上1。

但是有个难点,要保证相邻两数之间只有一位在变化,怎么办?

首先

00

00  01

00  01  11  10

本来是傻乎乎的,直接加的,没有保证只变化1位。

后来发现,从00到01是只变了1位,那么我们给01加上2呢?就变成11, 给00加上2就变成 10。

每次逆序加2^n-1,这样就保证了相邻只变一位。

说不清,基本是蒙对的。碰上了。上代码吧。

class Solution {
public:
vector<int> grayCode(int n) {
vector<int> ans; ans.push_back();
for(int i=;i<n;i++)
{
int len = ans.size();
for(int j = len-; j >=; j--)
{
ans.push_back(ans[j] + pow(,i));
}
}
return ans;
}
};

leetcode 题解: Gray Code的更多相关文章

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

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

  2. [LeetCode] 89. Gray Code 格雷码

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

  3. 【LeetCode】Gray Code

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

  4. 【leetcode】Gray Code (middle)

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

  5. leetcode[88] Gray Code

    题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...

  6. Java for LeetCode 089 Gray Code

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

  7. leetCode 89.Gray Code (格雷码) 解题思路和方法

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

  8. LeetCode题目:Gray Code

    原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...

  9. Leetcode#89 Gray Code

    原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...

  10. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

随机推荐

  1. 数据库SQL语言学习--上机练习3(插入 更新 删除)

    上机练习3 . 将一个新学生记录(学号::姓名:陈冬:性别:男:所在系:信息系:年龄:20岁)插入到Student表中: ALTER TABLE Student ,); UPDATE Student ...

  2. Python——ipython(python programming)

    Tab自动补充 Ctrl+c中断程序 ?帮助调出文档   _得到上次的结果 ,__的到上上次结果,___得到上上次结果  %开头的为魔术命令  %timeit 得到运算时间,多次求平均  %%time ...

  3. [转]Win 10 的 Win 按键没反应

    来自:http://www.pc0359.cn/article/win10/66397.html 方法步骤: 1.首先我们需要将任务管理器运行出来.方法有两种,第一种:使用鼠标右键单击屏幕下方的任务栏 ...

  4. gulp_css2js

    var gulp = require('gulp'); var rename = require('gulp-rename'); var concat = require('gulp-concat') ...

  5. Windows Server 2012 NAT端口转发

     

  6. SOLR缓存调优

    缓存在 Solr 中充当了一个非常重要的角色,Solr 中主要有这三种缓存: Filter cache(过滤器缓存),用于保存过滤器(fq 参数)和层面搜索的结果 Document cache(文档缓 ...

  7. pycharm中导入requests,xmlx等模块的方法。

    现在pycharm的功能越来越强大,我们需要什么直接就可以导入: 下面正式开始介绍: 第一步: 先选择左边的:project interpreter   ----->再点击后面的绿色的加号. 那 ...

  8. dubbo-文档

    Srping版Dubbo集成中文地址:https://dubbo.gitbooks.io/dubbo-user-book/content/preface/background.html SpringB ...

  9. C# WPF DevExpress 图表控件之柱状图

    说明:DevExpress版本是17.1.VS是2015. XAML: <!--#region 图表控件--> <dxc:ChartControl x:Name="char ...

  10. MVC字符串转json,ajax接受json返回值

    #region 功能 /// <summary> /// 查询 微信用户一定年月的账单 /// </summary> /// <param name="year ...