Commit afc54e79 by 李楚霏

week7 work

parent f0551119
/**
* @param {number[][]} M
* @return {number}
*/
var findCircleNum = function(M) {
if(M.length == 0) return;
let n = M.length;
let count =n;
let parent = new Array(n);
for(let i =0; i < n; i++) {
parent[i] = i;
}
const onion = (p, q) => {
let rootP = find(p);
let rootQ = find(q);
if (rootP === rootQ)
return;
parent[rootP] = rootQ;
count--;
}
const find = (p) => {
while(p !== parent[p]) {
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
for(let i =0; i < n; i++) {
for(let j =0; j < n; j++) {
if(M[i][j] === 1) {
onion(i, j);
}
}
}
return count;
};
\ No newline at end of file
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> res;
vector<string> chessBoard(n, string(n, '.'));
dfs(n, 0, res, chessBoard);
return res;
}
void dfs(int n, int row, vector<vector<string>> & res, vector<string>& chessBoard) {
if (row == n) {
res.push_back(chessBoard);
return;
}
for(int col = 0; col < n; col++) {
if (isValid(row, col, n, chessBoard)) {
chessBoard[row][col] = 'Q';
dfs(n, row+1, res, chessBoard);
chessBoard[row][col] = '.';
}
}
}
bool isValid(int row, int col, int n, vector<string>&chessBoard) {
// 检查固定列每一行上是否有queen
for(int i = 0 ; i < row; i++) {
if(chessBoard[i][col] == 'Q'){
return false;
}
}
// 左上对角检测是否有queen
for(int i = row -1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (chessBoard[i][j] == 'Q') {
return false;
}
}
//右上对角检测是否有queen
for(int i = row - 1,j = col + 1; i >= 0 && j < n; i--, j++) {
if (chessBoard[i][j] == 'Q') {
return false;
}
}
return true;
}
};
\ No newline at end of file
#include<string>
#include<vector>
#include<unordered_set>
#include<iostream>
using namespace std;
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> wordSet(wordList.begin(), wordList.end());
if (wordSet.count(endWord) == 0) return 0;
unordered_set<string> upToDown({beginWord});
unordered_set<string>downToUp({endWord});
int list_size = wordList.size();
int word_size = beginWord.size();
vector<int>flag(list_size, 0);
int res =2;
while(!upToDown.empty()) {
unordered_set<string>next;
for( auto & word : upToDown) {
for(int i =0; i < list_size; i++) {
int count = 0; // count是用来计算beginword和当前比较的string不同的字符个数
for(int j = 0; j < word_size; j++) {
if (word[j] != wordList[i][j]) {
count++;
}
if(count > 1) break; //如果不同的字符个数大于一个以上就跳出循环
}
if (count == 1) { // 题目要求每次只能变一个元素,所以不同字符的个数只能为一,满足条件,才能往下判断
if (downToUp.count(wordList[i]) != 0) return res;
if (flag[i] == 0)
next.insert(wordList[i]);
flag[i] = 1;
}
}
}
res++;
upToDown = next;
if (upToDown.size() > downToUp.size()) swap(upToDown, downToUp);
}
return 0;
}
};
int main() {
Solution s;
int res;
string beginWord = "hit";
string endWord = "cog";
vector<string>wordlist = {"hot","dot","dog","lot","log","cog"};
res = s.ladderLength(beginWord, endWord, wordlist);
cout<<res<<endl;
return 0;
}
\ No newline at end of file
#### 第7周学习总结
#### 第7周学习总结
- 学习内容
- 字典树;tri树,
- 题型
- tri树的构建,和删除,增加元素操作
- 并查集
- 题型
- [547. 朋友圈](https://leetcode-cn.com/problems/friend-circles/)
- 启发式搜索
- A*搜索
- 双向BFS
- 总结:双向bfs和普通的bfs搜索差在,双向bfs不仅会从前到后搜索,还会从后往前搜索,直到一个节点被访问了两次,一般就可以得出结果了
- 题型:
- #### [127. 单词接龙](https://leetcode-cn.com/problems/word-ladder/)
- 剪枝
- 红黑树,平衡二叉树
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment