383. Ransom Note【easy】
383. Ransom Note【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
解法一:
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
map<char, int> m_rans;
for (int i = ; i < ransomNote.length(); ++i) {
++m_rans[ransomNote[i]];
}
for (int i = ; i < magazine.length(); ++i) {
if (m_rans.find(magazine[i]) != m_rans.end()) {
--m_rans[magazine[i]];
}
}
for (map<char, int>::iterator it = m_rans.begin(); it != m_rans.end(); ++it) {
if (it->second > ) {
return false;
}
}
return true;
}
};
解法二:
public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] arr = new int[];
for (int i = ; i < magazine.length(); i++) {
arr[magazine.charAt(i) - 'a']++;
}
for (int i = ; i < ransomNote.length(); i++) {
if(--arr[ransomNote.charAt(i)-'a'] < ) {
return false;
}
}
return true;
}
}
参考@yidongwang 的代码。
解法三:
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char, int> map();
for (int i = ; i < magazine.size(); ++i)
++map[magazine[i]];
for (int j = ; j < ransomNote.size(); ++j)
if (--map[ransomNote[j]] < )
return false;
return true;
}
};
参考@haruhiku 的代码
解法四:
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int> vec(, );
for (int i = ; i < magazine.size(); ++i)
++vec[magazine[i] - 'a'];
for (int j = ; j < ransomNote.size(); ++j)
if (--vec[ransomNote[j] - 'a'] < )
return false;
return true;
}
};
参考@haruhiku 的代码
383. Ransom Note【easy】的更多相关文章
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
随机推荐
- [BZOJ3684]大朋友和多叉树
设答案为$f_s$,它的生成函数为$\begin{align*}F(x)=\sum\limits_{i=0}^\infty f_ix^i\end{align*}$,则我们有$\begin{align* ...
- java 面向接口编程的理解
初学者可能在学习中会有很多疑惑,为什么要这样,明明可以那样实现,这样做的好处又是什么? 可能会的人觉得很简单很容易理解,甚至可能觉得问的问题很智障,但对于小白来说可能是苦思冥想都不得其解的. 自己身为 ...
- Dom4jDemo应用-保存手机信息
---恢复内容开始--- import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStr ...
- Jenkins持续集成实战总结
原文:https://my.oschina.net/CandyDesire/blog/341331#comment-list 持续集成 什么是持续集成 随着软件开发复杂度的不断提高,团队开发成员间如何 ...
- Android ProgressBar手动控制开始和停止
这两天有个需求,点击按钮从SD卡解压压缩包,并读取压缩包内txt文档内容,然后在街面上显示出来.毕竟IO操作很耗时,如果文件较大会花费不少时间.所以,在处理数据的时候能给个进度就好了.我们通常的做法就 ...
- Vue生命周期各阶段发生的事情
首先,参考之前一篇vue生命周期的总结:Vue生命周期总结 接下来我们来分析下官方文档经典流程图,每个阶段到底发生了什么事情. 1.在beforeCreate和created钩子函数之间的生命周期 在 ...
- ClassNotFoundException和 NoClassDefFoundError区别验证
首先NoClassDefFoundError是一个错误,而ClassNotFoundException是一个异常 NoClassDefFoundError产生的原因: 如果JVM或者Classload ...
- Django——META内部类选项
Django 模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.以下对此作一总结: abstract 这个属性是定义当前的模型类是不是一个抽象类.所谓抽象类是不会对应 ...
- php开发第一课
开发环境:wampserver IDE工具:sublime2 问题记录: 1.如何避免在php中中文乱码? 在php头部加入:<meta charset="utf-8"> ...
- REDIS 内存满时删除策略
REDIS 内存满时删除策略