链接:https://leetcode.com/tag/recursion/

247 Strobogrammatic Number II (2019年2月22日,谷歌tag)

给了一个 n,给出长度为 n 的所有上下颠倒 180度以后都看起来一样的数字字符串。

题解:dfs,回溯。注意所有的能有pair的序列是 0, 1, 8, 6, 9

 class Solution {
public:
vector<string> findStrobogrammatic(int n) {
vector<pair<string, string>> digits{{"", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}};
vector<string> res;
if (n & ) {
vector<string> list = {"", "", ""};
for (int i = ; i < ; ++i) {
string s = list[i];
dfs(res, s, digits, n);
}
} else {
string s = "";
dfs(res, s, digits, n);
}
return res;
}
void dfs(vector<string>& res, string& s, vector<pair<string, string>>& digits, const int n) {
if (s.size() == n) {
if (s[] != '' || s == "") {
res.push_back(s);
}
return;
}
for (auto& p : digits) {
string s1 = p.first, s2 = p.second;
string oriS = s;
s = s1 + s + s2;
dfs(res, s, digits, n);
s = oriS;
}
}
};

248 Strobogrammatic Number III (2019年1月17日,谷歌高频)

给了两个字符串一个low,一个high,找出 low 和 high 之间所有的 strobogrammatic number 的数量。strobogrammatic number 是说一个数字上下颠倒180度之后看起来一样

题解:找出所有上下颠倒看起来一样的数字。有 0, 1, 8, 69, 和 96, 我们可以用这些number做recursion。每次在两边加上这些数字。就能生成一个上下颠倒后看起来一样的数。

 class Solution {
public:
int strobogrammaticInRange(string low, string high) {
if (low.size() > high.size() || low.size() == high.size() && low > high) {
return ;
}
this->low = low, this->high = high;
vector<string> start = {"", "", "", "", "", ""};
for (auto s : start) {
dfs(s);
}
return ret.size();
}
void dfs(string s) {
if (check(s)) {
ret.insert(s);
}
if (s.size() <= high.size()) {
for (int i = ; i < model.size(); ++i) {
string pre = model[i].first, suff = model[i].second;
dfs(pre + s + suff);
}
}
if (s > high) {
return;
}
}
bool check(string s) {
if (s.empty() || s.size() > && s[] == '' ) {
return false;
}
if (low.size() == high.size()) {
return s.size() == low.size() && s >= low && s <= high;
} else {
if (s.size() > low.size() && s.size() < high.size()) {
return true;
} else if (s.size() == low.size()) {
return s >= low;
} else if (s.size() == high.size()) {
return s <= high;
}
return false;
}
return false;
}
set<string> ret;
vector<pair<string, string>> model = {{"",""}, {"",""}, {"",""}, {"",""}, {"",""}};
string low, high;
};

544 Output Contest Matches(2019年2月22日,谷歌tag)

给定一个 n,假设 n 只队伍比赛(n是2的幂),我们每次安排比赛要把最强的队伍和最弱的队伍安排在一起。给出最后比赛的排列字符串。

Input: 8
Output: (((1,8),(4,5)),((2,7),(3,6)))
Explanation:
First round: (1,8),(2,7),(3,6),(4,5)
Second round: ((1,8),(4,5)),((2,7),(3,6))
Third round: (((1,8),(4,5)),((2,7),(3,6)))
Since the third round will generate the final winner, you need to output the answer (((1,8),(4,5)),((2,7),(3,6))).

题解:本题看起来比较复杂,但是其实仔细研究一下,我们只需要把所有team的编号放在一个数组里面,把第一个和最后一个组队形成pair,第二个和倒数第二个组队形成pair,生成新的数组,然后调用dfs递归生成即可。

 class Solution {
public:
string findContestMatch(int n) {
vector<string> pairs(n);
for (int i = ; i < n; ++i) {
pairs[i] = to_string(i+);
}
dfs(pairs);
return pairs[];
}
void dfs(vector<string>& pairs) {
if (pairs.size() == ) {return;}
int start = , end = pairs.size() - ;
vector<string> newPairs;
while (start < end) {
string str = "(" + pairs[start++] + "," + pairs[end--] + ")";
newPairs.push_back(str);
}
pairs = newPairs;
dfs(pairs);
}
};

625 Minimum Factorization(2019年2月22日)

给了一个正整数a,返回一个最小的正整数b,b中所有数位的乘积等于a。

题解:dfs,backtracking. 很慢,本题可以数学方法。

 class Solution {
public:
int smallestFactorization(int a) {
if (a == ) {return ;}
string s;
dfs(a, s);
return hasAns ? res : ;
}
bool hasAns = false;
int res = INT_MAX;
void dfs(int a, string& s) {
if (a == ) {
long long temp = stoll(s);
if (s.size() <= && temp <= INT_MAX) {
hasAns = true;
res = min(res, stoi(s));
}
return;
}
for (int i = ; i > ; --i) {
if (a % i == && s.size() + <= to_string(res).size()) {
s.push_back(i + '');
dfs(a/i, s);
s.pop_back();
}
}
}
};

687 Longest Univalue Path

698 Partition to K Equal Sum Subsets (2019年2月24日,谷歌tag)

