From ce8d523de2e89a942fdcdcf534fa045ea84ed56c Mon Sep 17 00:00:00 2001 From: YiChih Wang Date: Thu, 26 Aug 2021 21:34:35 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A00059.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5II=20Rust=E8=AA=9E=E8=A8=80=E5=AF=A6=E7=8F=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note - leetcode上提交通過 --- problems/0059.螺旋矩阵II.md | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 7d02ff15..228892de 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -358,6 +358,58 @@ func generateMatrix(_ n: Int) -> [[Int]] { } ``` +Rust: + +```rust +impl Solution { + pub fn generate_matrix(n: i32) -> Vec> { + let mut res = vec![vec![0; n as usize]; n as usize]; + let (mut startX, mut startY, mut offset): (usize, usize, usize) = (0, 0, 1); + let mut loopIdx = n/2; + let mid: usize = loopIdx as usize; + let mut count = 1; + let (mut i, mut j): (usize, usize) = (0, 0); + while loopIdx > 0 { + i = startX; + j = startY; + + while j < (startY + (n as usize) - offset) { + res[i][j] = count; + count += 1; + j += 1; + } + + while i < (startX + (n as usize) - offset) { + res[i][j] = count; + count += 1; + i += 1; + } + + while j > startY { + res[i][j] = count; + count += 1; + j -= 1; + } + + while i > startX { + res[i][j] = count; + count += 1; + i -= 1; + } + + startX += 1; + startY += 1; + offset += 2; + loopIdx -= 1; + } + + if(n % 2 == 1) { + res[mid][mid] = count; + } + res + } +} +``` ----------------------- From fcc8324d31e45a26e6b1deacdcdef2e08974e602 Mon Sep 17 00:00:00 2001 From: Corki <61616424+KangJ227@users.noreply.github.com> Date: Fri, 27 Aug 2021 00:07:05 +0800 Subject: [PATCH 2/6] =?UTF-8?q?Update=200205.=E5=90=8C=E6=9E=84=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加Go语言版本 --- problems/0205.同构字符串.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md index 5b0dd82b..5d20aa4a 100644 --- a/problems/0205.同构字符串.md +++ b/problems/0205.同构字符串.md @@ -97,6 +97,23 @@ class Solution { ## Go ```go +func isIsomorphic(s string, t string) bool { + map1 := make(map[byte]byte) + map2 := make(map[byte]byte) + for i := range s { + if _, ok := map1[s[i]]; !ok { + map1[s[i]] = t[i] // map1保存 s[i] 到 t[j]的映射 + } + if _, ok := map2[t[i]]; !ok { + map2[t[i]] = s[i] // map2保存 t[i] 到 s[j]的映射 + } + // 无法映射,返回 false + if (map1[s[i]] != t[i]) || (map2[t[i]] != s[i]) { + return false + } + } + return true +} ``` ## JavaScript From 7c83141ce27f0aead1250ac9722ca54d8728912a Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Thu, 26 Aug 2021 22:25:12 -0700 Subject: [PATCH 3/6] add type for each go and js code block --- problems/二叉树理论基础.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index edd1fed4..94a0d67f 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -215,7 +215,7 @@ class TreeNode: ``` Go: -``` +```go type TreeNode struct { Val int Left *TreeNode @@ -224,7 +224,7 @@ type TreeNode struct { ``` JavaScript: -``` +```javascript function TreeNode(val, left, right) { this.val = (val===undefined ? 0 : val) this.left = (left===undefined ? null : left) From 7447e9aed0de51117277a4f3797a3283fb129b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Fri, 27 Aug 2021 13:35:24 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20349.=20=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20Swift?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0349.两个数组的交集.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 62abf639..fe9b9121 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -192,6 +192,22 @@ var intersection = function(nums1, nums2) { }; ``` +Swift: +```swift +func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] { + var set1 = Set() + var set2 = Set() + for num in nums1 { + set1.insert(num) + } + for num in nums2 { + if set1.contains(num) { + set2.insert(num) + } + } + return Array(set2) +} +``` ## 相关题目 From 3a51b65106147ccf6c503cdd443a36bcacbe4009 Mon Sep 17 00:00:00 2001 From: YDLIN <1924723909@qq.com> Date: Fri, 27 Aug 2021 18:10:28 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A00541.=20=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2II=20Swift=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0541.反转字符串II.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 02713c65..bdcb0ff8 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -226,6 +226,28 @@ var reverseStr = function(s, k) { ``` +Swift: + +```swift +func reverseStr(_ s: String, _ k: Int) -> String { + var ch = Array(s) + + for i in stride(from: 0, to: ch.count, by: 2 * k) { + var left = i + var right = min(s.count - 1, left + k - 1) + + while left < right { + (ch[left], ch[right]) = (ch[right], ch[left]) + left += 1 + right -= 1 + } + } + return String(ch) +} +``` + + + ----------------------- From 3fcf18b0489295c81d11524945ad7f494a8decc2 Mon Sep 17 00:00:00 2001 From: YDLIN <1924723909@qq.com> Date: Sat, 28 Aug 2021 11:03:51 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=89=91=E6=8C=87Offer?= =?UTF-8?q?=2005.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC=20Swift=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/剑指Offer05.替换空格.md | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 3294a16b..47907319 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -264,6 +264,49 @@ javaScript: }; ``` +Swift: + +```swift +func replaceSpace(_ s: String) -> String { + var strArr = Array(s) + var count = 0 + + // 统计空格的个数 + for i in strArr { + if i == " " { + count += 1 + } + } + // left 指向旧数组的最后一个元素 + var left = strArr.count - 1 + // right 指向扩容后数组的最后一个元素(这里还没对数组进行实际上的扩容) + var right = strArr.count + count * 2 - 1 + + // 实际对数组扩容 + for _ in 0..<(count * 2) { + strArr.append(" ") + } + + while left < right { + if strArr[left] == " " { + strArr[right] = "0" + strArr[right - 1] = "2" + strArr[right - 2] = "%" + left -= 1 + right -= 3 + } else { + strArr[right] = strArr[left] + left -= 1 + right -= 1 + } + } + + return String(strArr) +} +``` + + + -----------------------