这是小川的第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. Spring Boot 跨域访问

    如何在 Spring Boot 中配置跨域访问呢? Spring Boot 提供了对 CORS 的支持,您可以实现WebMvcConfigurer 接口,重写addCorsMappings 方法来添加 ...

  2. uint128_t 添加 c++ 重载类型强制转换

    类型声明: class uint128 { public: uint128() :hi(), lo(){} uint128(uint32_t l) :hi(), lo(l){} uint128(int ...

  3. python中重要的概念:类(class)

    1.名词解释 类:类代表了具有相同特征的一类事物(人) 对象.实例:具体的某一个事物或者是人 实例化:将类变成对象的这么一个过程,即新建一个对象的过程,就是对类的一个实例化过程. 2.格式 函数定义: ...

  4. 有关 C# WebAPI知识

    1.[懒得安分博客总结的很全面] 2.关于基础类型作入参数的问题      参照此博客[ASP.NET WebAPI String 传值问题] 3.代码说明     using System; usi ...

  5. pat 甲级 1045 ( Favorite Color Stripe ) (动态规划 )

    1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She ...

  6. 51 Nod 1572 宝岛地图

    1572 宝岛地图  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 勇敢的水手们到达了一个小岛,在这个小岛上,曾 ...

  7. shell基础之二 bash特性详解

    https://blog.51cto.com/13520779/2093146 合格linux运维人员必会的30道shell编程面试题及讲解:https://blog.51cto.com/oldboy ...

  8. Leetcode题目79.单词搜索(回溯+DFS-中等)

    题目描述: 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许 ...

  9. 删除Linux系统多余的引导

    问题: 电脑中安装了多个Linux系统,需要删除不用的Linux系统的引导 解决方法: open terminal: su -     切换root用户 cd /boot/ ls ... grub  ...

  10. Java-JPDA 概述

    JPDA:Java 平台调试架构(Java Platform Debugger Architecture) 它是 Java 虚拟机为调试和监控虚拟机专门提供的一套接口. 一.JPDA https:// ...