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

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. P3374 【模板】树状数组 1(cdq)

    P3374 [模板]树状数组 1 cdq分治 刚学了cdq分治(dyf神犇强力安利下),发现可以做这种题,当然是来试水了(逃 cdq好像只能离线的样子 cdq分治(转) 以下是摘录的几句: 在合并的时 ...

  2. 使用requireJS加载不符合AMD规范的js文件:shim的使用方式和实现原理

    原文链接: http://www.bubuko.com/infodetail-671521.html

  3. QVector排序

    QVector<double> tempX ; qSort(tempX.begin(), tempX.end());//从小到大排序

  4. cmd命令安装、卸载、启动和停止Windows Servic

    1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:/Windows/Microsoft.NET ...

  5. luoguP2572 [SCOI2010]序列操作

    题目&&链接 反正数据都是一样的,luogu比较友好 luogu bzoj lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变 ...

  6. RHEL7使用NAT方式上网

    1.首先,Windows7无法设置网络共享VMNet8的问题,是因为禁用了Firewall服务,设置为自动,启用即可:且需要启动VMWare的DHCP和NAT两个服务,这两个服务在我的机器上是关闭的, ...

  7. nginx配置二级域名

    我在我的服务器上面跑了两个node应用程序,分别一个端口2368跑的是ghost博客,一个端口8000跑的是我的demo程序.想要一级域名zhangruojun.com用来访问博客,二级域名demo. ...

  8. .NET MVC请求流程

    ASP.NET MVC 请求流程:Controller MvcHandler Action Action参数赋值 .NET MVC权限设计思考之切入点

  9. hive学习4(hive的脚本执行)

    hive的脚本执行 hive -e "SQL" hvie -f file 实例 [root@spark1 ~]# hive -e "show tables" # ...

  10. HDU 6143 Killer Names(容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意: 用m个字母去取名字,名字分为前后两部分,各由n个字符组成,前后两部分不能出现相同字符,问合法的组成 ...