题目描述:

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

解题思路:

开26个数组存入magzine字母的数目,每个位置存对应字母的数目;

对ransom字符串来说,每读一个字母,则将对应位置的字母数目减一,如果某个字母数目小于0了,则表明字母不够用,从而返回false;否则返回true。

代码如下:

public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] ran_array = new int[26];
for(int i = 0; i < magazine.length(); i++){
ran_array[magazine.charAt(i) - 'a']++;
} for(int i = 0; i < ransomNote.length(); i++){
if(--ran_array[ransomNote.charAt(i) - 'a'] < 0)
return false;
} return true; }
}

  

Java [Leetcode 383]Ransom Note的更多相关文章

  1. leetcode 383. Ransom Note

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

  2. 14. leetcode 383. Ransom Note

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

  3. LeetCode: 383 Ransom Note(easy)

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

  4. 383. Ransom Note【easy】

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

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

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

  6. leetcode修炼之路——383. Ransom Note

    题目是这样的 Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 a ...

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

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

  8. 383. Ransom Note

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

  9. [LeetCode] 383. Ransom Note_Easy tag: Hash Table

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

随机推荐

  1. LeetCode: Max Consecutive Ones

    这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...

  2. 一个linux命令(6/13):traceroute命令

    通过traceroute 我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不 ...

  3. Spark机器学习8· 文本处理(spark-shell)

    Spark机器学习 自然语言处理(NLP,Natural Language Processing) 提取特征 建模 机器学习 TF-IDF(词频 term frequency–逆向文件频率 inver ...

  4. QMap的使用

    1.定义 mapQMap<QString,QColor> map; 2.插入数据 map.insert("AA",RGB(255,0,0)); map.insert(& ...

  5. Git使用的常用命令

    一.git工作流程 Workspace工作区:是当前工作目录,可以在此目录编辑文件 Index缓存区:add指令,保存文件的改动 Repository仓库:commit指令,将多次的文件改动最后提交 ...

  6. React之JSX语法

    1. JSX的介绍   JSX(JavaScript XML)——一种在React组件内部构建标签的类XML语法.react在不使用JSX的情况下一样可以工作,然而使用JSX可以提高组件的可读性,因此 ...

  7. Routing in ASP.NET Web API

    Why is HttpGet required only for some actions? https://stackoverflow.com/questions/28068868/why-is-h ...

  8. UVA 10900 So you want to be a 2n-aire? (概率dp)

    题意:玩家初始的金额为1:给出n,表示有n道题目:t表示说答对一道题目的概率在t到1之间均匀分布. 每次面对一道题,可以选择结束游戏,获得当前奖金:或者回答下一道问题,答对的话奖金翻倍,答错的话结束游 ...

  9. scala学习手记17 - 容器和类型推断

    关于scala的类型推断前面已经提到过多次.再来看一下下面这个例子: import java.util._ var list1: List[Int] = new ArrayList[Int] var ...

  10. GBDT 总结文档

    在做阿里的o2o优惠券预测的时候学习了GBDT.听闻GBDT的威力,自然要学习学习. 接下来从以下几个方面记录下我对于GBDT的理解. GBDT的用途,优势 GBDT的结构和算法流程 GBDT如何训练 ...