LeetCode383. 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
题目大意:给两个string,判断第一个string能否由第二个string里面所含有的字母组成,第二个string里面的所有字母只能使用一次~
分析:建立一个hash数组,对第二个string遍历并记录每个字符出现的次数,然后遍历第一个string,如果有出现hash里面不存在的字符,那么return false~
class Solution {
public:bool canConstruct(string ransomNote, string magazine) {vector<int> hash(256);for(int i = 0; i < magazine.size(); i++)hash[magazine[i]]++;for(int i = 0; i < ransomNote.size(); i++)if(hash[ransomNote[i]])hash[ransomNote[i]]--;elsereturn false;return true;}
};
LeetCode383. Ransom Note相关推荐
- LeetCode:383. Ransom Note
051104 题目 Given an arbitrary ransom note string and another string containing letters from all the m ...
- [LeetCode] NO.383 Ransom Note
[题目] Given an arbitrary ransom note string and another string containing letters from all ...
- Leetcode 383 Ransom Note
lc383 Ransom Note 两个for 第一个记录sourse字符串每种字母出现次数 第二个看现有字母是否能够填满target 1 class Solution { 2 public bool ...
- LeetCode之Ransom Note
1.题目 Given an arbitrary ransom note string and another string containing letters from all the magazi ...
- LeetCode 383. Ransom Note
题目 : Given an arbitrary ransom note string and another string containing letters from all the magazi ...
- C#LeetCode刷题之#383-赎金信(Ransom Note)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...
- Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the ...
- 383. Ransom Note/691. Stickers to Spell Word-- String, Map, back tracking-- 未完待续
383 easy 题,就是建立字母的hash 表 看第一个String 是否能被第二个String 所构建 canConstruct("aa", "aab") ...
- LeetCode Ransom Note(字符串)
题意:给出两个字符串s1,s2 ,看s1是否可以由s2中的字符组成 具体代码如下: public class Solution {public boolean canConstruct(String ...
最新文章
- C# string.Format谨慎使用
- HDU 3555 Bomb
- centreon问题总结
- sublime text常用快捷键及多行光标批量操作教程
- python哈夫曼编码注意_Python 算法(2) 哈夫曼编码 Huffman Encoding
- throws Exception的意思
- 关于python django.url问题 求助
- CUDA2.1-原理之索引与warp
- SQL Server 日期函数:EOMonth、DateFormat、Format、DatePart、DateName
- isinstance / issubclass / type, 方法和函数, 反射
- Javascript 加载详解
- 完整卸载SQL Server2008
- 几种常用的网络传输协议
- C++学习记录vs2013 植物大战僵尸mfc辅助大体框架编写
- “全球化”是一个漫长过程,海尔智家用了20年
- php中求解二元一次方程,基于Algebra.js解二元一次方程的实例教程
- LS1046修改寄存器翻转SATA引脚的N/P极性
- DBlink 创建 删除 脚本
- NASA顶级程序员编程十大原则
- python将秒换成天时分秒