From 9219242b3af749a96efbbaf7d8ea5089a36ba7fe Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 1 Jun 2021 20:34:57 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20go=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用map方式解题,降低时间复杂度 --- problems/0001.两数之和.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 77318294..3d0674ce 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -134,6 +134,20 @@ func twoSum(nums []int, target int) []int { } return []int{} } + +```go +// 使用map方式解题,降低时间复杂度 +func twoSum(nums []int, target int) []int { + m := make(map[int]int) + for index, val := range nums { + if preIndex, ok := m[target-val]; ok { + return []int{preIndex, index} + } else { + m[val] = index + } + } + return []int{} +} ``` Rust From 36626386c329d75524843fab42f740e548578173 Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 1 Jun 2021 20:35:55 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=9B=B4=E6=94=B9=200001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 3d0674ce..4a1a987f 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -134,6 +134,7 @@ func twoSum(nums []int, target int) []int { } return []int{} } +``` ```go // 使用map方式解题,降低时间复杂度 From ce970df5ffdb2e428b4ec2417e2bbccdc9371a31 Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 1 Jun 2021 23:43:02 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200454.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E7=9B=B8=E5=8A=A0II=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0454.四数相加II.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index 2c648f58..5291060c 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -154,6 +154,23 @@ class Solution(object): Go: +```go +func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { + m := make(map[int]int) + count := 0 + for _, v1 := range nums1 { + for _, v2 := range nums2 { + m[v1+v2]++ + } + } + for _, v3 := range nums3 { + for _, v4 := range nums4 { + count += m[-v3-v4] + } + } + return count +} +``` javaScript: From bed3f0b09fc46f11d319f33225e78e7c12d5842b Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Wed, 2 Jun 2021 20:11:11 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1=20go=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index fd8175db..23e2c5fd 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -166,6 +166,21 @@ class Solution(object): ``` Go: +```go +func canConstruct(ransomNote string, magazine string) bool { + record := make([]int, 26) + for _, v := range magazine { + record[v-'a']++ + } + for _, v := range ransomNote { + record[v-'a']-- + if record[v-'a'] < 0 { + return false + } + } + return true +} +``` javaScript: