LeetCode之383. Ransom Note

--------------------------------------------
思路就是进行频率统计。
统计一下第二个字符串字符出现次数++
统计一下第一个字符串中字符出现次数--
如果出现负数说明第二个中的字符不够用的。
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的更多相关文章
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 383. Ransom Note【easy】
383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...
- leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- leetcode修炼之路——383. Ransom Note
题目是这样的 Given an arbitrary ransom note string and another string containing letters from a ...
- 14. leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- [LeetCode&Python] Problem 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- Java [Leetcode 383]Ransom Note
题目描述: Given an arbitrary ransom note string and another string containing letters from al ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
- 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
随机推荐
- HTTP Status
Web服务器响应浏览器或其他客户程序的请求时,其应答一般由以下几个部分组成:一个状态行,几个应答 头,一个空行,内容文档.下面是一个最简单的应答 : 状态行包含HTTP版本.状态代码.与状态代码对应的 ...
- AOPR软件最小化消失了
结合日常使用软件的经历,我们都是选择最小化按钮后,点击状态栏中最小化图标即可恢复软件窗口.在使用Advanced Office Password Recovery的过程中,有时会出现点击最小化按钮后在 ...
- [CG编程] 基本光照模型的实现与拓展以及常见光照模型解析
0.前言 这篇文章写于去年的暑假.大二的假期时间多,小组便开发一个手机游戏的项目,开发过程中忙里偷闲地了解了Unity的shader编写,而CG又与shaderLab相似,所以又阅读了<CG教程 ...
- python基础回顾1
定义 tuple(元组), list (表) #!/usr/bin/env python # encoding: utf-8 a = 10 #定义一直变量,无需声明 s1 = (2,1.3,'love ...
- gulp-rev同时将js和css文件写在一个rev-manifest.json文件里面的方式探讨
参考: https://segmentfault.com/q/1010000002876613 https://github.com/sindresorhus/gulp-rev 测试发现,在官网上最主 ...
- Promise deeply lookup
Motivation Consider the following synchronous JavaScript function to read a file and parse it as JSO ...
- XInitThreads与XLIB
XInitThreads函数通常需要尽早调用,一般要在XLIB的其他函数前调用 否则XLIB的函数可能会在调用时直接崩溃(多线程程序中) 最好的做法是,在main入口即调用XInitThreads函数
- WordPress数据库优化技巧
各位站长都知道wordpress用久了就会越来越慢.今天就给大家介绍如何给自己的wordpress提速,分两种方法:1.插件属性wordpress的都知道其插件是相当的多,只要你能想得到的基本都有,在 ...
- 数据存储_ SQLite(3)
SQLite的应用 一.简单说明 1.在iOS中使用SQLite3,首先要添加库文件 libsqlite3.dylib 2.导入主头文件 #import <sqlite3.h> 二.具体说 ...
- JavaScript对象的理解 及 字面量对象与数组的关系
JavaScript的简单类型包括 数字.字符串.布尔值.null值.undefined值. 其他的值都是对象.对象是可变的键控集合.数组.函数.正则表达式都是对象. 对象是属性的容器,属性都是名字和 ...