leetcode修炼之路——383. Ransom Note
题目是这样的
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
题目分析:第二个字符串中,是否包含第一个字符串中出现的所有字符(重复的算出现的次数),第二个字符串中的每一个字符可以使用一次。
这个想起来挺简单:判断第一个字符串中每一个字符出现的次数与第二个字符串中每一个字符串出现的次数进行比较。
借鉴他人思路(只考虑小写的英文字母):
public static boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) {
return false;
}
int[] a = new int[26];// 最多有26个字母
int[] b = new int[26];
for (int i = 0; i < ransomNote.length(); i++) {
a[ransomNote.charAt(i) - 'a']++;// 进行字符个数的判断,如果已经存在了,就++(这里最重要)
}
for (int i = 0; i < magazine.length(); i++) {
b[magazine.charAt(i) - 'a']++;
}
for (int i = 0; i < a.length; i++) {
if (a[i] > b[i]) {// 判断第一个数组中的每一个是否有大于第二个数组的值
return false;
}
}
return true;
}
代码很简单,关键是思路!思路!思路!
重要的事说三遍哈哈^_^
leetcode修炼之路——383. Ransom Note的更多相关文章
- 383. Ransom Note【easy】
383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- 14. leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- [LeetCode&Python] Problem 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- Java [Leetcode 383]Ransom Note
题目描述: Given an arbitrary ransom note string and another string containing letters from al ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
- 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- 383. Ransom Note 在字典数组中查找笔记数组
[抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...
随机推荐
- [BZOJ 2326] [HNOI2011] 数学作业 【矩阵乘法】
题目链接:BZOJ - 2326 题目分析 数据范围达到了 10^18 ,显然需要矩阵乘法了! 可以发现,向数字尾部添加一个数字 x 的过程就是 Num = Num * 10^k + x .其中 k ...
- Qt 文件处理(readLine可以读取char[],并且有qSetFieldWidth qSetPadChar 等全局函数)
Qt 文件处理 Qt提供了QFile类来进行文件处理,为了更方便地处理文本文件或二进制文件,Qt还提了QTextStream类和QDataStream类,处理临时文件可以使用QTemporaryFil ...
- Solution for "De-serialization exception: Unable to find assembly xxxxx"
public void DeSerialize() { BinaryFormatter formatter = new BinaryFormatter(); AppDomain.CurrentDoma ...
- 根据body的内容 查找h2标签的@class="subtitle"的值
<pre name="code" class="html"><body class="api jquery listing" ...
- 【HDOJ】2289 Cup
二分.另外,圆台体积为v = PI*(r*r+r*R+R*R)*H/3.注意精度. #include <cstdio> #include <cmath> #define exp ...
- Linux on Power 上的调试工具和技术
Linux on Power 上的调试工具和技术 简介: 调试是一项主要的软件开发活动,作为应用程序开发人员,您无法避免对程序进行调试.有效的调试不仅能缩短软件开发周期,而且可以节省成本.本文简要介 ...
- 【解决办法】糟糕,我的电脑只有IE64位浏览器能上网,其他软件都上不了网
最近两周在三班四班有5位同学电脑7次出现网络故障,表现为能连上锐捷.DNS正常却不能上网,其中在我自己的计算机上就发生了2次.上网搜集并整理了以下资料,供大家参考.请直接参见[解决办法]. [网上 ...
- Delphi NativeXml读取中文乱码问题解决
NativeXml默认的字符类型为Utf8String,有时在读取中文时还是会出现乱码问题,在329版本中提供一种类型转换函数sdUtf8ToWide(),我们可以这样sdUtf8ToWide(AXm ...
- android下隐藏标题栏
最近在开发android,遇到一个问题标题栏影响美观,查了很多网上方法. 用到了方法一: requestWindowFeature(Window.FEATURE_NO_TITLE); 虽然一直强调要在 ...
- 【java基础】--(3)javaIO详细阐释
1.总的4类 字符:Reader 和Writer 字节:InputStream和OutputStream 2.Reader 六个子类BufferedReader, CharArrayReader, F ...