LeetCode:Ransom Note_383
LeetCode:Ransom Note
【问题再现】
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
【优质算法】
public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] arr = new int[26];
for (int i = 0; i < magazine.length(); i++) {
arr[magazine.charAt(i) - 'a']++;
}
for (int i = 0; i < ransomNote.length(); i++) {
if(--arr[ransomNote.charAt(i)-'a'] < 0) {
return false;
}
}
return true;
}
}
【题后反思】
本题用到了统计字符出现频率的数组计数器,这种实现最为简单,不做解释。
我做这道题的时候考虑了magazine要按照Ransom的顺序,结果一直通不过,把问题想的复杂化了。
public static boolean canConstruct(String ransomNote, String magazine) {
int Sp = 0;
int Lp = 0;
int count = 0;
while (Lp < magazine.length()) {
if(Sp==ransomNote.length())
break;
if (ransomNote.charAt(Sp)==magazine.charAt(Lp)) {
count++;
System.out.print(ransomNote.charAt(Sp));
Sp++;
Lp++;
} else
Lp++;
}
if (count == ransomNote.length())
return true;
else
return false;
这种题目也可以利用HashMap来计算:
public static boolean canConstruct(String ransomNote, String magazine) {
HashMap<Character,Integer> myMap = new HashMap<>();
for(int i=0;i<magazine.length();i++)
{
if(myMap.containsKey(magazine.charAt(i)))
myMap.put(magazine.charAt(i),myMap.get(magazine.charAt(i))+1);
else
myMap.put(magazine.charAt(i),1);
}
for(int i=0;i<ransomNote.length();i++)
{
if(myMap.containsKey(ransomNote.charAt(i)))
{
myMap.put(ransomNote.charAt(i),myMap.get(ransomNote.charAt(i))-1);
if(myMap.get(ransomNote.charAt(i))<=0)
return false;
}
else
return false;
}
return true;
}
LeetCode:Ransom Note_383的更多相关文章
- [LeetCode] Ransom Note 赎金条
Given an arbitrary ransom note string and another string containing letters from all th ...
- LeetCode: Ransom Note
public class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] rans ...
- C#LeetCode刷题之#383-赎金信(Ransom Note)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- LeetCode之383. Ransom Note
-------------------------------------------- 思路就是进行频率统计. 统计一下第二个字符串字符出现次数++统计一下第一个字符串中字符出现次数--如果出现负数 ...
- leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- 【leetcode❤python】Ransom Note
#-*- coding: UTF-8 -*- class Solution(object): def canConstruct(self, ransomNote, magazine): ...
- 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, ...
随机推荐
- 对于git的认识
对于git的认识,我只想说,我不会把他的概念复制下来在博客上再发一遍,我对于他的了解是代源码的开放编写.对于git我会在今后去认真的理解他,不是所谓的抄袭.不会的东西我会尽力去学习.
- springMVC参数传递
web.xml文件 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=& ...
- Ajax的同步与异步
原文地址:http://www.cnblogs.com/Joetao/articles/3525007.html <%@ Page Language="C#" AutoEve ...
- 《理解 ES6》阅读整理:函数(Functions)(五)Name Property
名字属性(The name Property) 在JavaScript中识别函数是有挑战性的,因为你可以使用各种方式来定义一个函数.匿名函数表达式的流行使用导致函数调试困难,在栈信息中难以找出函数名. ...
- HTML5-表格
表格:表头<caption>,行<tr>,列<td>,标题<th> 属性:cellpadding="10" cellspacing= ...
- 用php创建mysql数据库
接触php就等于向后台更近了一步,之前一直在做前端,不过也在学php,但一直没敢写博客,现在终于有勇气迈向了这一步,还请各位博友多多担待. 服务器是后台开发的必备工具,但对于一般初学者来说是没有自己的 ...
- UWP开发随笔——UWP新控件!AutoSuggestBox!
摘要 要开发一款优秀的application,控件肯定是必不可少的,uwp就为开发者提供了各种各样的系统控件,AutoSuggestBox就是uwp极具特色的控件之一,也是相对于之前win8.1的ua ...
- Linux1:Linux概述
为什么服务器尤其大型服务器都使用Linux系统 服务器尤其是大型服务器一般都使用Linux系统,有以下几点原因: 1.成本低,Linux操作系统是免费的 2.安全性好,Linux采取了许多的安全措施, ...
- Java多线程11:ReentrantLock的使用和Condition
ReentrantLock ReentrantLock,一个可重入的互斥锁,它具有与使用synchronized方法和语句所访问的隐式监视器锁相同的一些基本行为和语义,但功能更强大. Reentran ...
- 规范化的软件项目演进管理--从 Github 使用说起
规范化的软件项目演进管理 从 Github 使用说起 1 前言 首先,本文的层次定位是:很基本很基础的 Github 工具的入门级应用,写给入门级的用户看的. 基本上工作过几年的人,下面描述的这些 ...