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

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. [转][C#][WebApi]

    在 WebApi 中获取网页在服务器上的位置可以使用以下两种方式: string filePath = HostingEnvironment.MapPath(string.Format("/ ...

  2. [UE4]手柄导航 Navigation

    Navigation是对应游戏手柄.Left.Right.Up.Down.Next.Previous分别对应游戏手柄上的左.右.上.下.下一个.上一个按键. Left.Right.Up.Down.Ne ...

  3. [UE4]位移和形变 Render Transform

      任何UI控件都有Render Transform属性. 一.Transform,对应游戏场景中的Transform 1.Translation:位置,平移.对应游戏场景的Transform中的Lo ...

  4. [UE4]Tool Tip - 提示信息

    一.每一个Widget都有Tool Tip,在运行时鼠标移动到UI上,就会显示填写的Tool Tip文字   二.Toop Tips的字体样式和大小不可更改.但是可以Tool Tip可以绑定到一个Wi ...

  5. String,StringBuilder和StringBuffer区别

    String字符串常量 StringBuilder 字符串变量(非线程安全) StringBuffer  字符串变量(线程安全) 1.String String是字符串常量,为不可改变对象 Strin ...

  6. PHP微信公众号开发之基本配置

    (提示:需要有服务器或云虚拟机) 一开始不明白公众号不是可以这样管理吗?                那么为什么用开发平台进行公众号开发,官方文档是这样说的 为了识别用户,每个用户针对每个公众号会产 ...

  7. celipse关联hadoop源码

    可以在这里下载hadoop的源码包 https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/ 我自己下载的是hadoop2.6.0的源码包 ...

  8. Struts2学习:Action使用@Autowired注入为null的解决方案

    1.pom.xml引入struts2-spring-plugin <dependency> <groupId>org.apache.struts</groupId> ...

  9. vue的异步组件按需加载

    当build打包后,app.js过大的时候,可以考虑用异步组件的方式. import HomeHeader from "./components/Header"; import H ...

  10. 微信小程序内容组件图标 icon

    小程序内置了一下图标可以用 需要自定义图标的看这里 ==>微信小程序中使用iconfont/font-awesome等自定义字体图标 小程序内置图标使用示例 <icon type=&quo ...