题目是这样的

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的更多相关文章

  1. 383. Ransom Note【easy】

    383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...

  2. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  3. leetcode 383. Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  4. 14. leetcode 383. Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  5. [LeetCode&Python] Problem 383. Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  6. Java [Leetcode 383]Ransom Note

    题目描述: Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 al ...

  7. LeetCode: 383 Ransom Note(easy)

    题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...

  8. 383. Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  9. 383. Ransom Note 在字典数组中查找笔记数组

    [抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...

随机推荐

  1. zabbix 参数说明

    <pre name="code" class="html">数据采集的工作模式可以分为被动模式(服务器端到客户端采集数据) 主动模式(客户端主动上报 ...

  2. C# 精准获取代码运行时间

    纯粹转载,转载请注明参考链接及作者! 参考链接:http://www.cnblogs.com/ret00100/archive/2010/08/06/1793680.html,作者:博客园 大佬辉   ...

  3. joelonsoftware 读书摘录

    joelonsoftware 读书摘录   <五个为什么>  1.“黑天鹅难题”,代表外来因素,是一个超出正常预料之外的事件.  2.丰田佐吉的“五个为什么”,当某个地方出现问题时,你就一 ...

  4. Andriod视频http://pan.baidu.com/share/link?shareid=7300&uk=3339495714

    老罗Android开发 视频教程           一.Android入门介绍 视频教程     1.1 android系统介绍   1.3 如何搭建android开发环境   1.5 androi ...

  5. Jenkins 二:邮件配置

    默认邮件的配置 假设管理员邮箱是 user1@domain1.com,密码是pw1. 1. 打开“系统管理”-> “系统设置”. 2. 找到“Jenkins Location”-> “系统 ...

  6. 来自投资银行的20个Java面试题

    问题一:在多线程环境中使用HashMap会有什么问题?在什么情况下使用get()方法会产生无限循环? HashMap本身没有什么问题,有没有问题取决于你是如何使用它的.比如,你在一个线程里初始化了一个 ...

  7. [置顶] 步步辨析JS中的对象成员

    前提 首先我们应该明白创建一个JS对象的具体实例是实例化的过程,而实例化是通过new关键字实现的,这个对象是含有constructor的,一般的核心对象都会具有constructor以便创建其实例.因 ...

  8. ckfinder 1

    网上的破解教程对于2.4版本来说已经过时了. 以下是CKFinder 2.4 ASP.NET的亲测可用破解方法,经测试,只需修改两处代码. 打开ckfinder.js, 步骤1. 搜索替换如下代码: ...

  9. Delphi 2007体验!

    Delphi 2007体验! baidu 内容摘要:CodeGear(From Borland) 公司公布了最新的Delphi 2007 For Win32版本号.作为一个 Delphi 的使用者,第 ...

  10. [ES6] Array.find()

    Convenient method to find one item in an array, avoid writing and  for + if: let arys = [1,,5,,6] ; ...