LeetCode.1189-balloon实例数最大值(Maximum Number of Balloons)
这是小川的第416次更新,第449篇原创
看题和准备
今天介绍的是LeetCode算法题中Easy级别的第267题(顺位题号是1189)。给定一个字符串文本,使用文本字符来构成单词"balloon"的尽可能多的实例。每个字符最多可以在文本中使用一次。返回可以形成的最大实例数。
例如:
输入:text = "nlaebolko"
输出:1
输入:text = "loonbalxballpoon"
输出:2
输入:text = "leetcode"
输出:0
约束:
- 1 <= text.length <= 10^4
- 文本字符串仅包含小写英文字母。
第一种解法
题目的意思是要在一个给定的字符串中,找到能够组成字符串"balloon"的最大字符对数,本质上和木桶装水的容量由短板决定类似。
直接遍历text字符串中的字符,对字母a、b、l、n、o的出现次数计数,因为l和o是需要两个,在计数完后,需要对l和o的次数除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)的更多相关文章
- 【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 ...
- 【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 ...
- [LeetCode] Third Maximum Number 第三大的数
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- 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 ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
随机推荐
- HDU 6415 Rikka with Nash Equilibrium (计数DP)
题意:给两个整数n,m,让你使用 1 ~ n*m的所有数,构造一个矩阵n*m的矩阵,此矩阵满足:只有一个元素在它的此行和此列中都是最大的,求有多种方式. 析:根据题意,可以知道那个元素一定是 n * ...
- Mybatis那些事-拦截器(Plugin+Interceptor)
作者:yhjyumi的专栏 数据权限实现(Mybatis拦截器+JSqlParser) Mybatis的拦截器实现机制,使用的是JDK的InvocationHandler. 当我们调用Paramete ...
- CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)
起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...
- Can't connect to MySQL server on xxx (10061)
报错原因,数据库服务没有启动,在JDBC连接mysql数据库时会报错 解决方式,在服务中启用Mysql 备注:运行环境: windows10 x64 JDK 1.8.0_181 mysql-conne ...
- Vue + Vuex 简单使用
我们要实现的很简单,就是点击+1的count加一,点击-1的时候count-1 一.mutation 在vue 中,只有mutation 才能改变state. mutation 类似事件,每一个mu ...
- Go中&和*的区别
& 返回变量的内存地址 * 返回变量的值, * 只能作用在指针上 package main import "fmt" func main() { var a = 5 var ...
- redis基本介绍
1.Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能(NOSQL)的key-value数据库,Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化 ...
- python能用来做什么?这3大主要用途你一定要知道!(实用)
导读:如果你想学Python,或者你刚开始学习Python,那么你可能会问:“我能用Python做什么?” 这个问题不好回答,因为Python有很多用途. 但是随着时间,我发现有Python主要有以下 ...
- arcgis python desc.dataType
desc = arcpy.Describe(r"C:\Users\dell\Documents\ArcGIS\ddd.shp") 是ShapeFile desc = arcpy.D ...
- HearthBuddy CSharpCodeProvider 如何编译cs文件
源码 源码1 namespace Hearthbuddy.Windows{ // Token: 0x02000041 RID: 65 public class MainWindow : W ...