Skip to content

Commit bf3ded9

Browse files
committed
update
1 parent 8d66caf commit bf3ded9

20 files changed

+1690
-0
lines changed

1137.第-n-个泰波那契数.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* @lc app=leetcode.cn id=1137 lang=golang
3+
*
4+
* [1137] 第 N 个泰波那契数
5+
*
6+
* https://leetcode-cn.com/problems/n-th-tribonacci-number/description/
7+
*
8+
* algorithms
9+
* Easy (60.78%)
10+
* Likes: 200
11+
* Dislikes: 0
12+
* Total Accepted: 122.7K
13+
* Total Submissions: 201.9K
14+
* Testcase Example: '4'
15+
*
16+
* 泰波那契序列 Tn 定义如下:
17+
*
18+
* T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2
19+
*
20+
* 给你整数 n,请返回第 n 个泰波那契数 Tn 的值。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
* 输入:n = 4
27+
* 输出:4
28+
* 解释:
29+
* T_3 = 0 + 1 + 1 = 2
30+
* T_4 = 1 + 1 + 2 = 4
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
* 输入:n = 25
36+
* 输出:1389537
37+
*
38+
*
39+
*
40+
*
41+
* 提示:
42+
*
43+
*
44+
* 0 <= n <= 37
45+
* 答案保证是一个 32 位整数,即 answer <= 2^31 - 1。
46+
*
47+
*
48+
*/
49+
50+
// @lc code=start
51+
func tribonacci(n int) int {
52+
if n == 0 {
53+
return 0
54+
}
55+
if n == 1 || n == 2 {
56+
return 1
57+
}
58+
ant := 0
59+
penu := 1
60+
last := 1
61+
62+
result := 0
63+
for i := 3; i <= n; i++ {
64+
result = ant + penu + last
65+
ant = penu
66+
penu = last
67+
last = result
68+
}
69+
return result
70+
}
71+
72+
// @lc code=end
73+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* @lc app=leetcode.cn id=1491 lang=golang
3+
*
4+
* [1491] 去掉最低工资和最高工资后的工资平均值
5+
*
6+
* https://leetcode-cn.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/description/
7+
*
8+
* algorithms
9+
* Easy (64.58%)
10+
* Likes: 34
11+
* Dislikes: 0
12+
* Total Accepted: 31.9K
13+
* Total Submissions: 49.4K
14+
* Testcase Example: '[4000,3000,1000,2000]'
15+
*
16+
* 给你一个整数数组 salary ,数组里每个数都是 唯一 的,其中 salary[i] 是第 i 个员工的工资。
17+
*
18+
* 请你返回去掉最低工资和最高工资以后,剩下员工工资的平均值。
19+
*
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
* 输入:salary = [4000,3000,1000,2000]
25+
* 输出:2500.00000
26+
* 解释:最低工资和最高工资分别是 1000 和 4000 。
27+
* 去掉最低工资和最高工资以后的平均工资是 (2000+3000)/2= 2500
28+
*
29+
*
30+
* 示例 2:
31+
*
32+
* 输入:salary = [1000,2000,3000]
33+
* 输出:2000.00000
34+
* 解释:最低工资和最高工资分别是 1000 和 3000 。
35+
* 去掉最低工资和最高工资以后的平均工资是 (2000)/1= 2000
36+
*
37+
*
38+
* 示例 3:
39+
*
40+
* 输入:salary = [6000,5000,4000,3000,2000,1000]
41+
* 输出:3500.00000
42+
*
43+
*
44+
* 示例 4:
45+
*
46+
* 输入:salary = [8000,9000,2000,3000,6000,1000]
47+
* 输出:4750.00000
48+
*
49+
*
50+
*
51+
*
52+
* 提示:
53+
*
54+
*
55+
* 3 <= salary.length <= 100
56+
* 10^3 <= salary[i] <= 10^6
57+
* salary[i] 是唯一的。
58+
* 与真实值误差在 10^-5 以内的结果都将视为正确答案。
59+
*
60+
*
61+
*/
62+
63+
// @lc code=start
64+
func average(salary []int) float64 {
65+
minS := math.MaxInt32
66+
maxS := math.MinInt32
67+
68+
sum := 0
69+
for _, s := range salary {
70+
minS = min(s, minS)
71+
maxS = max(s, maxS)
72+
sum += s
73+
}
74+
return float64(sum-minS-maxS) / float64(len(salary)-2)
75+
}
76+
77+
func min(x, y int) int {
78+
if x < y {
79+
return x
80+
}
81+
return y
82+
}
83+
func max(x, y int) int {
84+
if x > y {
85+
return x
86+
}
87+
return y
88+
}
89+
90+
// @lc code=end
91+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @lc app=leetcode.cn id=1523 lang=golang
3+
*
4+
* [1523] 在区间范围内统计奇数数目
5+
*
6+
* https://leetcode-cn.com/problems/count-odd-numbers-in-an-interval-range/description/
7+
*
8+
* algorithms
9+
* Easy (51.34%)
10+
* Likes: 49
11+
* Dislikes: 0
12+
* Total Accepted: 24K
13+
* Total Submissions: 46.8K
14+
* Testcase Example: '3\n7'
15+
*
16+
* 给你两个非负整数 low 和 high 。请你返回 low 和 high 之间(包括二者)奇数的数目。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
* 输入:low = 3, high = 7
23+
* 输出:3
24+
* 解释:3 到 7 之间奇数数字为 [3,5,7] 。
25+
*
26+
* 示例 2:
27+
*
28+
* 输入:low = 8, high = 10
29+
* 输出:1
30+
* 解释:8 到 10 之间奇数数字为 [9] 。
31+
*
32+
*
33+
*
34+
* 提示:
35+
*
36+
*
37+
* 0 <= low <= high <= 10^9
38+
*
39+
*
40+
*/
41+
42+
// @lc code=start
43+
func countOdds(low int, high int) int {
44+
45+
// [0, x]区间内的奇数为 (x+1)/2
46+
// [a, b]区间内的奇数为 [0, b] - [0, a-1]
47+
odds := func(n int) int {
48+
return (n + 1) >> 1
49+
}
50+
51+
return odds(high) - odds(low-1)
52+
53+
}
54+
55+
// @lc code=end
56+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @lc app=leetcode.cn id=1550 lang=golang
3+
*
4+
* [1550] 存在连续三个奇数的数组
5+
*
6+
* https://leetcode-cn.com/problems/three-consecutive-odds/description/
7+
*
8+
* algorithms
9+
* Easy (65.60%)
10+
* Likes: 17
11+
* Dislikes: 0
12+
* Total Accepted: 24K
13+
* Total Submissions: 36.6K
14+
* Testcase Example: '[2,6,4,1]'
15+
*
16+
* 给你一个整数数组 arr,请你判断数组中是否存在连续三个元素都是奇数的情况:如果存在,请返回 true ;否则,返回 false 。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
* 输入:arr = [2,6,4,1]
23+
* 输出:false
24+
* 解释:不存在连续三个元素都是奇数的情况。
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
* 输入:arr = [1,2,34,3,4,5,7,23,12]
30+
* 输出:true
31+
* 解释:存在连续三个元素都是奇数的情况,即 [5,7,23] 。
32+
*
33+
*
34+
*
35+
*
36+
* 提示:
37+
*
38+
*
39+
* 1 <= arr.length <= 1000
40+
* 1 <= arr[i] <= 1000
41+
*
42+
*
43+
*/
44+
45+
// @lc code=start
46+
func threeConsecutiveOdds(arr []int) bool {
47+
odds := 0
48+
for _, v := range arr {
49+
if v%2 == 1 {
50+
odds++
51+
if odds == 3 {
52+
return true
53+
}
54+
} else {
55+
odds = 0
56+
}
57+
}
58+
return false
59+
}
60+
61+
// @lc code=end
62+

