数组篇 添加Go语言代码
parent
781e84ca99
commit
ea9ccc01b4
|
@ -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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -201,4 +201,20 @@ class Solution {
|
||||||
return [0]
|
return [0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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{}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -223,4 +223,26 @@ class Solution {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -183,4 +183,25 @@ class Solution {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -211,4 +211,29 @@ public:
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -216,4 +216,24 @@ public:
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -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]
|
例如:[0,0,0,1,1,1,2,2,2]
|
||||||
|
@ -285,4 +311,29 @@ class Solution {
|
||||||
nums[j] = temp
|
nums[j] = temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -176,4 +176,26 @@ class Solution {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -124,4 +124,35 @@ class Solution {
|
||||||
return windowlen == Int.max ? 0 : windowlen
|
return windowlen == Int.max ? 0 : windowlen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue