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 ...
随机推荐
- PHP7中php.ini、php-fpm和www.conf的配置
引自:https://typecodes.com/web/php7configure.html 1 配置php.ini php.ini是php运行核心配置文件: ######避免PHP信息暴露在htt ...
- 顺序表java实现
public class SeqList { Object[] data; int maxSize; int length; public SeqList(int maxSize) { this.ma ...
- 【OpenJudge 8463】Stupid cat & Doge
http://noi.openjudge.cn/ch0204/8463/ 挺恶心的一道简单分治. 一开始准备非递归. 大if判断,后来发现代码量过长,决定大打表判断后继情况,后来发现序号不对称. 最后 ...
- Python笔记(1)变量与表达式
列表list list是用的最多的类型 可以count计数 可嵌套,多钟类型并存 支持 + * a = [1,2,3] a_ref = a a_copy = a[:] 引用,a变化a_ref也变化 指 ...
- AngularJS 的嵌套路由 UI-Router
AngularJS 的嵌套路由 UI-Router 本篇文章翻译自:https://scotch.io/tutorials/angular-routing-using-ui-router 演示网站请查 ...
- 工作中的一些JS--为网页动态添加元素,类似于邮箱添加联系人的功能
项目中要解决一个为下拉框动态添加选项的问题,之前从网上搜到结果,写个JS函数 //先新建元素,并添加属性 var option = document.createElement("optio ...
- linux用命令删除重复行
文本处理时,经常要删除重复行,下面是三种方法 第一,用sort+uniq,注意,单纯uniq是不行的. sort -n test.txt | uniq 第二,用sort+awk命令,注意,单纯awk同 ...
- 精选9个值得学习的 HTML5 效果【附源码】
这里精选了一组很酷的 HTML5 效果.HTML5 是现 Web 开发领域的热点, 拥有很多让人期待已久的新特性,特别是在移动端,Web 开发人员可以借助 HTML5 强大功能轻松制作各种交互性强.效 ...
- Android之仿微信Tab滑动
这个项目实现了以下的功能:有三个标签聊天.发现和通讯录,左右滑动下面的ViewPager可以切换不同的标签,且标签下面的蓝色条可以随着手指的滑动来实时滑动.另外,如果第二次滑动到“聊天”界面,可以在“ ...
- MVVM开发模式简单实例MVVM Demo
本文主要是翻译Rachel Lim的一篇有关MVVM模式介绍的博文 A Simple MVVM Example 并具体给出了一个简单的Demo(原文是以WPF开发的,对于我自己添加或修改的一部分会用红 ...