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

思路就是进行频率统计。

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

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. html 元素分类

    在讲解CSS布局之前,我们需要提前知道一些知识,在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的块状元素有: <div> ...

  2. idea 静态资源不能即时更新

  3. 【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1501 dp求出环状不连续的前缀和,剩下东西都可以算出来,比较繁琐. 时间 ...

  4. 一些js 插件的作用

    前言: 从一些开源网站上下载下来的 后台管理系统模板一般会有很多的js ,其js 的功能是什么呢?这里随手查询了一下,记录下来 正文: 1.zDialog.js 各种弹窗插件详细案例:http://w ...

  5. c# DES加密解密

    class DESHelper { string _iv = "9AUP"; string _key = "9d"; /// <summary> / ...

  6. Nginx简易配置文件(三)(文件缓存)

    server { listen 80; listen 443 ssl; server_name user.17.net userapi.17.net; access_log logs/user/acc ...

  7. BZOJ4650: [Noi2016]优秀的拆分

    考场上没秒的话多拿5分并不划算的样子. 思想其实很简单嘛. 要统计答案,求以每个位置开始和结束的AA串数量就好了.那么枚举AA中A的长度L,每L个字符设一个关键点,这样AA一定经过相邻的两个关键点.计 ...

  8. Jquery动态添加的元素绑定事件的3种方法

    假设我们点击li标签,弹出他的文本,如果是动态添加的li,点击是没有效果的,压根弹不出来文本. 下面博主分享一下为动态添加的元素绑定事件的三种方法,网上一般都是两种,我在这里多增加了一种. 事件案例: ...

  9. SDL第一个程序:加载一张图片

    直接看代码吧 using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat ...

  10. unity之初识shader

    自己做个总结先.当然文中很多内容都是从各位大神的文档当中看的.我只是站在巨人的肩膀上.       首先什么是shader?其实就是一个在显示屏当中的显示程序,俗称着色器.它可以定义物体在硬件显示屏当 ...