Skip to content

Commit 0611c41

Browse files
committed
finish 1228 and 1232
1 parent 296cfee commit 0611c41

File tree

7 files changed

+214
-2
lines changed

7 files changed

+214
-2
lines changed

.vscode/settings.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
2-
"python.linting.enabled": true,
3-
"java.format.settings.profile": "GoogleStyle",
2+
// "python.linting.enabled": true,
3+
// "java.format.settings.profile": "GoogleStyle",
4+
"go.inferGopath": true,
5+
"go.buildOnSave": "workspace",
6+
"go.docsTool": "gogetdoc",
7+
"go.lintTool": "golint",
8+
"editor.formatOnPaste": true,
9+
"editor.formatOnSave": true
410
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [1228. Missing Number In Arithmetic Progression](https://leetcode.com/problems/check-if-it-is-a-straight-line/)
2+
3+
## 2019/10/20
4+
5+
### 题目 💗[easy]
6+
7+
In some array arr, the values were in arithmetic progression: the values arr[i+1] - arr[i] are all equal for every 0 <= i < arr.length - 1.
8+
9+
Then, a value from arr was removed that was not the first or last value in the array.
10+
11+
Return the removed value.
12+
13+
Example 1:
14+
15+
```bash
16+
Input: arr = [5,7,11,13]
17+
Output: 9
18+
Explanation: The previous array was [5,7,9,11,13].
19+
```
20+
21+
Example 2:
22+
23+
```bash
24+
Input: arr = [15,13,12]
25+
Output: 14
26+
Explanation: The previous array was [15,14,13,12].
27+
```
28+
29+
Constraints:
30+
31+
- 3 <= arr.length <= 1000
32+
- 0 <= arr[i] <= 10^5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package missingnumber
2+
3+
import "math"
4+
5+
func missingNumber(arr []int) int {
6+
if len(arr) < 3 {
7+
return -1
8+
}
9+
intMap := make(map[int]int)
10+
for i := 1; i < len(arr); i++ {
11+
dx := arr[i] - arr[i-1]
12+
if _, ok := intMap[dx]; ok {
13+
intMap[dx]++
14+
} else {
15+
intMap[dx] = 1
16+
}
17+
}
18+
max := 0
19+
arr1 := []int{}
20+
for i := range intMap {
21+
arr1 = append(arr1, i)
22+
}
23+
if intMap[arr1[0]] > intMap[arr1[1]] {
24+
max = arr1[0]
25+
} else if intMap[arr1[0]] < intMap[arr1[1]] {
26+
max = arr1[1]
27+
} else {
28+
if math.Abs(float64(arr1[0])) < math.Abs(float64(arr1[1])) {
29+
max = arr1[0]
30+
} else {
31+
max = arr1[1]
32+
}
33+
}
34+
35+
for i := 1; i < len(arr); i++ {
36+
if max != (arr[i] - arr[i-1]) {
37+
return arr[i-1] + max
38+
}
39+
}
40+
41+
return -1
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package missingnumber
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var tcs = []struct {
10+
N1 []int
11+
ans int
12+
}{
13+
{
14+
[]int{5, 7, 11, 13},
15+
9,
16+
},
17+
{
18+
[]int{15, 13, 12},
19+
14,
20+
},
21+
{
22+
[]int{100, 300, 400},
23+
200,
24+
},
25+
}
26+
27+
func Test(t *testing.T) {
28+
ast := assert.New(t)
29+
for _, tc := range tcs {
30+
ast.Equal(tc.ans, missingNumber(tc.N1), "输入:%v", tc)
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# [1232. Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)
2+
3+
## 2019/08/17
4+
5+
### 题目 💗[easy]
6+
7+
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
8+
9+
Example 1:
10+
11+
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
12+
Output: true
13+
Example 2:
14+
15+
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
16+
Output: false
17+
18+
Constraints:
19+
20+
- 2 <= coordinates.length <= 1000
21+
- coordinates[i].length == 2
22+
- -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
23+
- coordinates contains no duplicate point.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package checkstraightline
2+
3+
func checkStraightLine(coordinates [][]int) bool {
4+
if len(coordinates) == 0 || len(coordinates) == 1 {
5+
return true
6+
}
7+
var dx float64 = float64(coordinates[1][0] - coordinates[0][0])
8+
var dy float64 = float64(coordinates[1][1] - coordinates[0][1])
9+
var dd float64 = dy / dx
10+
d0 := coordinates[0]
11+
for i, coord := range coordinates {
12+
if i == 0 || i == 1 {
13+
continue
14+
} else if float64(coord[1]-d0[1])/float64(coord[0]-d0[0]) != dd {
15+
return false
16+
}
17+
}
18+
return true
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package checkstraightline
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var tcs = []struct {
10+
N1 [][]int
11+
ans bool
12+
}{
13+
{
14+
[][]int{
15+
[]int{1, 2},
16+
[]int{2, 3},
17+
[]int{3, 4},
18+
[]int{4, 5},
19+
[]int{5, 6},
20+
[]int{6, 7},
21+
},
22+
true,
23+
},
24+
{
25+
[][]int{
26+
[]int{1, 1},
27+
[]int{2, 2},
28+
[]int{3, 4},
29+
[]int{4, 5},
30+
[]int{5, 6},
31+
[]int{7, 7},
32+
},
33+
false,
34+
},
35+
{
36+
[][]int{
37+
[]int{-3, -2},
38+
[]int{-1, -2},
39+
[]int{2, -2},
40+
[]int{-2, -2},
41+
[]int{0, -2},
42+
},
43+
true,
44+
},
45+
{
46+
[][]int{
47+
[]int{-3, -2},
48+
},
49+
true,
50+
},
51+
}
52+
53+
func Test(t *testing.T) {
54+
ast := assert.New(t)
55+
for _, tc := range tcs {
56+
ast.Equal(tc.ans, checkStraightLine(tc.N1), "输入:%v", tc)
57+
}
58+
}

0 commit comments

Comments
 (0)