题目描述:

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. python入门四:异常

    一.异常 异常就是在触发异常条件时(解释器或程序员)而采取相应的措施 c++中异常使用try, throw, catch等关键字,而python中使用try, raise, except等 二.标准异 ...

  2. Django---自定义admin组件思维导图

  3. JavaEE之动态代理

    jdk动态代理主要使用的是java反射机制(既java.lang.reflect包) 动态代理:程序运行时,使用JDK提供工具类(Proxy),动态创建一个类,此类一般用于代理. 代理类需要实现Inv ...

  4. Spring Boot 快速入门(Eclipse)

    步骤一:关于版本(前期工作) JDK 1.8 maven 3.5 配置环境变量: 步骤二:创建项目 首先新建个maven项目(SpringBoot 应用,本质上是一个Java 程序,其采用的风格是 m ...

  5. linux 进阶命令___0002

    #列出重复文件,首先检查文件大小,再检查md5sum find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | ...

  6. Google maps API

    https://developers.google.com/kml/documentation/kml_tuthttps://developers.google.com/maps/documentat ...

  7. 【Boost】boost库asio详解5——resolver与endpoint使用说明

    tcp::resolver一般和tcp::resolver::query结合用,通过query这个词顾名思义就知道它是用来查询socket的相应信息,一般而言我们关心socket的东东有address ...

  8. IOS-环信

    一.即时通讯 即时通讯,又称实时通讯 即时通信(Instant Messaging,简称IM)是一个实时通信系统,允许两人或多人使用网络实时的传递文字消息.文件.语音与视频交流 即时通讯在开发中使用的 ...

  9. Python中的import,from...import以及模块、包、库的概念

    首先,说明一下,我使用的是python3.6.3win32版本,使用的IDE是pycharm2017社区免费版. 刚开始接触python编程不久,有很多概念都不是特别清楚,但是我觉得既然选择,尽自己最 ...

  10. mysql-in关键字,分组查询,分页查询

    1. in关键字,组查询 # 使用or来查询的化,不方便而且参数一多比较傻 select * from users where id=1 or id=2 or id=4; select * from ...