Skip to content

Commit 70ada9e

Browse files
committed
添加内容
1 parent 688a384 commit 70ada9e

File tree

488 files changed

+44266
-1999
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

488 files changed

+44266
-1999
lines changed

.github/workflows/deploy.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- master # 只在master上push触发部署
7+
- add_hugo
78
paths-ignore: # 下列文件的变更不触发部署,可以自行添加
89
- README.md
910
- LICENSE

Algorithms/1093.Statistics-from-a-Large-Sample/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Return the minimum, maximum, mean, median, and mode of the sample respectively,
4545

4646

4747
- 这个问题的关键需要理解题目的意思,什么是采样?`count[k]` 就是整数 `k` 的采样个数。
48-
- 题目要求返回样本的最小值、最大值、平均值、中位数和众数。最大值和最小值就很好处理,只需要遍历 count 判断最小的非 0 的 index 就是最小值,最大的非 0 的 index 就是最大值。平均值也非常好处理,对于所有非 0 的 count,我们通过累加 count[k] * index 得到所有数的和,然后除上所有非 0 的 count 的和。![](https://latex.codecogs.com/gif.latex?%24%24%5Csum_%7Bn%3D0%7D%5E%7B256%7Dcount%5Bn%5D%28while%5C%20%5C%20count%5Bn%5D%21%3D0%29%24%24)
48+
- 题目要求返回样本的最小值、最大值、平均值、中位数和众数。最大值和最小值就很好处理,只需要遍历 count 判断最小的非 0 的 index 就是最小值,最大的非 0 的 index 就是最大值。平均值也非常好处理,对于所有非 0 的 count,我们通过累加 count[k] * index 得到所有数的和,然后除上所有非 0 的 count 的和。![](https://latex.codecogs.com/svg.latex?\sum_{n=0}^{256}count[n](while\%20\%20count[n]!=0))
4949

5050
- 众数也非常容易,只需统计 count 值最大时的 index 即可。
5151
- 中位数的处理相对麻烦一些,因为需要分非 0 的 count 之和是奇数还是偶数两种情况。先假设非 0 的 count 和为 cnt,那么如果 cnt 是奇数的话,只需要找到 cnt/2 的位置即可,通过不断累加 count 的值,直到累加和超过 ≥ cnt/2。如果 cnt 是偶数的话,需要找到 cnt/2 + 1 和 cnt/2 的位置,找法和奇数情况相同,不过需要找两次(可以放到一个循环中做两次判断)。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func balancedStringSplit(s string) int {
4+
count, res := 0, 0
5+
for _, r := range s {
6+
if r == 'R' {
7+
count++
8+
} else {
9+
count--
10+
}
11+
if count == 0 {
12+
res++
13+
}
14+
}
15+
return res
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1221 struct {
9+
para1221
10+
ans1221
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1221 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1221 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1221(t *testing.T) {
26+
27+
qs := []question1221{
28+
29+
question1221{
30+
para1221{"RLRRLLRLRL"},
31+
ans1221{4},
32+
},
33+
34+
question1221{
35+
para1221{"RLLLLRRRLR"},
36+
ans1221{3},
37+
},
38+
39+
question1221{
40+
para1221{"LLLLRRRR"},
41+
ans1221{1},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 1221------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans1221, q.para1221
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, balancedStringSplit(p.s))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [1221. Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/)
2+
3+
4+
## 题目:
5+
6+
Balanced strings are those who have equal quantity of 'L' and 'R' characters.
7+
8+
Given a balanced string `s` split it in the maximum amount of balanced strings.
9+
10+
Return the maximum amount of splitted balanced strings.
11+
12+
**Example 1**:
13+
14+
Input: s = "RLRRLLRLRL"
15+
Output: 4
16+
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
17+
18+
**Example 2**:
19+
20+
Input: s = "RLLLLRRRLR"
21+
Output: 3
22+
Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'.
23+
24+
**Example 3**:
25+
26+
Input: s = "LLLLRRRR"
27+
Output: 1
28+
Explanation: s can be split into "LLLLRRRR".
29+
30+
**Constraints**:
31+
32+
- `1 <= s.length <= 1000`
33+
- `s[i] = 'L' or 'R'`
34+
35+
## 题目大意
36+
37+
38+
在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的。给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。返回可以通过分割得到的平衡字符串的最大数量。
39+
40+
提示:
41+
42+
- 1 <= s.length <= 1000
43+
- s[i] = 'L' 或 'R'
44+
45+
46+
## 解题思路
47+
48+
- 给出一个字符串,要求把这个字符串切成一些子串,这些子串中 R 和 L 的字符数是相等的。问能切成多少个满足条件的子串。
49+
- 这道题是简单题,按照题意模拟即可。从左往右扫,遇到 `R` 就加一,遇到 `L` 就减一,当计数是 `0` 的时候就是平衡的时候,就切割。
50+
51+
## 代码
52+
53+
```go
54+
55+
package leetcode
56+
57+
func balancedStringSplit(s string) int {
58+
count, res := 0, 0
59+
for _, r := range s {
60+
if r == 'R' {
61+
count++
62+
} else {
63+
count--
64+
}
65+
if count == 0 {
66+
res++
67+
}
68+
}
69+
return res
70+
}
71+
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode
2+
3+
func checkStraightLine(coordinates [][]int) bool {
4+
dx0 := coordinates[1][0] - coordinates[0][0]
5+
dy0 := coordinates[1][1] - coordinates[0][1]
6+
for i := 1; i < len(coordinates)-1; i++ {
7+
dx := coordinates[i+1][0] - coordinates[i][0]
8+
dy := coordinates[i+1][1] - coordinates[i][1]
9+
if dy*dx0 != dy0*dx { // check cross product
10+
return false
11+
}
12+
}
13+
return true
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1232 struct {
9+
para1232
10+
ans1232
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1232 struct {
16+
arr [][]int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1232 struct {
22+
one bool
23+
}
24+
25+
func Test_Problem1232(t *testing.T) {
26+
27+
qs := []question1232{
28+
29+
question1232{
30+
para1232{[][]int{[]int{1, 2}, []int{2, 3}, []int{3, 4}, []int{4, 5}, []int{5, 6}, []int{6, 7}}},
31+
ans1232{true},
32+
},
33+
34+
question1232{
35+
para1232{[][]int{[]int{1, 1}, []int{2, 2}, []int{3, 4}, []int{4, 5}, []int{5, 6}, []int{7, 7}}},
36+
ans1232{false},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 1232------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans1232, q.para1232
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, checkStraightLine(p.arr))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [1232. Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)
2+
3+
4+
## 题目:
5+
6+
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.
7+
8+
**Example 1**:
9+
10+
![](https://img.halfrost.com/Leetcode/leetcode_1232_1.png)
11+
12+
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
13+
Output: true
14+
15+
**Example 2**:
16+
17+
18+
![](https://img.halfrost.com/Leetcode/leetcode_1232_2.png)
19+
20+
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
21+
Output: false
22+
23+
**Constraints**:
24+
25+
- `2 <= coordinates.length <= 1000`
26+
- `coordinates[i].length == 2`
27+
- `-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4`
28+
- `coordinates` contains no duplicate point.
29+
30+
## 题目大意
31+
32+
33+
在一个 XY 坐标系中有一些点,我们用数组 coordinates 来分别记录它们的坐标,其中 coordinates[i] = [x, y] 表示横坐标为 x、纵坐标为 y 的点。
34+
35+
请你来判断,这些点是否在该坐标系中属于同一条直线上,是则返回 true,否则请返回 false。
36+
37+
提示:
38+
39+
- 2 <= coordinates.length <= 1000
40+
- coordinates[i].length == 2
41+
- -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
42+
- coordinates 中不含重复的点
43+
44+
45+
46+
## 解题思路
47+
48+
- 给出一组坐标点,要求判断这些点是否在同一直线上。
49+
- 按照几何原理,依次计算这些点的斜率是否相等即可。斜率需要做除法,这里采用一个技巧是换成乘法。例如 `a/b = c/d` 换成乘法是 `a*d = c*d`
50+
51+
52+
## 代码
53+
54+
```go
55+
56+
package leetcode
57+
58+
func checkStraightLine(coordinates [][]int) bool {
59+
dx0 := coordinates[1][0] - coordinates[0][0]
60+
dy0 := coordinates[1][1] - coordinates[0][1]
61+
for i := 1; i < len(coordinates)-1; i++ {
62+
dx := coordinates[i+1][0] - coordinates[i][0]
63+
dy := coordinates[i+1][1] - coordinates[i][1]
64+
if dy*dx0 != dy0*dx { // check cross product
65+
return false
66+
}
67+
}
68+
return true
69+
}
70+
71+
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode
2+
3+
func balancedString(s string) int {
4+
count, k := make([]int, 128), len(s)/4
5+
for _, v := range s {
6+
count[int(v)]++
7+
}
8+
left, right, res := 0, -1, len(s)
9+
for left < len(s) {
10+
if count['Q'] > k || count['W'] > k || count['E'] > k || count['R'] > k {
11+
if right+1 < len(s) {
12+
right++
13+
count[s[right]]--
14+
} else {
15+
break
16+
}
17+
} else {
18+
res = min(res, right-left+1)
19+
count[s[left]]++
20+
left++
21+
}
22+
}
23+
return res
24+
}
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 question1234 struct {
9+
para1234
10+
ans1234
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1234 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1234 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1234(t *testing.T) {
26+
27+
qs := []question1234{
28+
29+
question1234{
30+
para1234{"QWER"},
31+
ans1234{0},
32+
},
33+
34+
question1234{
35+
para1234{"QQWE"},
36+
ans1234{1},
37+
},
38+
39+
question1234{
40+
para1234{"QQQW"},
41+
ans1234{2},
42+
},
43+
44+
question1234{
45+
para1234{"QQQQ"},
46+
ans1234{3},
47+
},
48+
49+
question1234{
50+
para1234{"WQWRQQQW"},
51+
ans1234{3},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 1234------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans1234, q.para1234
59+
fmt.Printf("【input】:%v 【output】:%v\n", p, balancedString(p.s))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}

0 commit comments

Comments
 (0)