-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
185 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,6 @@ class Solution { | |
visited.insert(minusOneStr); | ||
} | ||
} | ||
|
||
} | ||
// 遍历队列的for循环外面, 在此处++ | ||
step++; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 22. 括号生成 | ||
* 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。 | ||
* | ||
* 输入:n = 3 | ||
输出:[ | ||
"((()))", | ||
"(()())", | ||
"(())()", | ||
"()(())", | ||
"()()()" | ||
] | ||
分析:https://labuladong.gitbook.io/algo/suan-fa-si-wei-xi-lie/3.1-hui-su-suan-fa-dfs-suan-fa-pian/he-fa-kuo-hao-sheng-cheng | ||
*/ | ||
#include <algorithm> | ||
#include <iostream> | ||
#include <stack> | ||
#include <unordered_map> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
private: | ||
vector<string> res; | ||
|
||
public: | ||
vector<string> generateParenthesis(int n) { | ||
vector<char> choice = {'(', ')'}; | ||
string track; | ||
dfs(n, n, track); | ||
return res; | ||
} | ||
void dfs(int left, int right, string& track) { | ||
// 对于一个「合法」的括号字符串组合 p, | ||
// 必然对于任何 0 <= i < len(p) 都有:子串 p[0..i] 中左括号的数量都大于或等于右括号的数量。 | ||
if (left > right) return; | ||
if (left < 0 || right < 0) return; | ||
// 一个「合法」括号组合的左括号数量一定等于右括号数量,这个很好理解。 | ||
if (left == 0 && right == 0) { | ||
res.push_back(track); | ||
return; | ||
} | ||
|
||
track.push_back('('); | ||
dfs(left - 1, right, track); | ||
track.pop_back(); | ||
|
||
track.push_back(')'); | ||
dfs(left, right - 1, track); | ||
track.pop_back(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 37. 解数独 | ||
*/ | ||
#include <algorithm> | ||
#include <iostream> | ||
#include <unordered_set> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
void solveSudoku(vector<vector<char>>& board) { backtrack(board, 0, 0); } | ||
bool backtrack(vector<vector<char>>& board, int i, int j) { | ||
int m = 9, n = 9; | ||
if (j == n) { | ||
// 穷举到最后一列的话就换到下一行重新开始。 | ||
return backtrack(board, i + 1, 0); | ||
} | ||
if (i == m) { | ||
// 找到一个可行解,触发 base case | ||
return true; | ||
} | ||
|
||
if (board[i][j] != '.') { | ||
// 如果有预设数字,不用我们穷举 | ||
return backtrack(board, i, j + 1); | ||
} | ||
|
||
for (char ch = '1'; ch <= '9'; ch++) { | ||
// 如果遇到不合法的数字,就跳过 | ||
if (!isValid(board, i, j, ch)) continue; | ||
|
||
board[i][j] = ch; | ||
// 如果找到一个可行解,立即结束 | ||
if (backtrack(board, i, j + 1)) { | ||
return true; | ||
} | ||
board[i][j] = '.'; | ||
} | ||
// 穷举完 1~9,依然没有找到可行解,此路不通 | ||
return false; | ||
} | ||
|
||
// 判断 board[i][j] 是否可以填入 n | ||
static bool isValid(vector<vector<char>>& board, int r, int c, char n) { | ||
for (int i = 0; i < 9; i++) { | ||
// 判断行是否存在重复 | ||
if (board[r][i] == n) return false; | ||
// 判断列是否存在重复 | ||
if (board[i][c] == n) return false; | ||
// 判断 3 x 3 方框是否存在重复 | ||
if (board[(r / 3) * 3 + i / 3][(c / 3) * 3 + i % 3] == n) return false; | ||
} | ||
return true; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
[1,2], | ||
[] | ||
] | ||
子集问题:每一个节点都要 | ||
*/ | ||
#include <algorithm> | ||
#include <iostream> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 20. 有效括号 | ||
*/ | ||
#include <algorithm> | ||
#include <iostream> | ||
#include <stack> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
private: | ||
stack<char> stack; | ||
unordered_map<char, char> map = { | ||
{'(', ')'}, | ||
{'{', '}'}, | ||
{'[', ']'}, | ||
}; | ||
|
||
public: | ||
bool isValid(string s) { | ||
for (auto& c : s) { | ||
if (map.count(c)) { | ||
// 如果是正括号 | ||
stack.push(c); | ||
} else { | ||
// 如果是反括号 | ||
// 注意条件有两个: | ||
// 1. 如果此时栈为空 说明没有正括号能跟这个反括号匹配 return false | ||
// 2. 栈顶的括号不匹配 | ||
if (stack.empty() || map[stack.top()] != c) return false; | ||
// 记得c++要手动pop | ||
stack.pop(); | ||
} | ||
} | ||
return stack.empty(); | ||
} | ||
}; |