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. 在mac下svn冲突或其它什么原因无法更新svn副本或是必须要删除svn信息时,如何清除svn信息

    find . -type d -name ".svn"|xargs rm -rf 出处: http://blog.csdn.net/springsky_/article/detai ...

  2. 在自定义TableViewCell类里面添加按钮事件触发不了的一些实践

    我的自定义cell上面有5个控件,分别是一个背景的UIImageView,一个专辑的UIImageView(上面加了一个播放的button),一个专辑名字的UIImageView(上面加了显示标题的U ...

  3. [转]Git - 重写历史

    转自http://git-scm.com/book/zh/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E5%86%99%E5%8E%86%E5%8F%B2    重写历史 很多时 ...

  4. 使用gson在解析unicode时遇到的问题

    之前在用gson解析的时候未记录下来,所以今天做一个小的总结, 比如遇到像这种"\u003d"的unicode的字符,我们想解码这个字符,用gson可以这样表达 Gson gson ...

  5. zookeeper 简介

    一.简介 zookeeper是hadoop的一个子项目,A distribute coordination service for distributed applications 为了分布式应用而开 ...

  6. 微软Connect教程系列--自动生成增删改查页面工具介绍(二)

    本章课程描述了vs2015的三个特点,其中主要将描述在vs2015下面,使用命令自动生成增删改查界面,具体如下: 1.web.config文件不在存在,用config.json替代,以适应支撑vs的插 ...

  7. 年底了,特贡献一些C#有意思的算法题

    2013年,即将要过去了.屌丝C#程序员们拿到了年终奖不?是不是又想蠢蠢欲动了?是不是想通过跳槽来为自己实现加薪的梦想?好吧,跳槽之前还是做点准备吧,准备好C#的笔试吧.这里我收集了些奉献给大家,大家 ...

  8. Metrics-Java版的指标度量工具之一

    Metrics是一个给JAVA服务的各项指标提供度量工具的包,在JAVA代码中嵌入Metrics代码,可以方便的对业务代码的各个指标进行监控,同时,Metrics能够很好的跟Ganlia.Graphi ...

  9. dojo事件驱动编程之事件绑定

    什么是事件驱动? 事件驱动编程是以事件为第一驱动的编程模型,模块被动等待通知(notification),行为取决于外来的突发事件,是事件驱动的,符合事件驱动式编程(Event-Driven Prog ...

  10. 一个App完成入门篇(三)-完善主框架

    本节教程将继续带领大家完善教学demo 导入项目 完善主框架 完成viewShower子视图 打开新页 启动动画 将要学习的demo效果图如下所示 1. 如何导入完整项目 本节示例demo请参考下载地 ...