【10】Regular Expression Matching

【17】Letter Combinations of a Phone Number

【22】Generate Parentheses (2019年2月13日)

给了一个N,生成N对括号的所有情况的字符串。

n = 3

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

题解:dfs生成。

 class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
string str;
int left = n, right = n;
dfs(str, res, left, right);
return res;
}
void dfs(string& str, vector<string>& res, int left, int right) {
if (left == && right == ) {
res.push_back(str);
return;
}
if (left <= right && left - >= ) {
str += "(";
dfs(str, res, left-, right);
str.pop_back();
}
if (left < right && right - >= ) {
str += ")";
dfs(str, res, left, right - );
str.pop_back();
}
}
};

【37】Sudoku Solver (2019年3月18日,google tag)

题意:填数独

题解:backtracking,唯一的难点是判断每一个 3 * 3 的 block 是否合法的时候,坐标的判断需要点技巧。

 class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
dfs(board);
return;
}
const int n = , size = ;
bool dfs(vector<vector<char>>& board) {
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
if (board[i][j] == '.') {
for (char c = ''; c <= ''; ++c) {
if (isValid(board, i, j, c)) {
board[i][j] = c;
if (dfs(board)) {return true;}
board[i][j] = '.';
}
}
return false;
}
}
}
return true;
}
bool isValid(vector<vector<char>>& board, int r, int c, char x) {
const int idxR = (r / size) * , idxC = (c / size) * ;
for (int i = ; i < n; ++i) {
if (board[r][i] == x || board[i][c] == x) {return false;}
if (board[idxR+i/][idxC+i%] == x) {return false;}
}
return true;
}
};

【39】Combination Sum

【40】Combination Sum II

【44】Wildcard Matching

【46】Permutations (2019年1月23日,谷歌tag复习)(M)

给了一个distinct numbers 的数组,返回所有的排列。

题解:dfs

 class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
dfs(nums);
return ret;
}
vector<int> arr;
vector<vector<int>> ret;
void dfs(const vector<int>& nums) {
if (arr.size() == nums.size()) {
ret.push_back(arr);
return;
}
for (int i = ; i < nums.size(); ++i) {
set<int> st(arr.begin(), arr.end());
if (st.find(nums[i]) != st.end()) { continue; }
arr.push_back(nums[i]);
dfs(nums);
arr.pop_back();
}
return;
}
};

【47】Permutations II (2019年2月17日,有重复元素的全排列。今天周赛的第四题 996. Number of Squareful Arrays)

写一个有重复元素的全排列。

Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
]

题解:我们可以先 sort 一下数组,然后用一个临时的array,存放现在结果,重点是遇到重复元素的时候如何避免生成重复的序列。如果当前想放进列表的元素已经访问过了,就continue,如果当前想放进列表的元素和它前一个元素的值一样,但是前一个元素还没有放进数组里面,那么这个元素肯定不能放在数组里面,比如说原数组是[2, 2],对应下标是[0, 1],如果第一个2还没有在列表里面,那么第二个2就肯定不让它在列表里面。因为如果第二个2在列表里面并且在第一个2前面,就有 [1,0] 和 [0,1] 的两种排列。

 class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
const int n = nums.size();
sort(nums.begin(), nums.end());
vector<vector<int>> ret;
vector<int> visited(n, ), arr;
dfs(nums, ret, arr, visited);
return ret;
}
void dfs(vector<int>& nums, vector<vector<int>>& ret, vector<int>& arr, vector<int>& visited) {
if (arr.size() == nums.size()) {
ret.push_back(arr);
return;
}
for (int i = ; i < nums.size(); ++i) {
if (visited[i]) {continue;}
if (i - >= && nums[i] == nums[i-] && !visited[i-]) {continue;}
arr.push_back(nums[i]);
visited[i] = ;
dfs(nums, ret, arr, visited);
arr.pop_back();
visited[i] = ;
}
}
};

【51】N-Queens

【52】N-Queens II

【60】Permutation Sequence

【77】Combinations (2019年1月21日,算法群打卡题)

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],

题解:直接回溯。

 class Solution {
public:
vector<vector<int>> ret;
vector<vector<int>> combine(int n, int k) {
vector<int> nums;
dfs(nums, , n, k);
return ret;
}
void dfs(vector<int>& nums, int cur, const int n, const int k) {
if (nums.size() == k) {
ret.push_back(nums);
return;
}
for (int i = cur; i <= n; ++i) {
nums.push_back(i);
dfs(nums, i + , n, k); //这里注意不要写错了。 我有时候会写成 dfs(nums, cur + 1, n, k);
nums.pop_back();
}
return;
}
};

【78】Subsets (相似的题目见 90 题)

给了一个 distinct 的数组,返回它所有的子集。

题解见位运算专题【78】题,一样的。https://www.cnblogs.com/zhangwanying/p/9886589.html

【79】Word Search (2019年1月25日,谷歌tag复习) (Medium)

给了一个单词板和一个单词,四联通,问能不能在板子上面找到这个单词。

