--------------------------------------------

思路就是进行频率统计。

统计一下第二个字符串字符出现次数++
统计一下第一个字符串中字符出现次数--
如果出现负数说明第二个中的字符不够用的。

AC代码如下:

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

改进的AC代码(节省了一个for循环):

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

或者:

统计一下第一个的字符出现次数++;
统计一下第二个字符出现次数--
如果出现正数说明第一个没减完呗

AC代码:

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

题目来源: https://leetcode.com/problems/ransom-note/

LeetCode之383. Ransom Note的更多相关文章

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

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

  2. 383. Ransom Note【easy】

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

  3. leetcode 383. Ransom Note

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

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

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

  5. 14. leetcode 383. Ransom Note

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

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

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

  7. Java [Leetcode 383]Ransom Note

    题目描述: Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 al ...

  8. LeetCode: 383 Ransom Note(easy)

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

  9. 383. Ransom Note

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

随机推荐

  1. jquery 将disabled的元素置为enabled的三种方法

    转--http://www.jb51.net/article/19342.htm 在jquery中可以通过jqueryObj.attr("disabled","disab ...

  2. 使用自定义tld标签简化jsp的繁琐操作

    最近做一个树形结构的展示,请求目标页面后,后台只返回简单的List,虽然有想过在jsp页面内做一些操作简化,但是太繁琐了,其他的标签又不能满足需求,所以只能自己做一个.使用tld标签可以简化jsp代码 ...

  3. [日常训练]yayamao的神题

    Description $yayamao$是数学神犇,一天他在纸上计算起了$1/P$, 我们知道按照模拟除法可以得到准确解,例如$1/7=0.(142857),1/10=0.1(0)$.$yayama ...

  4. MapReduce实现手机上网流量分析(业务逻辑)

    一.问题背景 现在的移动刚一通话就可以在网站上看自己的通话记录,以前是本月只能看上一个月.不过流量仍然是只能看上一月的. 目的就是找到用户在一段时间内的上网流量. 本文并没有对时间分组.下一节进行分区 ...

  5. gulp删除文件和文件夹

    使用模块:del,npm install --save-dev gulp del var gulp = require('gulp'); var del = require('del'); gulp. ...

  6. <<< PermGen space溢出解决方法

    错误信息中的PermGen space的全称是Permanent Generation space,是指内存的永久保存区域.还没有弄明白PermGen space是属于非堆内存,还是就是非堆内存,但至 ...

  7. centos 软件包安装与管理

    http://rpmfind.net/linux/RPM/index.html http://pkgs.org/ https://sourceforge.net/

  8. Android ViewPager sharedpreferences

    http://www.cnblogs.com/dwinter/archive/2012/02/27/AndroidViewPager%E5%A4%9A%E9%A1%B5%E9%9D%A2%E6%BB% ...

  9. Bash 中为 _ 变量赋空值的三个场景

    $_ 有好几个功能,我们最常用的是用它来获取“刚刚执行过的命令的最后一个参数”这个功能,比如下面这样: $ ls ~/Downloads/very/long/dir/  # ls 到某个目录看看有没有 ...

  10. HTML5+CSS3 - 代码简写篇

    有话先说:我是一只菜鸟,还是一只刚步入前端这个领域的小菜年,在不断的进阶,理解最深刻的还是代码,既爱又恨却不知如何去感悟. background属性简写: background-position属性组 ...