LeetCode:Ransom Note

【问题再现】

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

【题后反思】

  本题用到了统计字符出现频率的数组计数器,这种实现最为简单,不做解释。

  我做这道题的时候考虑了magazine要按照Ransom的顺序,结果一直通不过,把问题想的复杂化了。

    public static boolean canConstruct(String ransomNote, String magazine) {
int Sp = 0;
int Lp = 0;
int count = 0; while (Lp < magazine.length()) {
if(Sp==ransomNote.length())
break;
if (ransomNote.charAt(Sp)==magazine.charAt(Lp)) {
count++;
System.out.print(ransomNote.charAt(Sp));
Sp++;
Lp++;
} else
Lp++;
}
if (count == ransomNote.length())
return true;
else
return false;

  这种题目也可以利用HashMap来计算:

  

public static boolean canConstruct(String ransomNote, String magazine) {
HashMap<Character,Integer> myMap = new HashMap<>();
for(int i=0;i<magazine.length();i++)
{
if(myMap.containsKey(magazine.charAt(i)))
myMap.put(magazine.charAt(i),myMap.get(magazine.charAt(i))+1);
else
myMap.put(magazine.charAt(i),1);
} for(int i=0;i<ransomNote.length();i++)
{
if(myMap.containsKey(ransomNote.charAt(i)))
{
myMap.put(ransomNote.charAt(i),myMap.get(ransomNote.charAt(i))-1);
if(myMap.get(ransomNote.charAt(i))<=0)
return false;
}
else
return false;
}
return true;
}

LeetCode:Ransom Note_383的更多相关文章

  1. [LeetCode] Ransom Note 赎金条

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

  2. LeetCode: Ransom Note

    public class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] rans ...

  3. C#LeetCode刷题之#383-赎金信(Ransom Note)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...

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

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

  5. LeetCode之383. Ransom Note

    -------------------------------------------- 思路就是进行频率统计. 统计一下第二个字符串字符出现次数++统计一下第一个字符串中字符出现次数--如果出现负数 ...

  6. leetcode 383. Ransom Note

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

  7. 【leetcode❤python】Ransom Note

    #-*- coding: UTF-8 -*- class Solution(object):       def canConstruct(self, ransomNote, magazine):   ...

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

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

  9. 14. leetcode 383. Ransom Note

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

随机推荐

  1. .Net下的 ORM框架介紹

    在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...

  2. MYSQL删除表的记录后如何使ID从1开始

    MYSQL删除表的记录后如何使ID从1开始 MYSQL删除表的记录后如何使ID从1开始 http://hi.baidu.com/289766516/blog/item/a3f85500556e2c09 ...

  3. spring 3.1 配置 JCR 303 Bean Validation

    A) 导入Hibernate-Validator  要使用JSR303 校验框架, 需要加入框架的具体实现Hibernate-Validator, 在soureforge上下载最新的Hibernate ...

  4. iBatis简单介绍

    1.       Ibatis是开源软件组织Apache推出的一种轻量级的对象关系映射(ORM)框架,和Hibernate.Toplink等在java编程的对象持久化方面深受开发人员欢迎. 对象关系映 ...

  5. vs2010 和vs2012的区别 副标题--Loaded事件走两次

    我上一遍博文没有通过首页显示!这边就简短的描述一下问题,希望大拿们有遇到类似问题或者知道原因的回答一下下!!! 最终的问题是Loaded事件走两次,具体可以看我上一篇对问题的描述. 在目标框架同样都是 ...

  6. Android(蓝牙)

    因近期项目需求调试了Android蓝牙通讯接口,主要是两个终端作为服务端和客户端的通信,本文将部分重要知识点记录如下. 蓝牙是短距离无线通信,通常分经典蓝牙和低功耗蓝牙(即蓝牙4.0),两类蓝牙协议各 ...

  7. WebAdaptor Object reference not set to an instance of an object.

    C:\inetpub\wwwroot\arcgis目录下webAdaptor.config文件内容被清空,从别的地方拷贝一份即可. <?xml version="1.0" e ...

  8. Javascript函数节流

    最近在做网页的时候有个需求,就是浏览器窗口改变的时候需要改一些页面元素大小,于是乎很自然的想到了window的resize事件,于是乎我是这么写的 <!DOCTYPE html> < ...

  9. java程序 启动时参数

      iEMP34:/opt/version/lktest/b030/jre/jre_linux/bin # ./java -classpath . SysInfo Exception in threa ...

  10. flex 阅读器

    遇到很多的困难 首先是 pdf2swf 而后又下载swftools 而后有swfcombine.exe 让制作的swf 文字可选? —— 这应该是很常见的需求啊! 可是我搜索来搜索去都找不到... 搜 ...