【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. Java浏览器弹出下载框,多个文件导出压缩包

    项目里一直有这个功能,也一直没怎么注意,今天研究了一下 依据逻辑往下走: 首先是要下载的ajax的Java方法,只有返回值需要设定一下,其他的不用管: Map<String, Object> ...

  2. JS自定义 Map

    <script>function HashMap(){this.map = {};}HashMap.prototype = { put : function(key, value){ th ...

  3. Dubbo学习-8-dubbo启动时检查

    启动时检查的作用如下: (1)Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check="true& ...

  4. Docker Swarm学习教程【转载】

    Swarm介绍 Swarm是Docker公司在2014年12月初发布的一套较为简单的工具,用来管理Docker集群,它将一群Docker宿主机变成一个单一的,虚拟的主机.Swarm使用标准的Docke ...

  5. 请问有支持直接从 word 文档复制图片的 editor 吗

    Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...

  6. c#image与byte数组的转换

    // image to byte[] Image photo = new Bitmap(selectPictureFile); System.IO.MemoryStream ms = new Syst ...

  7. sqlserver连接-2

    本地连接 方法1. 方法2. 远程连接 如果无法通过IP地址远程连接你的SQL Server 2008服务器,可以参考下面的内容进行设置. 在进行下述设置之前,应该确保你的网络已经安装设置完毕,服务器 ...

  8. HTTP请求时候总是设置的两个参数ConnectionTimeOut和SocketTimeOut

    在HTTP请求时候总是设置两个参数,就是连接超时和Socket超时 public static final String SO_TIMEOUT = "http.socket.timeout& ...

  9. php面试专题---16、MySQL创建高性能索引考点

    php面试专题---16.MySQL创建高性能索引考点 一.总结 一句话总结: 注意:只写精品 1.索引的基础? 类似书籍的目录:索引类似于书籍的目录,要想找到一本书的某个特定主题,需要先查找书的目录 ...

  10. JAVA File的创建及相对路径绝对路径

    http://blog.sina.com.cn/s/blog_9386f17b0100w2vv.html JAVA File的创建及相对路径绝对路径 (2011-12-09 08:27:56) 转载▼ ...