From ea9ccc01b4721c48c40ff0d4d682e70271df8628 Mon Sep 17 00:00:00 2001 From: zouxinyao Date: Wed, 28 Jul 2021 00:26:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=AF=87=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0Go=E8=AF=AD=E8=A8=80=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode1438绝对值不超过限制的最长子数组.md | 43 +++++++++++++++ .../数组篇/leetcode1两数之和.md | 18 ++++++- .../数组篇/leetcode219数组中重复元素2.md | 24 ++++++++- .../数组篇/leetcode27移除元素.md | 23 +++++++- .../数组篇/leetcode41缺失的第一个正数.md | 24 +++++++++ .../数组篇/leetcode485最大连续1的个数.md | 27 +++++++++- .../数组篇/leetcode54螺旋矩阵.md | 37 +++++++++++++ .../数组篇/leetcode560和为K的子数组.md | 22 +++++++- .../数组篇/leetcode59螺旋矩阵2.md | 45 ++++++++++++++++ animation-simulation/数组篇/leetcode66加一.md | 16 ++++++ .../数组篇/leetcode75颜色分类.md | 53 ++++++++++++++++++- .../数组篇/剑指offer3数组中重复的数.md | 24 ++++++++- .../数组篇/长度最小的子数组.md | 33 +++++++++++- 13 files changed, 381 insertions(+), 8 deletions(-) diff --git a/animation-simulation/数组篇/leetcode1438绝对值不超过限制的最长子数组.md b/animation-simulation/数组篇/leetcode1438绝对值不超过限制的最长子数组.md index 691040c..01e067d 100644 --- a/animation-simulation/数组篇/leetcode1438绝对值不超过限制的最长子数组.md +++ b/animation-simulation/数组篇/leetcode1438绝对值不超过限制的最长子数组.md @@ -260,3 +260,46 @@ class Solution { } ``` +Go Code: + +```go +func longestSubarray(nums []int, limit int) int { + maxdeq := []int{} // 递减队列 + mindeq := []int{} // 递增队列 + + length := len(nums) + left, right, maxwin := 0, 0, 0 + for right < length { + for len(maxdeq) != 0 && maxdeq[len(maxdeq) - 1] < nums[right] { + maxdeq = maxdeq[: len(maxdeq) - 1] + } + maxdeq = append(maxdeq, nums[right]) + + for len(mindeq) != 0 && mindeq[len(mindeq) - 1] > nums[right] { + mindeq = mindeq[: len(mindeq) - 1] + } + mindeq = append(mindeq, nums[right]) + + for maxdeq[0] - mindeq[0] > limit { + if maxdeq[0] == nums[left] { + maxdeq = maxdeq[1:] + } + if mindeq[0] == nums[left] { + mindeq = mindeq[1:] + } + left++ + } + maxwin = max(maxwin, right - left + 1) + right++ + } + return maxwin +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + diff --git a/animation-simulation/数组篇/leetcode1两数之和.md b/animation-simulation/数组篇/leetcode1两数之和.md index b226bc1..8739029 100644 --- a/animation-simulation/数组篇/leetcode1两数之和.md +++ b/animation-simulation/数组篇/leetcode1两数之和.md @@ -201,4 +201,20 @@ class Solution { return [0] } } -``` \ No newline at end of file +``` + +Go Code: + +```go +func twoSum(nums []int, target int) []int { + m := make(map[int]int) + for i, num := range nums { + if v, ok := m[target - num]; ok { + return []int{v, i} + } + m[num] = i + } + return []int{} +} +``` + diff --git a/animation-simulation/数组篇/leetcode219数组中重复元素2.md b/animation-simulation/数组篇/leetcode219数组中重复元素2.md index 482c375..e44e6b6 100644 --- a/animation-simulation/数组篇/leetcode219数组中重复元素2.md +++ b/animation-simulation/数组篇/leetcode219数组中重复元素2.md @@ -223,4 +223,26 @@ class Solution { return false } } -``` \ No newline at end of file +``` + +Go Code: + +```go +func containsNearbyDuplicate(nums []int, k int) bool { + length := len(nums) + if length == 0 { + return false + } + m := map[int]int{} + for i := 0; i < length; i++ { + if v, ok := m[nums[i]]; ok { + if i - v <= k { + return true + } + } + m[nums[i]] = i + } + return false +} +``` + diff --git a/animation-simulation/数组篇/leetcode27移除元素.md b/animation-simulation/数组篇/leetcode27移除元素.md index 75a5f61..2355a93 100644 --- a/animation-simulation/数组篇/leetcode27移除元素.md +++ b/animation-simulation/数组篇/leetcode27移除元素.md @@ -183,4 +183,25 @@ class Solution { return i } } -``` \ No newline at end of file +``` + +Go Code: + +```go +func removeElement(nums []int, val int) int { + length := len(nums) + if length == 0 { + return 0 + } + i := 0 + for j := 0; j < length; j++ { + if nums[j] == val { + continue + } + nums[i] = nums[j] + i++ + } + return i +} +``` + diff --git a/animation-simulation/数组篇/leetcode41缺失的第一个正数.md b/animation-simulation/数组篇/leetcode41缺失的第一个正数.md index 784c8de..cd0de46 100644 --- a/animation-simulation/数组篇/leetcode41缺失的第一个正数.md +++ b/animation-simulation/数组篇/leetcode41缺失的第一个正数.md @@ -273,3 +273,27 @@ public: } }; ``` + +Go Code: + +```go +func firstMissingPositive(nums []int) int { + length := len(nums) + if length == 0 { return 1 } + for i := 0; i < length; i++ { + // 将不在正确位置的元素放在正确的位置上。 + for nums[i] > 0 && nums[i] < length + 1 && nums[i] != i + 1 && nums[i] != nums[nums[i] - 1] { + j := nums[i] - 1 + nums[i], nums[j] = nums[j], nums[i] + } + } + // 第一个不在正确位置上的元素就是结果。 + for i := 0; i < length; i++ { + if nums[i] != i + 1 { + return i + 1 + } + } + return length + 1 +} +``` + diff --git a/animation-simulation/数组篇/leetcode485最大连续1的个数.md b/animation-simulation/数组篇/leetcode485最大连续1的个数.md index 8e032d1..6529227 100644 --- a/animation-simulation/数组篇/leetcode485最大连续1的个数.md +++ b/animation-simulation/数组篇/leetcode485最大连续1的个数.md @@ -211,4 +211,29 @@ public: return result; } }; -``` \ No newline at end of file +``` + +Go Code: + +```go +func findMaxConsecutiveOnes(nums []int) int { + cnt, maxCnt := 0, 0 + for i := 0; i < len(nums); i++ { + if nums[i] == 1 { + cnt++ + } else { + maxCnt = max(maxCnt, cnt) + cnt = 0 + } + } + return max(maxCnt, cnt) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + diff --git a/animation-simulation/数组篇/leetcode54螺旋矩阵.md b/animation-simulation/数组篇/leetcode54螺旋矩阵.md index 28be55b..a4a9962 100644 --- a/animation-simulation/数组篇/leetcode54螺旋矩阵.md +++ b/animation-simulation/数组篇/leetcode54螺旋矩阵.md @@ -193,3 +193,40 @@ class Solution { } ``` +Go Code: + +```go +func spiralOrder(matrix [][]int) []int { + res := []int{} + left, right := 0, len(matrix[0]) - 1 + top, down := 0, len(matrix) - 1 + + for { + for i := left; i <= right; i++ { + res = append(res, matrix[top][i]) + } + top++ + if top > down { break } + + for i := top; i <= down; i++ { + res = append(res, matrix[i][right]) + } + right-- + if left > right { break } + + for i := right; i >= left; i-- { + res = append(res, matrix[down][i]) + } + down-- + if top > down { break } + + for i := down; i >= top; i-- { + res = append(res, matrix[i][left]) + } + left++ + if left > right { break } + } + return res +} +``` + diff --git a/animation-simulation/数组篇/leetcode560和为K的子数组.md b/animation-simulation/数组篇/leetcode560和为K的子数组.md index 6665d27..6808f0f 100644 --- a/animation-simulation/数组篇/leetcode560和为K的子数组.md +++ b/animation-simulation/数组篇/leetcode560和为K的子数组.md @@ -216,4 +216,24 @@ public: return result; } }; -``` \ No newline at end of file +``` + +Go Code: + +```go +func subarraySum(nums []int, k int) int { + m := map[int]int{} + m[0] = 1 + sum := 0 + cnt := 0 + for _, num := range nums { + sum += num + if v, ok := m[sum - k]; ok { + cnt += v + } + m[sum]++ + } + return cnt +} +``` + diff --git a/animation-simulation/数组篇/leetcode59螺旋矩阵2.md b/animation-simulation/数组篇/leetcode59螺旋矩阵2.md index afb5f4d..8e4fb9e 100644 --- a/animation-simulation/数组篇/leetcode59螺旋矩阵2.md +++ b/animation-simulation/数组篇/leetcode59螺旋矩阵2.md @@ -376,3 +376,48 @@ class Solution { } } ``` + +Go Code: + +```go +func generateMatrix(n int) [][]int { + res := make([][]int, n) + for i := 0; i < n; i++ { + res[i] = make([]int, n) + } + left, right := 0, n - 1 + top, buttom := 0, n - 1 + size, num := n * n, 1 + for { + for i := left; i <= right; i++ { + res[top][i] = num + num++ + } + top++ + if num > size { break } + + for i := top; i <= buttom; i++ { + res[i][right] = num + num++ + } + right-- + if num > size { break } + + for i := right; i >= left; i-- { + res[buttom][i] = num + num++ + } + buttom-- + if num > size { break } + + for i := buttom; i >= top; i-- { + res[i][left] = num + num++ + } + left++ + if num > size { break } + } + return res +} +``` + diff --git a/animation-simulation/数组篇/leetcode66加一.md b/animation-simulation/数组篇/leetcode66加一.md index 8b5d59f..e64c42b 100644 --- a/animation-simulation/数组篇/leetcode66加一.md +++ b/animation-simulation/数组篇/leetcode66加一.md @@ -123,3 +123,19 @@ class Solution { } ``` +Go Code: + +```go +func plusOne(digits []int) []int { + l := len(digits) + for i := l - 1; i >= 0; i-- { + digits[i] = (digits[i] + 1) % 10 + if digits[i] != 0 { + return digits + } + } + digits = append([]int{1}, digits...) + return digits +} +``` + diff --git a/animation-simulation/数组篇/leetcode75颜色分类.md b/animation-simulation/数组篇/leetcode75颜色分类.md index 7e65e4e..b37156e 100644 --- a/animation-simulation/数组篇/leetcode75颜色分类.md +++ b/animation-simulation/数组篇/leetcode75颜色分类.md @@ -149,6 +149,32 @@ class Solution { } ``` +Go Code: + +```go +func sortColors(nums []int) { + length := len(nums) + left, right := 0, length - 1 + i := left + for i <= right { + if nums[i] == 2 { + // 把2换到最后 + nums[i], nums[right] = nums[right], nums[i] + right-- + } else if nums[i] == 0 { + // 把0换到最前面 + nums[i], nums[left] = nums[left], nums[i] + i++ + left++ + } else { + i++ + } + } +} +``` + + + 另外我们看这段代码,有什么问题呢?那就是我们即使完全符合时,仍会交换元素,这样会大大降低我们的效率。 例如:[0,0,0,1,1,1,2,2,2] @@ -285,4 +311,29 @@ class Solution { nums[j] = temp } } -``` \ No newline at end of file +``` + +Go Code: + +```go +func sortColors(nums []int) { + length := len(nums) + left, right := 0, length - 1 + for i := 0; i <= right; i++ { + if nums[i] == 0 { + // 为0时,和头交换 + nums[i], nums[left] = nums[left], nums[i] + left++ + } else if nums[i] == 2 { + // 为2时,和尾交换 + nums[i], nums[right] = nums[right], nums[i] + right-- + // 不为1时,需要把i减回去 + if nums[i] != 1 { + i-- + } + } + } +} +``` + diff --git a/animation-simulation/数组篇/剑指offer3数组中重复的数.md b/animation-simulation/数组篇/剑指offer3数组中重复的数.md index 82c541f..34770ef 100644 --- a/animation-simulation/数组篇/剑指offer3数组中重复的数.md +++ b/animation-simulation/数组篇/剑指offer3数组中重复的数.md @@ -176,4 +176,26 @@ class Solution { return -1 } } -``` \ No newline at end of file +``` + +Go Code: + +```go +func findRepeatNumber(nums []int) int { + l := len(nums) + if l == 0 { + return -1 + } + for i := 0; i < l; i++ { + // 将nums[i]换到i的位置。 + for nums[i] != i { + if nums[i] == nums[nums[i]] { + return nums[i] + } + nums[i], nums[nums[i]] = nums[nums[i]], nums[i] + } + } + return -1 +} +``` + diff --git a/animation-simulation/数组篇/长度最小的子数组.md b/animation-simulation/数组篇/长度最小的子数组.md index b5b393d..dc52938 100644 --- a/animation-simulation/数组篇/长度最小的子数组.md +++ b/animation-simulation/数组篇/长度最小的子数组.md @@ -124,4 +124,35 @@ class Solution { return windowlen == Int.max ? 0 : windowlen } } -``` \ No newline at end of file +``` + +Go Code: + +```go +func minSubArrayLen(target int, nums []int) int { + length := len(nums) + winLen := length + 1 + i := 0 + sum := 0 + for j := 0; j < length; j++ { + sum += nums[j] + for sum >= target { + winLen = min(winLen, j - i + 1) + sum -= nums[i] + i++ + } + } + if winLen == length + 1 { + return 0 + } + return winLen +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} +``` +