Skip to content

Commit 2331702

Browse files
committed
添加 problem 978
1 parent 7468234 commit 2331702

File tree

5 files changed

+166
-10
lines changed

5 files changed

+166
-10
lines changed

Algorithms/0498. Diagonal Traverse/498. Diagonal Traverse.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ func findDiagonalOrder1(matrix [][]int) []int {
55
if matrix == nil || len(matrix) == 0 || len(matrix[0]) == 0 {
66
return nil
77
}
8-
row := len(matrix)
9-
col := len(matrix[0])
10-
dir := [2][2]int{
8+
row, col, dir, i, x, y, d := len(matrix), len(matrix[0]), [2][2]int{
119
{-1, 1},
1210
{1, -1},
13-
}
11+
}, 0, 0, 0, 0
1412
total := row * col
1513
res := make([]int, total)
16-
var i, x, y, d int
1714
for i < total {
1815
for x >= 0 && x < row && y >= 0 && y < col {
1916
res[i] = matrix[x][y]

Algorithms/0509. Fibonacci Number/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
# 509. Fibonacci Number
1+
# [509. Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)
22

3-
Created Time: Aug 24, 2019 2:52 PM
4-
Difficulty: Easy
5-
Link: https://leetcode.com/problems/fibonacci-number/
6-
Tags: Array,Dynamic Programming,Math,Recursive
73

84
## 题目:
95

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode
2+
3+
// 解法一 模拟法
4+
func maxTurbulenceSize(A []int) int {
5+
inc, dec := 1, 1
6+
maxLen := min(1, len(A))
7+
for i := 1; i < len(A); i++ {
8+
if A[i-1] < A[i] {
9+
inc = dec + 1
10+
dec = 1
11+
} else if A[i-1] > A[i] {
12+
dec = inc + 1
13+
inc = 1
14+
} else {
15+
inc = 1
16+
dec = 1
17+
}
18+
maxLen = max(maxLen, max(inc, dec))
19+
}
20+
return maxLen
21+
}
22+
23+
// 解法二 滑动窗口
24+
func maxTurbulenceSize1(A []int) int {
25+
if len(A) == 1 {
26+
return 1
27+
}
28+
// flag > 0 代表下一个数要大于前一个数,flag < 0 代表下一个数要小于前一个数
29+
res, left, right, flag, lastNum := 0, 0, 0, A[1]-A[0], A[0]
30+
for left < len(A) {
31+
if right < len(A)-1 && ((A[right+1] > lastNum && flag > 0) || (A[right+1] < lastNum && flag < 0) || (right == left)) {
32+
right++
33+
flag = lastNum - A[right]
34+
lastNum = A[right]
35+
} else {
36+
if right != left && flag != 0 {
37+
res = max(res, right-left+1)
38+
}
39+
left++
40+
}
41+
}
42+
return max(res, 1)
43+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question978 struct {
9+
para978
10+
ans978
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para978 struct {
16+
one []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans978 struct {
22+
one int
23+
}
24+
25+
func Test_Problem978(t *testing.T) {
26+
27+
qs := []question978{
28+
29+
question978{
30+
para978{[]int{0, 1, 1, 0, 1, 0, 1, 1, 0, 0}},
31+
ans978{5},
32+
},
33+
34+
question978{
35+
para978{[]int{9, 9}},
36+
ans978{1},
37+
},
38+
39+
question978{
40+
para978{[]int{9, 4, 2, 10, 7, 8, 8, 1, 9}},
41+
ans978{5},
42+
},
43+
44+
question978{
45+
para978{[]int{4, 8, 12, 16}},
46+
ans978{2},
47+
},
48+
49+
question978{
50+
para978{[]int{100}},
51+
ans978{1},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 978------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans978, q.para978
59+
fmt.Printf("【input】:%v 【output】:%v\n", p, maxTurbulenceSize(p.one))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# [978. Longest Turbulent Subarray](https://leetcode.com/problems/longest-turbulent-subarray/)
2+
3+
## 题目:
4+
5+
A subarray `A[i], A[i+1], ..., A[j]` of `A` is said to be *turbulent* if and only if:
6+
7+
- For `i <= k < j``A[k] > A[k+1]` when `k` is odd, and `A[k] < A[k+1]` when `k` is even;
8+
- **OR**, for `i <= k < j``A[k] > A[k+1]` when `k` is even, and `A[k] < A[k+1]` when `k` is odd.
9+
10+
That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.
11+
12+
Return the **length** of a maximum size turbulent subarray of A.
13+
14+
**Example 1:**
15+
16+
Input: [9,4,2,10,7,8,8,1,9]
17+
Output: 5
18+
Explanation: (A[1] > A[2] < A[3] > A[4] < A[5])
19+
20+
**Example 2:**
21+
22+
Input: [4,8,12,16]
23+
Output: 2
24+
25+
**Example 3:**
26+
27+
Input: [100]
28+
Output: 1
29+
30+
**Note:**
31+
32+
1. `1 <= A.length <= 40000`
33+
2. `0 <= A[i] <= 10^9`
34+
35+
36+
## 题目大意
37+
38+
39+
当 A 的子数组 A[i], A[i+1], ..., A[j] 满足下列条件时,我们称其为湍流子数组:
40+
41+
若 i <= k < j,当 k 为奇数时, A[k] > A[k+1],且当 k 为偶数时,A[k] < A[k+1]
42+
或 若 i <= k < j,当 k 为偶数时,A[k] > A[k+1] ,且当 k 为奇数时, A[k] < A[k+1]
43+
也就是说,如果比较符号在子数组中的每个相邻元素对之间翻转,则该子数组是湍流子数组。
44+
45+
返回 A 的最大湍流子数组的长度。
46+
47+
提示:
48+
49+
- 1 <= A.length <= 40000
50+
- 0 <= A[i] <= 10^9
51+
52+
53+
54+
## 解题思路
55+
56+
57+
- 给出一个数组,要求找出“摆动数组”的最大长度。所谓“摆动数组”的意思是,元素一大一小间隔的。
58+
- 这一题可以用滑动窗口来解答。用一个变量记住下次出现的元素需要大于还是需要小于前一个元素。也可以用模拟的方法,用两个变量分别记录上升和下降数字的长度。一旦元素相等了,上升和下降数字长度都置为 1,其他时候按照上升和下降的关系增加队列长度即可,最后输出动态维护的最长长度。

0 commit comments

Comments
 (0)