-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path209.长度最小的子数组.go
79 lines (68 loc) · 1.28 KB
/
209.长度最小的子数组.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* @lc app=leetcode.cn id=209 lang=golang
*
* [209] 长度最小的子数组
*/
// @lc code=start
// 输入:s = 7, nums = [2,3,1,2,4,3]
// 输出:2
// 解释:子数组 [4,3] 是该条件下的长度最小的子数组。
package main
import "fmt"
func sum2(s []int) int{
sum1 := 0
for _, v := range s {
sum1 += v
}
return sum1
}
func minSubArrayLen(s int, nums []int) int {
if len(nums) == 0 {
return 0
}
i := 0
j := 0
size := len(nums)
result := 0
sum := nums[0]
for {
// 总数比 s 大,则i向右前进
if sum >= s {
if i >= size {
break
}
leng := j - i + 1
// if leng == 1 {
// result = j - i + 1
// break
// }
if leng < result || result == 0 {
result = leng
}
fmt.Printf("first, i is %d, j is %d, sum is %d\n", i, j, sum2(nums[i:j+1]))
if i <= j {
sum -= nums[i]
i++
}
// 总数比 s 小,则i向右前进
} else {
fmt.Printf("second: i is %d, j is %d, sum is %d\n", i, j, sum2(nums[i:j+1]))
j++
if j >= size {
break
}
sum += nums[j]
}
}
return result
}
// @lc code=end
func main() {
s := 7
nums := []int{2,3,1,2,4,3}
fmt.Printf("%v\n", nums)
fmt.Printf("min sub is %d\n", minSubArrayLen(s, nums))
// b := []int{}
// b = append(b, nums[5:7]...)
// fmt.Println(b)
}