Leetcode 89
回溯写到自闭;不想就删了;
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的更多相关文章
- LeetCode 89,因为题目晦涩而被点了1500+反对的搜索问题
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第55篇文章,我们一起来看看LeetCode中的第89题 Gray Code(格雷码). 这题的官方难度是Medi ...
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- Java实现 LeetCode 89 格雷编码
89. 格雷编码 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输 ...
- Leetcode#89 Gray Code
原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...
- leetcode[89] Merge Sorted Array
合并两个有序数组,放在A中,A中的空间足够. Given two sorted integer arrays A and B, merge B into A as one sorted array. ...
- Leetcode 89.格雷编码
格雷编码 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输入: 2 ...
- leetCode 89.Gray Code (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- Python 除法运算
Python中的除法较其它语言显得非常高端,有套很复杂的规则.Python中的除法有两个运算符,/和// 首先来说/除法: 在python 2.x中/除法就跟我们熟悉的大多数语言,比如Java啊C啊差 ...
- C/C++中的位运算
位运算 位运算的运算分量只能是整型或字符型数据,位运算把运算对象看作是由二进位组成的位串信息,按位完成指定的运算,得到位串信息的结果. 位运算符有: &(按位与).|(按位或) ...
- Linux启动vi编辑器时提示E325: ATTENTION解决方案
Linux启动vi编辑器时提示E325: ATTENTION解决方案 Vi编辑器是Linux的文本编辑器,在Linux系统的运用非常广泛,不少朋友在打开Vi编辑器的时候提示E325: ATTENTIO ...
- 01: git & github
目录:GIT其他篇 01: git & github 02: git分支管理 目录: 1.1 常见版本管理工具介绍 及 版本工具作用 1.2 git.GitHub和SVN比较 1.3 本地gi ...
- python脚本监控获取当前Linux操作系统[内存]/[cpu]/[硬盘]/[登录用户]
此脚本应用在linux, 前提是需要有python和python的psutil模块 脚本 #!/usr/bin/env python # coding=utf-8 import sys import ...
- Splay简介
Splay树,又叫伸展树,可以实现快速分裂合并一个序列,几乎可以完成平衡树的所有操作.其中最重要的操作是将指定节点伸展到指定位置, 目录 节点定义 旋转操作 伸展操作 插入操作 删除操作 lower_ ...
- Socket:读写处理及连接断开的检测
作为进程间通信及网络通信的一种重要技术,在实际的开发中,socket编程是经常被用到的.关于socket编程的一般步骤,这里不再赘述,相关资料和文章很多,google/baidu即可. 本文主要是探讨 ...
- POJ1128 Frame Stacking(拓扑排序+dfs)题解
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ... ...
- redis安装使用配置
一.安装前的准备 下载redis http://redis.io/download https://github.com/mythz/redis-windows 下载Windows版客户端net版sd ...
- 【Coursera】Fifth week(3)
Ethernet 在 PARC(Xerox) 发明. 第一个 Local-Area-Network (LAN 局域网). 把 PCs 连接到 激光打印机上. 在夏威夷大学,被早期的无线网络 Aloha ...