回溯写到自闭;不想就删了;

class Solution {
public:
vector<int> grayCode(int n) {
vector<vector<int>> res;
vector<int> add(n,);
DFS(res,add,n,);
vector<int> realres;
for(int i=;i < res.size();i++){
realres.push_back(func(res[i]));
}
return realres;
} int func(vector<int> nums){
int res = ;
int x = ;
for(int i=nums.size()-;i >= ;i--){
if(nums[i] == ){
res += x;
}
x = x*;
}
return res;
} int trans(int a){
if(a == ) return ;
else return ;
} void DFS(vector<vector<int>>& res,vector<int> add,int n,int pos){
if(add.size() == pos){
res.push_back(add);
}
else{
DFS(res,add,n,pos+); //其实就是递归的顺序不对!改不来
add[pos] = trans(add[pos]);
DFS(res,add,n,pos+);
}
}
};

正解:(数学方法)

// Binary to grey code
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res;
for (int i = ; i < pow(,n); ++i) {
res.push_back((i >> ) ^ i);
}
return res;
}
};

Leetcode 89的更多相关文章

  1. LeetCode 89,因为题目晦涩而被点了1500+反对的搜索问题

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第55篇文章,我们一起来看看LeetCode中的第89题 Gray Code(格雷码). 这题的官方难度是Medi ...

  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. Java实现 LeetCode 89 格雷编码

    89. 格雷编码 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输 ...

  4. Leetcode#89 Gray Code

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

  5. leetcode[89] Merge Sorted Array

    合并两个有序数组,放在A中,A中的空间足够. Given two sorted integer arrays A and B, merge B into A as one sorted array. ...

  6. Leetcode 89.格雷编码

    格雷编码 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输入: 2 ...

  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 & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  9. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

随机推荐

  1. [转载]ASP.NET中IsPostBack详解

    1.IsPostBack介绍Page.IsPostBack是一个标志:当前请求是否第一次打开. 调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者 ...

  2. 通过Jenkins + Docker实现antdPro自动化推送私服、自动容器化部署功能

    Docker与Docker私服 1. 安装docker https://docs.docker.com/install/ 2. 配置docker镜像加速 https://www.daocloud.io ...

  3. 【翻唱】Keep On Fighting

    http://video.yingtu.co/0/77868591-502c-4af1-853b-d313e83c94a9.mp4 Keep On Fighting

  4. Web负载均衡学习笔记之实现负载均衡的几种实现方式

    0x00 概要 负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用是Web负载均衡.根 ...

  5. c++性能之map实现性能比较

    http://www.cnblogs.com/zhjh256/p/6346501.html讲述了基本的map操作,在测试的时候,发现map的性能极为低下,与java相比相差了接近200倍.测试的逻辑如 ...

  6. 20145322何志威 Exp7 网络欺诈技术防范

    20145322何志威 Exp7 网络欺诈技术防范 一 实践过程记录 简单应用SET工具建立冒名网站 1 确保kali和靶机能ping通: 2 为了使得apache开启后,靶机通过ip地址可以直接访问 ...

  7. 20165211 2017-2018-2 《Java程序设计》第7周学习总结

    20165211 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 本周,我学习了书本上第十一章的内容,以下是我整理的主要知识. 第十一章 JDBC和MySQL ...

  8. Python3基础 生成器推导式 简单示例

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. 51NOD 1069 Nim游戏

    1069 Nim游戏   有N堆石子.A B两个人轮流拿,A先拿.每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出 ...

  10. Educational Codeforces Round 57 (Rated for Div. 2)

    我好菜啊. A - Find Divisible 好像没什么可说的. #include<cstdio> #include<cstring> #include<algori ...