这是小川的第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. nginx与php之间的交互方式

    1.  2种方式 TCP的socket  跟 UNIX的socket 2.TCP的socket  首先进入容器然后修改nginx下的配置文件 3. 修改/usr/local/nginx/conf/ng ...

  2. JS !function 稀奇古怪的写法

    !function(){alert("Execute after ()fun!")}(alert("Execute Firstly!")) 注意上面的执行顺序

  3. 智能指针weak_ptr记录

    智能指针weak_ptr为弱共享指针,实际上是share_ptr的辅助指针,不具备指针的功能.主要是为了协助 shared_ptr 工作,可用来观测资源的使用情况.weak_ptr 只对 shared ...

  4. [Luogu] 文艺平衡树(Splay)

    题面:https://www.luogu.org/problemnew/show/P3391 题解:https://www.zybuluo.com/wsndy-xx/note/1138892

  5. 5.13T1Send 题(send)

    Send 题(send) [题目描述] 某个国家有

  6. Python数据抓取(1) —数据处理前的准备

    (一)数据抓取概要 为什么要学会抓取网络数据? 对公司或对自己有价值的数据,80%都不在本地的数据库,它们都散落在广大的网络数据,这些数据通常都伴随着网页的形式呈现,这样的数据我们称为非结构化数据 如 ...

  7. Django-静态文件导入/url命名及反向解析

    一.静态文件导入 js.css.img等都叫做静态文件,那么关于django中静态文件的配置,我们就需要在settings配置文件里面写上这写内容: # STATIC_URL = '/xxx/' #别 ...

  8. Django-批量更新

    1.表结构 class Student(models.Model): """ 学生表(已报名) """ customer = models. ...

  9. ie8中如何使用base64

    由于ie8中不能使用jQuery2.0以上版本所以无法使用 window.btoa()加密 window.atob()解密 所以只能使用最原生的base64加密方法如下: /** * Created ...

  10. 最远 Manhattan 距离

    最远 Manhattan 距离 处理问题 K维空间下的n个点,求两点最远曼哈顿距离 思路 以二维为例介绍算法思想,即可类推到k维.对于P,Q两点,曼哈顿距离|Px-Qx|+|Py-Qy|可看作(±Px ...