diff --git a/README.md b/README.md index 15b10bfa..b25b102d 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ * [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP) * [C++语言基础课](https://kamacoder.com/course.php?course_id=1) * [Java语言基础课](https://kamacoder.com/course.php?course_id=2) + * [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern) * 项目 * [基于跳表的轻量级KV存储引擎](https://github.com/youngyangyang04/Skiplist-CPP) diff --git a/problems/0130.被围绕的区域.md b/problems/0130.被围绕的区域.md index e8a1f02f..8014c0c8 100644 --- a/problems/0130.被围绕的区域.md +++ b/problems/0130.被围绕的区域.md @@ -435,6 +435,132 @@ class Solution: ``` +### JavaScript +```JavaScript +/** + * @description 深度搜索优先 + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ +function solve(board) { + const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + const [rowSize, colSize] = [board.length, board[0].length]; + + function dfs(board, x, y) { + board[x][y] = 'A'; + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + x; + const nextY = dir[i][1] + y; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (board[nextX][nextY] === 'O') { + dfs(board, nextX, nextY); + } + } + } + + for (let i = 0; i < rowSize; i++) { + if (board[i][0] === 'O') { + dfs(board, i, 0); + } + if (board[i][colSize - 1] === 'O') { + dfs(board, i, colSize - 1); + } + } + + for (let i = 1; i < colSize - 1; i++) { + if (board[0][i] === 'O') { + dfs(board, 0, i); + } + if (board[rowSize - 1][i] === 'O') { + dfs(board, rowSize - 1, i); + } + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (board[i][k] === 'A') { + board[i][k] = 'O'; + } else if (board[i][k] === 'O') { + board[i][k] = 'X'; + } + } + } +} + +/** + * @description 广度搜索优先 + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ +function solve(board) { + const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + const [rowSize, colSize] = [board.length, board[0].length]; + + function bfs(board, x, y) { + board[x][y] = 'A'; + const stack = [y, x]; + + while (stack.length !== 0) { + const top = [stack.pop(), stack.pop()]; + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + top[0]; + const nextY = dir[i][1] + top[1]; + + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + + if (board[nextX][nextY] === 'O') { + board[nextX][nextY] = 'A'; + stack.push(nextY, nextX); + } + } + } + + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + x; + const nextY = dir[i][1] + y; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (board[nextX][nextY] === 'O') { + dfs(board, nextX, nextY); + } + } + } + + for (let i = 0; i < rowSize; i++) { + if (board[i][0] === 'O') { + bfs(board, i, 0); + } + if (board[i][colSize - 1] === 'O') { + bfs(board, i, colSize - 1); + } + } + + for (let i = 1; i < colSize - 1; i++) { + if (board[0][i] === 'O') { + bfs(board, 0, i); + } + if (board[rowSize - 1][i] === 'O') { + bfs(board, rowSize - 1, i); + } + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (board[i][k] === 'A') { + board[i][k] = 'O'; + } else if (board[i][k] === 'O') { + board[i][k] = 'X'; + } + } + } +} +``` +

diff --git a/problems/0417.太平洋大西洋水流问题.md b/problems/0417.太平洋大西洋水流问题.md index 6777e2d9..53ae14ed 100644 --- a/problems/0417.太平洋大西洋水流问题.md +++ b/problems/0417.太平洋大西洋水流问题.md @@ -450,6 +450,117 @@ class Solution: return ans ``` +### JavaScript +```JavaScript +/** + * @description 深度搜索优先 + * @param {number[][]} heights + * @return {number[][]} + */ +function pacificAtlantic(heights) { + const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]]; + const [rowSize, colSize] = [heights.length, heights[0].length]; + const visited = Array.from({ length: rowSize }, _ => + Array.from({ length: colSize }, _ => new Array(2).fill(false)) + ); + const result = []; + + function dfs(heights, visited, x, y, sign) { + if (visited[x][y][sign]) { + return; + } + visited[x][y][sign] = true; + for (let i = 0; i < 4; i++) { + const nextX = x + dir[i][0]; + const nextY = y + dir[i][1]; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (heights[x][y] > heights[nextX][nextY]) { + continue; + } + dfs(heights, visited, nextX, nextY, sign); + } + } + + for (let i = 0; i < rowSize; i++) { + dfs(heights, visited, i, 0, 0); + dfs(heights, visited, i, colSize - 1, 1); + } + + for (let i = 0; i < colSize; i++) { + dfs(heights, visited, 0, i, 0); + dfs(heights, visited, rowSize - 1, i, 1); + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (visited[i][k][0] && visited[i][k][1]) { + result.push([i, k]); + } + } + } + + return result; +} + +/** + * @description 广度搜索优先 + * @param {number[][]} heights + * @return {number[][]} + */ +function pacificAtlantic(heights) { + const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]]; + const [rowSize, colSize] = [heights.length, heights[0].length]; + const visited = Array.from({ length: rowSize }, _ => + Array.from({ length: colSize }, _ => new Array(2).fill(false)) + ); + const result = []; + + function bfs(heights, visited, x, y, sign) { + if (visited[x][y][sign]) { + return; + } + visited[x][y][sign] = true; + const stack = [y, x]; + while (stack.length !== 0) { + [x, y] = [stack.pop(), stack.pop()]; + for (let i = 0; i < 4; i++) { + const nextX = x + dir[i][0]; + const nextY = y + dir[i][1]; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (heights[x][y] > heights[nextX][nextY] || visited[nextX][nextY][sign]) { + continue; + } + visited[nextX][nextY][sign] = true; + stack.push(nextY, nextX); + } + } + } + + for (let i = 0; i < rowSize; i++) { + bfs(heights, visited, i, 0, 0); + bfs(heights, visited, i, colSize - 1, 1); + } + + for (let i = 0; i < colSize; i++) { + bfs(heights, visited, 0, i, 0); + bfs(heights, visited, rowSize - 1, i, 1); + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (visited[i][k][0] && visited[i][k][1]) { + result.push([i, k]); + } + } + } + + return result; +} +``` diff --git a/problems/kama53.寻宝.md b/problems/kama53.寻宝.md index 08fc9a18..3a4b8b27 100644 --- a/problems/kama53.寻宝.md +++ b/problems/kama53.寻宝.md @@ -1,7 +1,4 @@ -思考一下边的权值为负数的情况 - - # 寻宝 [卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053) diff --git a/problems/qita/join.md b/problems/qita/join.md index 3b3e2d40..a2698e66 100644 --- a/problems/qita/join.md +++ b/problems/qita/join.md @@ -156,9 +156,9 @@ python代码 虽然我主张没有绝对正确的代码风格,但既然是给LeetCode-Master提交代码,尽量遵循Google编程规范。 -经常看我的代码的录友应该都知道,我的代码格严格按照 Google C++ 编程规范来的,这样看上去会比较整洁。 +经常看我的代码的录友应该都知道,我的代码风格严格按照 Google C++ 编程规范来的,这样看上去会比较整洁。 -大家提交代码的时候遇到规范性问题,例如哪里应该由空格,哪里没有空格,可以参考我的代码来。 +大家提交代码的时候遇到规范性问题,例如哪里应该有空格,哪里没有空格,可以参考我的代码来。 有一位录友在提交代码的时候会把之前的代码 做一下规范性的调整,这就很棒。 diff --git a/problems/qita/shejimoshi.md b/problems/qita/shejimoshi.md new file mode 100644 index 00000000..cf980e45 --- /dev/null +++ b/problems/qita/shejimoshi.md @@ -0,0 +1,55 @@ + +关于设计模式的学习,大家应该还是看书或者看博客,但却没有一个边学边练的学习环境。 + +学完了一种设计模式 是不是应该去练一练? + +所以卡码网 针对 23种设计,**推出了 23道编程题目,来帮助大家练习设计模式**。 + +

+ +这里的23到编程题目对应了 23种这几模式。 例如第一题,小明的购物车,就是单例模式: + +
+ +区别于网上其他教程,本教程的特点是: + +* **23种设计模式全覆盖**,涵盖了所有Gang of Four设计模式,包括创建型、结构型和行为型设计模式。 +* 通过23道简单而实用的例子,**以刷算法题的形式了解每种设计模式的概念、结构和应用场景**。 +* **为每个设计模式提供清晰的文字解释、结构图和代码演示**,帮助你更好地理解和实践。 +* **难度安排循序渐进**,从基础的、常用的设计模式逐步深入。 + +这样的一个学习体验,要收费吗? + +**免费的**! + +相信录友们可能还没有这种学习设计模式的体验,快去卡码网(kamacoder.com)上体验吧。 + +23道 设计模式的题目给大家出了,那么是不是得安排上对应的讲解? + +**当然安排**! + +针对每道题目,还给大家编写了一套 23种设计模式精讲,已经开源到Github上: + +> https://github.com/youngyangyang04/kama-DesignPattern + +支持Java,Python,Go,C++ 版本,也欢迎大家去Github上提交PR,补充其他语言版本。 + +所以题解也免费开放给录友! + +同时还给全部整理到PDF上,这份PDF,我们写的很用心了,来个大家截个图: + +
+ +
+ +
+ +
+ +关于设计模式的题目,大家现在就可以去 卡码网(kamacoder)去做了。 + +关于这23道题目对应 设计模式精讲 PDF,也免费分享给录友们,大家可以加我的企业微信获取: +
+ +已经有我企业微信的录友,直接发:设计模式,这四个字就好,我会直接发你。 +