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

思路就是进行频率统计。

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

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. 面向对象和面向过程的js版选项卡

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  2. 教你一招:win 7 或win 10右键菜单 添加获取管理员权限

    当我们从经典的 windows XP 系统升级到 win 7 或 win 10,我们会发现,想要删除一些文件时,总是提示被占用或者是没有权限,很是烦恼. 这里,写下解决这个烦恼的办法,以安慰心里的不平 ...

  3. ED2k Resource

    http://www.lwkk.com/ http://www.ed2000.com/

  4. New Features In SNMPv3 - SNMP Tutorial

    30.12 New Features In SNMPv3 We said that version 3 of SNMP represents an evolution that follows and ...

  5. AXIS 调用 webservice服务时传递 服务器验证需要的用户名密码

    System.setProperty("javax.net.ssl.trustStore", T.class.getResource(".").getPath( ...

  6. js中join和split的用法

  7. js自动闭合html标签,自动补全html标记

    假如我有一个DIV,如果没有闭合后面的样式都会乱了,这样的代码可能会影响后面的样式,我希望用js去自动闭合这种没有闭合的标签: 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  8. Android 基础篇(二)

    ADB进程 adb指令 adb install xxx.apk adb uninstall 包名 adb devices adb start-server adb kill-server adb sh ...

  9. [Data Structure] Bit-map空间压缩和快速排序去重

    Bit-map是一种很巧妙的数据存储结构.所谓的Bit-map就是用一个bit位来标记某个元素对应的Value,而Key即是该元素.由于采用了Bit为单位来存储数据,可以大大节省存储空间.Bit-ma ...

  10. UVA2636

    理解;类似我们离散的命题  因为只有一个是坏的 超过一个人说你坏  你一定就是坏的  有人说你对 你就对了 分为两种情况 1.说你对的是好的  他的判断是正确的 2.说你对的人 是坏的 他的判断是错误 ...