From 6b34fcd0df9a1e76322ac409fab5cf3149ecd574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Sun, 13 Feb 2022 21:47:52 +0800 Subject: [PATCH] =?UTF-8?q?1189:=E2=80=9C=E6=B0=94=E7=90=83=E2=80=9D=20?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/MaximumNumberOfBalloons.java | 65 +++++++++++++++++++ .../cn/doc/content/MaximumNumberOfBalloons.md | 37 +++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/MaximumNumberOfBalloons.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/MaximumNumberOfBalloons.md diff --git a/src/main/java/leetcode/editor/cn/MaximumNumberOfBalloons.java b/src/main/java/leetcode/editor/cn/MaximumNumberOfBalloons.java new file mode 100644 index 0000000..5c2a4f6 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/MaximumNumberOfBalloons.java @@ -0,0 +1,65 @@ +//给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。 +// +// 字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。 +// +// +// +// 示例 1: +// +// +// +// 输入:text = "nlaebolko" +//输出:1 +// +// +// 示例 2: +// +// +// +// 输入:text = "loonbalxballpoon" +//输出:2 +// +// +// 示例 3: +// +// 输入:text = "leetcode" +//输出:0 +// +// +// +// +// 提示: +// +// +// 1 <= text.length <= 10^4 +// text 全部由小写英文字母组成 +// +// Related Topics 哈希表 字符串 计数 👍 102 👎 0 + +package leetcode.editor.cn; + +//1189:“气球” 的最大数量 +class MaximumNumberOfBalloons { + public static void main(String[] args) { + //测试代码 + Solution solution = new MaximumNumberOfBalloons().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int maxNumberOfBalloons(String text) { + int[] arrs = new int[26]; + for (char ch : text.toCharArray()) { + arrs[ch - 'a']++; + } + int count = Math.min(arrs[0], arrs[1]); + count = Math.min(count, arrs['l' - 'a'] / 2); + count = Math.min(count, arrs['o' - 'a'] / 2); + count = Math.min(count, arrs['n' - 'a']); + return count; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/MaximumNumberOfBalloons.md b/src/main/java/leetcode/editor/cn/doc/content/MaximumNumberOfBalloons.md new file mode 100644 index 0000000..d84f5f5 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/MaximumNumberOfBalloons.md @@ -0,0 +1,37 @@ +

给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)

+ +

字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"

+ +

 

+ +

示例 1:

+ +

+ +
输入:text = "nlaebolko"
+输出:1
+
+ +

示例 2:

+ +

+ +
输入:text = "loonbalxballpoon"
+输出:2
+
+ +

示例 3:

+ +
输入:text = "leetcode"
+输出:0
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 哈希表
  • 字符串
  • 计数

  • 👍 102
  • 👎 0
  • \ No newline at end of file