【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/ransom-note/
- Difficulty: Easy
题目描述
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
题目大意
判断ransom能否由magazines的字符构成。
解题方法
Java解法
理解题意很关键,这个是说从magazine中取出几个元素排列组合能够摆成ransomNote。
参考Find the Difference的题目,做个有26个位置的数组,保存字符出现的次数,最后统计一下即可。
其中一个字符串的元素使位置元素++,另外个字符串使字符串–;
public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if(ransomNote.length() > magazine.length())
return false;
int []chars= new int[26];
for(int i=0; i< magazine.length(); i++){
chars[magazine.charAt(i)- 'a']++;
}
for(int i=0; i< ransomNote.length(); i++){
chars[ransomNote.charAt(i)- 'a']--;
if(chars[ransomNote.charAt(i)- 'a'] < 0){
return false;
}
}
return true;
}
}
AC:18 ms
Python解法
直接Counter,然后判断前者的每个字符出现次数都小于后者即可。
class Solution:
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
rcount = collections.Counter(ransomNote)
mcount = collections.Counter(magazine)
for r, c in rcount.items():
if c > mcount[r]:
return False
return True
日期
2017 年 1 月 7 日
2018 年 11 月 14 日 —— 很严重的雾霾
【LeetCode】383. Ransom Note 解题报告(Java & Python)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- Java [Leetcode 383]Ransom Note
题目描述: Given an arbitrary ransom note string and another string containing letters from al ...
- leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- 14. leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
随机推荐
- 【机器学习与R语言】8- 神经网络
目录 1.理解神经网络 1)基本概念 2)激活函数 3)网络拓扑 4)训练算法 2.神经网络应用示例 1)收集数据 2)探索和准备数据 3)训练数据 4)评估模型 5)提高性能 1.理解神经网络 1) ...
- 【Meta】16s rRNA和16s rDNA的区别
在文章或宣传稿中经常看到两者滥用,实际上是不同的. 首先是各个字母的含义: 16S中的"S"是一个沉降系数,亦即反映生物大分子在离心场中向下沉降速度的一个指标,值越高,说明分子越大 ...
- 【ThermoRawFileParser】质谱raw格式转换mgf
众所周知,Proteowizard MSconvert用于质谱原始数据的格式转换,但主要平台是windows,要想在Linux上运行需要打Docker或Wine,对于普通用户来说还是很困难的,想想质谱 ...
- nodeJs-Stream接口
JavaScript 标准参考教程(alpha) 草稿二:Node.js Stream接口 GitHub TOP Stream接口 来自<JavaScript 标准参考教程(alpha)> ...
- mango后台
环境搭建 项目配置 下载后导入项目,删除mvnw.mvnw.cmd两个文件 修改spring-boot-starter-web pom.xml --> run as --> mave i ...
- 【Python】【Module】random
mport random print random.random() print random.randint(1,2) print random.randrange(1,10) 随机数 import ...
- js调用高德地图API获取地理信息进行定位
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=(需要自 ...
- eclipse.ini配置 vmargs 说明
-vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M 1. 各个参数的含义什么? 参数中-vmargs的意思是设置JVM参数, ...
- java多线程并发编程中的锁
synchronized: https://www.cnblogs.com/dolphin0520/p/3923737.html Lock:https://www.cnblogs.com/dolphi ...
- 【C#】【假条生成系统】【单位剖析】如何判断在文本框输入了几个人名?
我们规定,人名和人名之间使用顿号隔开 那么, 1个人,就是0个顿号 2个人,就是1个顿号 3个人,就是2个顿号 -- 所以我们可以判断文本框中顿号的出现次数. 出现0次,则为1人,出1次,则为两人. ...