题目是这样的

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. 【深入浅出jQuery】源码浅析2--使用技巧

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  2. linux 发布 qt(更新ld命令的路径依赖)

    PATH 错误解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such ...

  3. 我的VSTO之路(三):Word基本知识

    原文:我的VSTO之路(三):Word基本知识 在前一篇文章中,我初步介绍了如何如何开发一个VSTO程序,在本文中,我将进一步深入介绍Word的插件开发.Word是一个大家在日常工作中一直接触的文档工 ...

  4. JPA学习笔记

    一.JPA基础1.1 JPA基础JPA: java persistence api 支持XML.JDK5.0注解俩种元数据的形式,是SUN公司引入的JPA ORM规范 元数据:对象和表之间的映射关系 ...

  5. Redis Sentinel实现Failover

    redis版本:2.8.17   服务器规划: 10.50.13.34(6379 master) 10.50.13.35(6379 slave) 10.50.13.36(6379 slave) 10. ...

  6. POJ 1651 Multiplication PuzzleDP方法:

    有N张写有数字的卡片排成一行,按一定次序从中拿走N-2张(第1张和最后一张不能拿),每次只拿一张,取走一张卡片的同时,会得到一个分数,分值的计算方法是:要拿的卡片,和它左右两边的卡片,这三张卡片上数字 ...

  7. sharepoint2007的中文版

    今天终于下载到了sharepoint2007的中文版了,以前微软的测试板也没有中文版,今天终于下载到了,经过测试,用英文版的sn也是可以用的.微软提供的测试版,输入正式的key,就可以变成正式版了.我 ...

  8. Android按钮式进度条

    package com.example.progress.demo; import android.annotation.SuppressLint; import android.content.Co ...

  9. poj 1269 水题

    题目链接:http://poj.org/problem?id=1269 #include<cstdio> #include<cstring> #include<cmath ...

  10. Http报文 状态码

    一.HTTP报文 1.HTTP报文介绍 HTTP报文:用于HTTP协议交互的信息. HTTP报文由报文头部和报文主体(非必须)构成,中间由空行来分隔. 1.1 请求报文:客户端发起的报文. 1).报文 ...