本题和 544 Output Contest Matches 拼火柴棍的这个题非常类似。基本一模一样。

给了一个数组nums,一个k,问这个数组能不能划分成 k 个总和大小相等的子集。每个数组元素只能用一次,并且必须用掉。

题解:dfs。

 class Solution {
public:
bool canPartitionKSubsets(vector<int>& nums, int k) {
this->k = k;
size = nums.size();
if (size == ) {return false;}
int tot = ;
sort(nums.begin(), nums.end(), greater<int>()); //从大到小排列
for (auto& num : nums) {
tot += num;
}
if (tot % k) {return false;}
const int length = tot / k;
vector<int> visit(size, );
return dfs(nums, visit, , , length, );
}
int size = , k = ;
bool dfs(const vector<int>& nums, vector<int>& visit, int curLen, int cnt, const int target, int start) {
if (cnt == k) {return true;}
for (int i = start; i < size; ++i) {
if (visit[i]) {continue;}
if (curLen + nums[i] > target) {continue;}
visit[i] = ;
bool res = false;
if (curLen + nums[i] == target) { //如果当前这个子集的元素的和等于target,就去寻找下一个子集。
res = dfs(nums, visit, , cnt + , target, );
} else if (curLen + nums[i] < target) {
res = dfs(nums, visit, curLen + nums[i], cnt, target, i + ); //如果当前这个子集的元素的和小于target,就继续递归找当前子集的其他元素。
}
visit[i] = ;
if (res) {
return res;
}
}
return false;
}
};

726 Number of Atoms

761 Special Binary String

779 K-th Symbol in Grammar(2019年2月22日,谷歌tag)(和群主mock的那题非常相似。)

当 N = 1 的时候,字符串为 “0”,每当 N 递增1,就把上一个字符串的 “0” 变成 “01”, 把上一个字符串的“1”变成“10”。返回第N个字符串的第K个字符。

题解:我们这题用递归解。我们先求出当前字符串的第 K 个字符是由前一个字符串的字符是 0 还是 1 生成的,然后根据前一个字符,做一点逻辑判断,判断出当前字符是 0 还是 1。

 class Solution {
public:
int kthGrammar(int N, int K) {
if (N == ) {return ;}
int preNumber = kthGrammar(N-, (K+)/);
int res = -;
if (preNumber == ) { //
res = K & ? : ;
} else if (preNumber == ){ //
res = K & ? : ;
}
return res;
}
};

794 Valid Tic-Tac-Toe State

894 All Possible Full Binary Trees

【LeetCode】Recursion(共11题)的更多相关文章

  1. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...

  2. Leetcode 简略题解 - 共567题

    Leetcode 简略题解 - 共567题     写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...

  3. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  4. WEB前端面试选择题解答(共36题)

    第1题 ["1", "2", "3"].map(parseInt) A:["1", "2", &qu ...

  5. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

  6. 剑指offer 面试11题

    面试11题: 题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4 ...

  7. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  8. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

  9. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

随机推荐

  1. 数据库的目录IDF打不开!附加失败

    选择附加数据库,结果 武汉地图打不开 这是mapgis k9里面自带的地图 IDF:Identity Definition File?https://zhidao.baidu.com/question ...

  2. vue项目中使用element的dialog中引入ztree却不能初始化解决办法

    一开始我在 里边写的,发现获取不到,那么采用dialog自带的回调函数,窗口打开后opend进行处理, 结果:

  3. HihoCoder - 1104 Suzhou Adventure (树上背包)

    题目:https://vjudge.net/contest/323605#problem/D 题意:给你一棵n个点的树,1点出发,然后规定k个点必须去,每个点上有一个权值,要走m个点,问最大权值是多少 ...

  4. drawArc

    1) 画笔设置 Paint.Style.STROKE 中空模式 paint = new Paint(); //新建一个画笔对象 paint.setAntiAlias(true);//抗锯齿功能 pai ...

  5. idea2019.2 svn 忽略文件问题

    自己用的是idea2019.2最新版本,今天提交的时候Commit Changes Dialog local changes refresh一直再刷新 其他的方法都是老版本都不适合 解决办法 找到Se ...

  6. 利用 Skywalking 搭建 APM(应用性能管理)— 安装与配置

    1.什么是 Skywalking Skywalking 是一个APM系统,即应用性能监控系统,为微服务架构和云原生架构系统设计.它通过探针自动收集所需的指标,并进行分布式追踪.通过这些调用链路以及指标 ...

  7. 全文搜索 ElasticSearch

    今天突然想了解一下ES,看看有什么优势,能不能用在项目中. 说到ES就不得不了解它的底层技术-全文检索 Ref: 全文检索的基本原理 https://blog.csdn.net/wangmaohong ...

  8. Git015--标签管理

    Git--标签管理 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...

  9. jvm 这我就能会了 擦

    最近老有人问jvm,恕我直言,完蛋了,不会,慢慢学吧,开始第一个学习,后续补充,走起... 我看的他的https://www.cnblogs.com/dingyingsi/p/3760447.html ...

  10. CentOS vim的使用

    安装vim工具 [root@bogon ~]# yum install -y vim-enhanced 卸载vim工具 [root@bogon ~]# yum remove -y vim* vim常用 ...