题解:backtracking,注意边界哇哭死,没有一次AC

 class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
n = board.size(), m = board[].size();
for (int i = ; i < n; ++i) {
for (int j = ; j < m; ++j) {
if (board[i][j] == word[]) {
vector<vector<int>> visit(n, vector<int>(m, ));
visit[i][j] = ;
if (dfs(board, i, j, word, , visit)) {
return true;
}
}
}
}
return false;
}
int n, m;
bool dfs(vector<vector<char>>& board, int x, int y, const string word, int cur, vector<vector<int>>& visit) {
if (cur + == word.size()) {
return true;
}
for (int i = ; i < ; ++i) {
int newx = x + dirx[i], newy = y + diry[i];
if (newx >= && newx < n && newy >= && newy < m && !visit[newx][newy] && board[newx][newy] == word[cur+]) {
visit[newx][newy] = ;
if (dfs(board, newx, newy, word, cur + , visit)) {
return true;
}
visit[newx][newy] = ;
}
}
return false;
}
int dirx[] = {-, , , };
int diry[] = {, -, , };
};

【89】Gray Code

【90】Subsets II (算法群,2018年11月21日)

给了一个有重复数字的数组,返回它所有的unique子集。

Input: [,,]
Output:
[
[],
[],
[,,],
[,],
[,],
[]
]

题解:回溯法。用set去重,有一点需要注意的是,千万不能每次递归的时候对 现在vector的状态sort一下,不然递归回溯的时候肯定有问题的。

 class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
const int n = nums.size();
vector<vector<int>> ret;
set<vector<int>> stRet;
vector<int> temp;
dfs(nums, stRet, , temp);
for (auto ele : stRet) {
ret.push_back(ele);
}
return ret;
}
void dfs(const vector<int>& nums, set<vector<int>>& st, int cur, vector<int>& temp) {
vector<int> t1 = temp;
sort(t1.begin(), t1.end());
st.insert(t1);
if (cur == nums.size()) {return;}
for (int i = cur; i < nums.size(); ++i) {
temp.push_back(nums[i]);
dfs(nums, st, i+, temp);
temp.pop_back();
}
}
};

2019年2月25日更新一个更好的解法:

如果当前层次有两个一样的数,一定不会选择后面的那个数字。(可以参考 有重复元素的permutation)

 class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
if (nums.empty()) {return res;}
vector<int> path;
dfs(nums, res, path, );
return res;
}
void dfs(vector<int>& nums, vector<vector<int>>& res, vector<int>& path, int start) {
res.push_back(path);
for (int i = start; i < nums.size(); ++i) {
if (i != start && nums[i] == nums[i-]) {continue;}
path.push_back(nums[i]);
dfs(nums, res, path, i + );
path.pop_back();
}
}
};

【93】Restore IP Addresses (2019年2月14日)

给了一个串纯数字的字符串,在字符串里面加‘.’,返回所有合法的ip字符串。

Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"] 

题解:backtracking,用一个新的字符串保存当前的ip字符串。ipv4的每一个小段必须是[0, 255],四个小段。

 class Solution {
public:
vector<string> restoreIpAddresses(string s) {
const int n = s.size();
vector<string> ret;
if (n == ) {return ret;}
string temp;
dfs(s, , ret, temp, );
return ret;
}
void dfs(const string s, int cur, vector<string>& ret, string& temp, int part) {
if (cur == s.size() && part == ) {
ret.push_back(temp);
return;
}
if (part >= ) {return;}
string num;
if (s[cur] == '') {
string oriTemp = temp;
num = string(, s[cur]);
temp += temp.empty() ? (num) : ("." + num);
dfs(s, cur + , ret, temp, part + );
temp = oriTemp;
} else {
string oriTemp = temp;
for (int k = ; k <= ; ++k) {
if (cur + k >= s.size()) {break;}
num += s[cur + k];
int inum = stoi(num);
if (inum < || inum > ) {break;}
temp += temp.empty() ? (num) : ("." + num);
dfs(s, cur + k + , ret, temp, part + );
temp = oriTemp;
}
}
}
};

【126】Word Ladder II

【131】Palindrome Partitioning

【140】Word Break II(2018年12月19日,算法群,类似题目 472. DFS专题)

【211】Add and Search Word - Data structure design

【212】Word Search II

【216】Combination Sum III

【254】Factor Combinations

【267】Palindrome Permutation II

【291】Word Pattern II

【294】Flip Game II

【306】Additive Number

【320】Generalized Abbreviation

【351】Android Unlock Patterns

【357】Count Numbers with Unique Digits

【401】Binary Watch

【411】Minimum Unique Word Abbreviation

【425】Word Squares

【526】Beautiful Arrangement

【691】Stickers to Spell Word

【784】Letter Case Permutation

【842】Split Array into Fibonacci Sequence

给了一个字符串 S,看能不能通过分割字符串,把字符串搞成一个斐波那契数组。(1 <= S.size() <= 200)

