Skip to content

Commit

Permalink
2020.12.21
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxuyan committed Dec 21, 2020
1 parent d1c46ef commit b872c6c
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 36 deletions.
13 changes: 8 additions & 5 deletions Array/PreSum/0560-subarray-sum-equals-k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class Solution {
* index 0 1 2 3 4 5 6
* value 0 3 8 10 8 12 13
*
int subarraySum(vector<int>& nums, int k) {
*
*/
int subarraySum_pure(vector<int>& nums, int k) {
int n = nums.size();
int sum[n + 1];
sum[0] = 0;
Expand All @@ -36,18 +38,19 @@ class Solution {
}

int res = 0;
for (int i = 1; i <=n ; ++i) {
// i>j
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
if(sum[i] - sum[j] == k){
if (sum[i] - sum[j] == k) {
res++;
}
}
}
return res;
}
*/

// 前缀和 + 哈希表优化
// 由于不需要记录i, j
int subarraySum(vector<int>& nums, int k) {
// key: 前0到i的和(前缀和)
// value: 该和出现的次数
Expand All @@ -58,7 +61,7 @@ class Solution {
sum += x;
if (mp.find(sum - k) != mp.end()) {
// 查找sum[i] - k = sum[j]
// 且j < i
// 且i > j
count += mp[sum - k];
}
mp[sum]++;
Expand Down
4 changes: 2 additions & 2 deletions BFS/0111-Minimum-Depth-of-Binary-Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class Solution {
queue<TreeNode *> q;
q.push(root);
// root 本身就是一层,depth 初始化为 1
// 因为树的BFS遍历不会走回头路 所以不需要visited
int depth = 1;

while (!q.empty()) {
int sz = q.size();
/* 将当前队列中的所有节点向四周扩散 */
for (int i = 0; i < sz; i++) {
for (int i = 0; i < q.size(); i++) {
TreeNode *cur = q.front();
q.pop();
if (cur->left == nullptr && cur->right == nullptr) return depth;
Expand Down
1 change: 0 additions & 1 deletion BFS/0752-Unlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class Solution {
visited.insert(minusOneStr);
}
}

}
// 遍历队列的for循环外面, 在此处++
step++;
Expand Down
52 changes: 52 additions & 0 deletions DFS/0022-generate-parentheses.cpp
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();
}
};
56 changes: 56 additions & 0 deletions DFS/0037-sudoku-solver.cpp
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;
}
};
8 changes: 4 additions & 4 deletions DFS/0051-N-Queens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using namespace std;

class Solution {
private:
vector<vector<string>> res;
public:
vector<vector<string>> solveNQueens(int n) {
// '.'表示空 'Q'表示皇后 初始化空棋盘
Expand All @@ -20,6 +22,7 @@ class Solution {
* @param row
*/
void dfs(vector<string>& board, int row) {
// 结束条件
if (row == board.size()) {
res.push_back(board);
return;
Expand All @@ -41,7 +44,7 @@ class Solution {
static bool isValid(vector<string>& board, int row, int col) {
int n = board.size();
// 检查列是否有皇后互相冲突
for (int i = 0; i < n; i++) {
for (int i = 0; i < row; i++) {
if (board[i][col] == 'Q') return false;
}
// 检查右上方是否有皇后互相冲突
Expand All @@ -54,7 +57,4 @@ class Solution {
}
return true;
}

private:
vector<vector<string>> res;
};
2 changes: 1 addition & 1 deletion DFS/pailie-permutations/0031-next-permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*
* 1. 先找出最大的索引 k 满足 nums[k] < nums[k+1],如果不存在,就翻转整个数组;
* 2. 再找出另一个最大索引 l 满足 nums[l] > nums[k];
* 2. 再找出另一个最大索引 l, l>k, 满足 nums[l] > nums[k];
* 3. 交换 nums[l] 和 nums[k]
* 4. 最后翻转 nums[k+1:]
*
Expand Down
44 changes: 21 additions & 23 deletions DFS/pailie-permutations/0047-permutations-ii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,39 @@ using namespace std;
class Solution {
private:
vector<vector<int>> res;
unordered_map<int, int> nums_count_map;
unordered_map<int, int> used_map;

public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
for (auto& num : nums) nums_count_map[num]++;
// 路径
vector<int> track;
// 排序
sort(nums.begin(), nums.end());
dfs(nums, track);
int len = nums.size();
vector<bool> used(len, false);
backtrack(nums, used, track);
return res;
}
/**
* dfs
* @param nums 可选路径
* @param track 已选路径
*/
void dfs(vector<int>& nums, vector<int> track) {

void backtrack(vector<int>& nums, vector<bool>& used, vector<int>& track) {
if (track.size() == nums.size()) {
res.push_back(track);
return;
}
// 因为是组合 所以每次都从0开始
for (int i = 0; i < nums.size(); ++i) {
// 排除不合法的选择 这里其实就是检查used
if (used_map[nums[i]] > 1 || used_map[nums[i]] >= nums_count_map[nums[i]]) continue;
// 做选择
for (int i = 0; i < nums.size(); i++) {
if (used[i]) continue;
// used[i-1] == false是对同一树枝不去重 对同一层去重
// used[i-1] == true是对同一层不去重 对同一树枝去重
// https://leetcode-cn.com/problems/permutations-ii/solution/47-quan-pai-lie-iiche-di-li-jie-pai-lie-zhong-de-q/
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
used[i] = true;
track.push_back(nums[i]);
used_map[nums[i]]++;
// 进入下一层决策树
dfs(nums, track);
// 取消选择
backtrack(nums, used, track);
track.pop_back();
used_map[nums[i]]--;
used[i] = false;
}
}
};
};

int main() {
Solution solution;
vector<int> nums = {1, 1, 2};
solution.permuteUnique(nums);
}
2 changes: 2 additions & 0 deletions DFS/zuhe-combination/0078-subsets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
[1,2],
[]
]
子集问题:每一个节点都要
*/
#include <algorithm>
#include <iostream>
Expand Down
39 changes: 39 additions & 0 deletions DataStructure/0020-valid-parentheses.cpp
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();
}
};

0 comments on commit b872c6c

Please sign in to comment.