1603.设计停车系统.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* @lc app=leetcode.cn id=1603 lang=golang
3+
*
4+
* [1603] 设计停车系统
5+
*
6+
* https://leetcode-cn.com/problems/design-parking-system/description/
7+
*
8+
* algorithms
9+
* Easy (84.46%)
10+
* Likes: 110
11+
* Dislikes: 0
12+
* Total Accepted: 63.7K
13+
* Total Submissions: 75.4K
14+
* Testcase Example: '["ParkingSystem","addCar","addCar","addCar","addCar"]\n' +
15+
'[[1,1,0],[1],[2],[3],[1]]'
16+
*
17+
* 请你给一个停车场设计一个停车系统。停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位。
18+
*
19+
* 请你实现 ParkingSystem 类:
20+
*
21+
*
22+
* ParkingSystem(int big, int medium, int small) 初始化 ParkingSystem
23+
* 类,三个参数分别对应每种停车位的数目。
24+
* bool addCar(int carType) 检查是否有 carType 对应的停车位。 carType 有三种类型:大,中,小,分别用数字 1,
25+
* 2 和 3 表示。一辆车只能停在  carType 对应尺寸的停车位中。如果没有空车位,请返回 false ,否则将该车停入车位并返回 true
26+
* 。
27+
*
28+
*
29+
*
30+
*
31+
* 示例 1:
32+
*
33+
*
34+
* 输入:
35+
* ["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
36+
* [[1, 1, 0], [1], [2], [3], [1]]
37+
* 输出:
38+
* [null, true, true, false, false]
39+
*
40+
* 解释:
41+
* ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
42+
* parkingSystem.addCar(1); // 返回 true ,因为有 1 个空的大车位
43+
* parkingSystem.addCar(2); // 返回 true ,因为有 1 个空的中车位
44+
* parkingSystem.addCar(3); // 返回 false ,因为没有空的小车位
45+
* parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一一个大车位已经被占据了
46+
*
47+
*
48+
*
49+
*
50+
* 提示:
51+
*
52+
*
53+
* 0
54+
* carType 取值为 1, 2 或 3
55+
* 最多会调用 addCar 函数 1000 次
56+
*
57+
*
58+
*/
59+
60+
// @lc code=start
61+
type ParkingSystem struct {
62+
Big int
63+
Medium int
64+
Small int
65+
}
66+
67+
func Constructor(big int, medium int, small int) ParkingSystem {
68+
return ParkingSystem{
69+
Big: big,
70+
Medium: medium,
71+
Small: small,
72+
}
73+
74+
}
75+
76+
func (this *ParkingSystem) AddCar(carType int) bool {
77+
switch carType {
78+
case 1:
79+
if this.Big > 0 {
80+
this.Big--
81+
return true
82+
}
83+
return false
84+
case 2:
85+
if this.Medium > 0 {
86+
this.Medium--
87+
return true
88+
}
89+
return false
90+
case 3:
91+
if this.Small > 0 {
92+
this.Small--
93+
return true
94+
}
95+
return false
96+
}
97+
return false
98+
}
99+
100+
/**
101+
* Your ParkingSystem object will be instantiated and called as such:
102+
* obj := Constructor(big, medium, small);
103+
* param_1 := obj.AddCar(carType);
104+
*/
105+
// @lc code=end
106+

0 commit comments

Comments
 (0)