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 ...
随机推荐
- [Atcoder SHPC2018] Tutorial
Link: SHPC2018 传送门 C: 一道看上去有些吓人的题目,不过$1e9$规模下的$n^m$代表肯定是可以约分的 可以发现能提供贡献的数对只有$2*(n-d)$种,那么总贡献为$2*(n-d ...
- [CF235E]Number Challenge
$\newcommand{fl}[1]{\left\lfloor#1\right\rfloor}$题意:求$\sum\limits_{i=1}^a\sum\limits_{j=1}^b\sum\lim ...
- [Contest20180122]超级绵羊异或
题意:求$a\ xor\left(a+b\right)xor\cdots xor\left(a+b\left(n-1\right)\right)$ 对每一位求答案,第$k$的答案是$\sum\limi ...
- 【计算几何】【极角序】【前缀和】bzoj1132 [POI2008]Tro
把点按纵坐标排序,依次枚举,把它作为原点,然后把之后的点极角排序,把叉积的公式稍微化简一下,处理个后缀和统计答案. #include<cstdio> #include<cmath&g ...
- Java学习笔记(6)
java是面向对象的语言. 对象:真实存在的唯一的事物. 类:实际就是对某种类型事物的共性属性与行为的抽取 面向对象的计算机语言的核心思想:找适合的对象做适合的事情. 如何找适合的对象呢: 1.sun ...
- 专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路(HipHop,HHVM)
专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路 http://www.infoq.com/cn/articles/interview-alibaba-zhaohaiping
- css3动画和JS+DOM动画和JS+canvas动画比较
css3兼容:IE10+.FF.oprea(animation):safari.chrome(-webkit-animation) js+dom:没有兼容问题: js+canvas:IE9+:(性能最 ...
- PostgreSQL配置文件--连接和认证
1 连接和认证 CONNECTIONS AND AUTHENTICATION 1.1 连接 CONNECTIONS 1.1.1 listen_addresses 字符型 默认: listen_addr ...
- touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
问题:在从 https://c.163.com/hub#/m/repository/?repoId=3093 下载镜像 docker pull hub.c.163.com/library/jenkin ...
- [原创]用逻辑嗅探破解接触式IC卡口令
最近两周对接触型IC卡很感兴趣,就动手实践了一下,最终实现的效果是通过破解IC卡口令实现对数据修改,然后就可以随意洗衣服喽~IC卡从数据传递方式上划分为接触型和非接触型两种.接触型的卡片表面有金属贴片 ...