Skip to content

Commit 3f9ab8a

Browse files
committed
添加 problem 1189
1 parent 596f08c commit 3f9ab8a

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode
2+
3+
func maxNumberOfBalloons(text string) int {
4+
fre := make([]int, 26)
5+
for _, t := range text {
6+
fre[t-'a']++
7+
}
8+
// 字符 b 的频次是数组下标 1 对应的元素值
9+
// 字符 a 的频次是数组下标 0 对应的元素值
10+
// 字符 l 的频次是数组下标 11 对应的元素值,这里有 2 个 l,所以元素值需要除以 2
11+
// 字符 o 的频次是数组下标 14 对应的元素值,这里有 2 个 o,所以元素值需要除以 2
12+
// 字符 n 的频次是数组下标 13 对应的元素值
13+
return min(fre[1], min(fre[0], min(fre[11]/2, min(fre[14]/2, fre[13]))))
14+
}
Lines changed: 52 additions & 0 deletions
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 question1189 struct {
9+
para1189
10+
ans1189
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1189 struct {
16+
text string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1189 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1189(t *testing.T) {
26+
27+
qs := []question1189{
28+
29+
question1189{
30+
para1189{"nlaebolko"},
31+
ans1189{1},
32+
},
33+
34+
question1189{
35+
para1189{"loonbalxballpoon"},
36+
ans1189{2},
37+
},
38+
39+
question1189{
40+
para1189{"leetcode"},
41+
ans1189{0},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 1189------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans1189, q.para1189
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, maxNumberOfBalloons(p.text))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [1189. Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/)
2+
3+
4+
## 题目:
5+
6+
Given a string `text`, you want to use the characters of `text` to form as many instances of the word **"balloon"** as possible.
7+
8+
You can use each character in `text` **at most once**. Return the maximum number of instances that can be formed.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2019/09/05/1536_ex1_upd.JPG)
13+
14+
Input: text = "nlaebolko"
15+
Output: 1
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2019/09/05/1536_ex2_upd.JPG)
20+
21+
Input: text = "loonbalxballpoon"
22+
Output: 2
23+
24+
**Example 3:**
25+
26+
Input: text = "leetcode"
27+
Output: 0
28+
29+
**Constraints:**
30+
31+
- `1 <= text.length <= 10^4`
32+
- `text` consists of lower case English letters only.
33+
34+
35+
## 题目大意
36+
37+
给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。
38+
39+
提示:
40+
41+
- 1 <= text.length <= 10^4
42+
- text 全部由小写英文字母组成
43+
44+
## 解题思路
45+
46+
47+
- 给出一个字符串,问这个字符串里面的数组能组成多少个 **balloon** 这个单词。
48+
- 简单题,先统计 26 个字母每个字母的频次,然后取出 balloon 这 5 个字母出现频次最小的值就是结果。

0 commit comments

Comments
 (0)