题解:就是暴力搜,但是WA吐了快,有几个点要注意,如果是 “000” 这种是可以返回的,返回 [0, 0, 0]。然后如果分割的字符串太长的就要continue,不然你 long long 也没有用,200位肯定超过 long long 了。

 // WA到吐了,太多情况
class Solution {
public:
vector<int> splitIntoFibonacci(string S) {
vector<int> ans;
if (S.empty()) { return ans; } //S如果是“000”是可以的,所以需要留着dfs.
return dfs(S, ans, ) ? ans : vector<int>{};
}
bool dfs(string S, vector<int>& ans, int cur_idx) {
int n = S.size();
if (cur_idx == n) {
return ans.size() >= ? true : false;
}
for (int i = cur_idx; i < n; ++i) {
if (ans.size() < ) {
string strNumber = S.substr(cur_idx, i - cur_idx + );
if (strNumber.size() > || strNumber.size() > && strNumber[] == '') {continue; }
long long number = stoll(strNumber);
if (number > INT_MAX) {continue;}
ans.push_back(number);
if (dfs(S, ans, i + )) {
return true;
}
ans.pop_back();
} else {
int ansSize = ans.size();
int num1 = ans.back(), num2 = ans[ansSize - ];
string strNumber = S.substr(cur_idx, i - cur_idx + );
if (strNumber.size() > || strNumber.size() > && strNumber[] == '') {continue; }
long long num3 = stoll(strNumber);
if (num3 > INT_MAX) {continue;}
if (num1 + num2 == num3) {
ans.push_back(num3);
if (dfs(S, ans, i + )){
return true;
}
ans.pop_back();
}
}
}
return false;
}
};

但是好像我的方法巨慢,可以看看discuss怎么求解的。

【LeetCode】回溯法 backtracking(共39题)的更多相关文章

  1. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  2. Leetcode 回溯法 典型例题

    那些要求列举所有的情况,或者说所有的情况都要探讨一下的例题,一般都可以考虑回溯法. 当遇到一个可以用到回溯法的时候需要按照如下步骤进行: 1.确定问题一个可以用到回溯法的时候需要按照如下步骤进行: 1 ...

  3. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  4. 【LeetCode】Recursion(共11题)

    链接:https://leetcode.com/tag/recursion/ 247 Strobogrammatic Number II (2019年2月22日,谷歌tag) 给了一个 n,给出长度为 ...

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

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

  6. 【LeetCode】BFS(共43题)

    [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...

  7. 【LeetCode】树(共94题)

    [94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...

  8. LeetCode 回溯法 别人的小结 八皇后 递归

    #include <iostream> #include <algorithm> #include <iterator> #include <vector&g ...

  9. [Leetcode] Backtracking回溯法解题思路

    碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大 ...

随机推荐

  1. 对calc()的研究

    1.calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,用来指定元素的长度 calc()最大的好处就是用在流体布局上 2.calc()使用通用的数学运算规则 使用“+”.“ ...

  2. React / Vue 跨端渲染原理与实现探讨

    跨端渲染是渲染层并不局限在浏览器 DOM 和移动端的原生 UI 控件,连静态文件乃至虚拟现实等环境,都可以是你的渲染层.这并不只是个美好的愿景,在今天,除了 React 社区到 .docx / .pd ...

  3. 对async 函数的研究

    async 函数 1.ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是什么?一句话,它就是 Generator 函数的语法糖. 前文有一个 Generator ...

  4. python全栈开发,Day43(引子,协程介绍,Greenlet模块,Gevent模块,Gevent之同步与异步)

    昨日内容回顾 I/O模型,面试会问道 I/O操作,不占用CPU,它内部有一个专门的处理I/O模块 print和写log属于I/O操作,它不占用CPU 线程 GIL保证一个进程中的多个线程在同一时刻只有 ...

  5. 【Swagger2】SpringBoot整合swagger2

    Swagger 简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法, ...

  6. [IOI2018] meetings 会议

    https://www.luogu.org/problemnew/show/P5044 题解 这种关于最大值或者最小值的问题,可以往笛卡尔树的方面想. 先考虑一个朴素的\(dp\),设\(dp[l][ ...

  7. C#-概念-类:类

    ylbtech-C#-概念-类:类 类(Class)是面向对象程序设计(OOP,Object-Oriented Programming)实现信息封装的基础.类是一种用户定义类型,也称类类型.每个类包含 ...

  8. PHP-图片处理

    开启 GD 扩展(php_gd2.dll) 创建画布 画布:一种资源型数据,可以操作的图像资源. 创建新画布(新建) ImageCreate(宽,高); 创建基于调色板的画布. imageCreate ...

  9. PHP-会话技术

    B/S 请求响应模式是无状态的.任意的请求间不存在任何的联系,不能将请求状态保持下去. 会话技术可以给每个浏览器分配持久数据,这些数据不会随着一次请求和相应结束而销毁. COOKIE cookie 是 ...

  10. ScriptControl接口

    http://www.cnblogs.com/railgunman/articles/1824304.html BAIDU一下ScriptControl,大多数都是“Delphi中ScriptCont ...