题目是这样的

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. 架设wordpress再vps上的 一些感想总结

    日本vps.樱花系列 配置: 2cpu+1G内存+100G硬盘 系统 第一次我把默认的centos 给换了..原因就是,不会linux.而且我主要用.net  感觉 mono也行.但是linux不会. ...

  2. jQuery常用特效插件汇总

    jquery的CDN引用及下载地址 http://www.bootcdn.cn/jquery/   1:semantictabs.js可以简单地制作Tabs菜单2:tabBox.js可以非常简单方便地 ...

  3. 【java】利用异常机制,往前台写错误信息

    有时候,程序可能会报异常,而这些异常,通常需要提示前台操作人员怎么去处理,才能完成业务. 此时,我们只需要在业务层,自己抛出一个异常,自己捕捉之后,调用下类,即可输出到前台. 1.servlet里面可 ...

  4. 【Java】理解 UDDI 注册中心的 WSDL

    如何发布和查找 WSDL 服务描述 Web 服务描述语言(WSDL)有多种用法.特别是,根据应用程序的需要,WSDL 在 UDDI 注册中心有好几种使用方法.在这第 1 篇文章中(本系列共三篇),我们 ...

  5. Basic Wall Maze

    poj2935:http://poj.org/problem?id=2935 题意:在6*6的格子中,有一些,如果两个格子之间有墙的话,就不能直接相通,问最少要经过几步才能从起点走到终点.并且输出路径 ...

  6. SHell命令总结

    cat  files-to-copy.txt | xargs -i cp {} /tmp

  7. delphi编程里的bool跟boolean类型有什么区别

    bool是LongBool类型. Delphi中定义了四种布尔类型:Boolean,ByteBool,WordBool和LongBool.后面三种布尔类型是为了与其他语言兼容而引入的,一般情况下建议使 ...

  8. bzoj 1191 [HNOI2006]超级英雄Hero(最大基数匹配)

    1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2813  Solved: 1331[Submit][ ...

  9. tyvj P1431 [Tyvj Jan]分配任务(最大流)

    P1431 [Tyvj Jan]分配任务 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述      随着tyvj发展越来越大,管理员的任务越来越重,如何合理的 ...

  10. UVALive4513 Stammering Aliens(哈希法,后缀数组)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12580 [思路] 求出现次数不小于k次的最长可重叠子串和最后的出现 ...