From 3933b49cb36eaacb1fca0d40861386722b32347e Mon Sep 17 00:00:00 2001 From: han Date: 2023年7月26日 17:21:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A01002.=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70347円224円250円345円255円227円347円254円246円.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" "b/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" index a53148b313..81b07b3943 100644 --- "a/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" +++ "b/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" @@ -518,6 +518,54 @@ impl Solution { } ``` +Ruby: +```ruby +def common_chars(words) + result = [] + #统计所有字符串里字符出现的最小频率 + hash = {} + #初始化标识 + is_first = true + + words.each do |word| + #记录共同字符 + chars = [] + word.split('').each do |chr| + #第一个字符串初始化 + if is_first + chars << chr + else + #字母之前出现过的最小次数 + if hash[chr] != nil && hash[chr]> 0 + hash[chr] -= 1 + chars << chr + end + end + end + + is_first = false + #清除hash,更新字符最小频率 + hash.clear + chars.each do |chr| + if hash[chr] != nil + hash[chr] += 1 + else + hash[chr] = 1 + end + end + end + + #字符最小频率hash转换为字符数组 + hash.keys.each do |key| + for i in 0..hash[key] - 1 + result << key + end + end + + return result +end +``` +

From 3f0966382e37245b96e7967f95e725f37c2cbdc6 Mon Sep 17 00:00:00 2001 From: leslieCHUENGT <1278207976@qq.com> Date: 2023年7月27日 09:56:18 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A00463=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E5=91=A8=E9=95=BF=20TypeScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77347円232円204円345円221円250円351円225円277円.md" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git "a/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" "b/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" index 18f1d01eb2..35b89dcf87 100644 --- "a/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" +++ "b/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" @@ -300,6 +300,104 @@ var islandPerimeter = function(grid) { }; ``` +TypeScript: + +```typescript +/** + * 方法一:深度优先搜索(DFS) + * @param grid 二维网格地图,其中 grid[i][j] = 1 表示陆地, grid[i][j] = 0 表示水域 + * @returns 岛屿的周长 + */ +function islandPerimeter(grid: number[][]): number { + // 处理特殊情况:网格为空或行列数为 0,直接返回 0 + if (!grid || grid.length === 0 || grid[0].length === 0) { + return 0; + } + + // 获取网格的行数和列数 + const rows = grid.length; + const cols = grid[0].length; + let perimeter = 0; // 岛屿的周长 + + /** + * 深度优先搜索函数 + * @param i 当前格子的行索引 + * @param j 当前格子的列索引 + */ + const dfs = (i: number, j: number) => { + // 如果当前位置超出网格范围,或者当前位置是水域(grid[i][j] === 0),则周长增加1 + if (i < 0 || i>= rows || j < 0 || j>= cols || grid[i][j] === 0) { + perimeter++; + return; + } + + // 如果当前位置已经访问过(grid[i][j] === -1),则直接返回 + if (grid[i][j] === -1) { + return; + } + + // 标记当前位置为已访问(-1),避免重复计算 + grid[i][j] = -1; + + // 继续搜索上、下、左、右四个方向 + dfs(i + 1, j); + dfs(i - 1, j); + dfs(i, j + 1); + dfs(i, j - 1); + }; + + // 遍历整个网格,找到第一个陆地格子(grid[i][j] === 1),并以此为起点进行深度优先搜索 + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (grid[i][j] === 1) { + dfs(i, j); + break; + } + } + } + + return perimeter; +} + +/** + * 方法二:遍历每个陆地格子,统计周长 + * @param grid 二维网格地图,其中 grid[i][j] = 1 表示陆地, grid[i][j] = 0 表示水域 + * @returns 岛屿的周长 + */ +function islandPerimeter(grid: number[][]): number { + // 处理特殊情况:网格为空或行列数为 0,直接返回 0 + if (!grid || grid.length === 0 || grid[0].length === 0) { + return 0; + } + + // 获取网格的行数和列数 + const rows = grid.length; + const cols = grid[0].length; + let perimeter = 0; // 岛屿的周长 + + // 遍历整个网格 + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + // 如果当前格子是陆地(grid[i][j] === 1) + if (grid[i][j] === 1) { + perimeter += 4; // 周长先加上4个边 + + // 判断当前格子的上方是否也是陆地,如果是,则周长减去2个边 + if (i> 0 && grid[i - 1][j] === 1) { + perimeter -= 2; + } + + // 判断当前格子的左方是否也是陆地,如果是,则周长减去2个边 + if (j> 0 && grid[i][j - 1] === 1) { + perimeter -= 2; + } + } + } + } + + return perimeter; +} +```

From 7ee623f4da4e6615f867d2456a6ec174362f0100 Mon Sep 17 00:00:00 2001 From: han Date: 2023年7月27日 16:27:19 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A00349.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20Ruby=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04347円232円204円344円272円244円351円233円206円.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" "b/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" index 8daf5a35ba..26a9286dc9 100644 --- "a/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" +++ "b/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" @@ -465,6 +465,25 @@ object Solution { } ``` + +###Ruby +```ruby +def intersection(nums1, nums2) + hash = {} + result = {} + + nums1.each do |num| + hash[num] = 1 if hash[num].nil? + end + + nums2.each do |num| + #取nums1和nums2交集 + result[num] = 1 if hash[num] != nil + end + + return result.keys +end +``` ## 相关题目 * [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/)

AltStyle によって変換されたページ (->オリジナル) /