这是小川的第416次更新,第449篇原创

看题和准备

今天介绍的是LeetCode算法题中Easy级别的第267题(顺位题号是1189)。给定一个字符串文本,使用文本字符来构成单词"balloon"的尽可能多的实例。每个字符最多可以在文本中使用一次。返回可以形成的最大实例数。

例如:

输入:text = "nlaebolko"

输出:1

输入:text = "loonbalxballpoon"

输出:2

输入:text = "leetcode"

输出:0

约束

  • 1 <= text.length <= 10^4
  • 文本字符串仅包含小写英文字母。

第一种解法

题目的意思是要在一个给定的字符串中,找到能够组成字符串"balloon"的最大字符对数,本质上和木桶装水的容量由短板决定类似。

直接遍历text字符串中的字符,对字母ablno的出现次数计数,因为l和o是需要两个,在计数完后,需要对lo的次数除2,然后比较5个字母出现次数的最小值,因为只有出现次数最小的那个字母才能最终决定组成多少个"balloon"

public int maxNumberOfBalloons(String text) {
if (text == "" || text.length() < 7) {
return 0;
}
int A = 0, B = 0, L = 0, O = 0, N = 0;
int len = text.length();
for (int i=0; i<len; i++) {
if (text.charAt(i) == 'a') {
A++;
} else if (text.charAt(i) == 'b') {
B++;
} else if (text.charAt(i) == 'l') {
L++;
} else if (text.charAt(i) == 'n') {
N++;
} else if (text.charAt(i) == 'o') {
O++;
}
}
L /= 2;
O /= 2;
int min = Math.min(A, B);
min = Math.min(min, N);
if (min == 0) {
return 0;
}
if (L !=0 && O != 0) {
min = Math.min(min, L);
min = Math.min(min, O);
return min;
}
return 0;
}

第二种解法

针对第一种解法里,在循环中判断字母的方式,可以换成使用一个长度为26的int数组,根据26个英文字母的顺序,直接从数组中取值即可,最后返回5个数中的最小值。

public int maxNumberOfBalloons2(String text) {
if (text == "" || text.length() < 7) {
return 0;
}
int A = 0, B = 0, L = 0, O = 0, N = 0;
int len = text.length();
int[] arr = new int[26];
for (int i=0; i<len; i++) {
arr[text.charAt(i)-'a']++;
}
A = arr[0];
B = arr[1];
L = arr[11]/2;
N = arr[13];
O = arr[14]/2;
int min = Math.min(A, B);
min = Math.min(min, N);
min = Math.min(min, L);
min = Math.min(min, O);
return min;
}

第三种解法

针对第二种解法,我们可以将5个局部变量也省略掉,毕竟只是比较最小值,直接去数组里取就行。

public int maxNumberOfBalloons3(String text) {
if (text == "" || text.length() < 7) {
return 0;
}
int len = text.length();
int[] arr = new int[26];
for (int i=0; i<len; i++) {
arr[text.charAt(i)-'a']++;
}
int min = Math.min(arr[0], arr[1]); //a b
min = Math.min(min, arr[13]); // n
min = Math.min(min, arr[11]/2); // l
min = Math.min(min, arr[14]/2); // o
return min;
}

小结

算法专题目前已更新LeetCode算法题文章273+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、在看就是对我最大的回报和支持!

LeetCode.1189-balloon实例数最大值(Maximum Number of Balloons)的更多相关文章

  1. 【leetcode】1189. Maximum Number of Balloons

    题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...

  2. 【leetcode】1072. Flip Columns For Maximum Number of Equal Rows

    题目如下: Given a matrix consisting of 0s and 1s, we may choose any number of columns in the matrix and ...

  3. [LeetCode] Third Maximum Number 第三大的数

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  4. 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...

  5. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  6. LeetCode 414 Third Maximum Number

    Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...

  7. LeetCode 414. Third Maximum Number (第三大的数)

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  8. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. 【leetcode】414. Third Maximum Number

    problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...

随机推荐

  1. Linux命令之tree(目录树结构)

    tree命令 官方下载地址:http://mama.indstate.edu/users/ice/tree/,右键复制如下图地址: 下载: 执行wget  http://mama.indstate.e ...

  2. 【leetcode】1288. Remove Covered Intervals

    题目如下: Given a list of intervals, remove all intervals that are covered by another interval in the li ...

  3. 校验正确获取对象或者数组的属性方法(babel-plugin-idx/_.get)

    背景: 开发中经常遇到取值属性的时候,需要校验数值的有效性. 例如: 获取props对象里面的friends属性 props.user && props.user.friends &a ...

  4. js 获取当前focus 的 input 元素

    document.querySelector("#pro-code").focus(); console.log("focus:" + document.act ...

  5. hdu 5514 Frogs 容斥思想+gcd 银牌题

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. Hdu 1247 Hat's Words(Trie树)

    Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. 捣乱Linux环境下的C语言

    #include <stdlib.h> 头文件作用.CSDN C 标准库 – <stdlib.h> | 菜鸟教程  https://www.runoob.com/cprogra ...

  8. mybatis 多级级联(多级嵌套)

    注:笔者这里的嵌套可以用词有点欠缺,忘见谅 需求:用一个查询接口查出其结果集,这里就用伪代码标识要返回前端的类与类之间的关系. class 顶层{ String otherValue; LinkedL ...

  9. Leetcode题目分类整理

    一.数组 8) 双指针 ---- 滑动窗口 例题: 3. Longest Substring Without Repeating Characters 描述:Given a string, find ...

  10. JS的数据储存格式

    javaScript有三种数据存储方式,分别是:sessionStoragelocalStoragecookier 相同点:都保存在浏览器端,同源的 不同点:①传递方式不同cookie数据始终在同源的 ...