LeetCode: 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) {
if (ransomNote == "")
return true;
for (auto c : ransomNote){
int position = magazine.find_first_of(c);
if (position != magazine.npos)
magazine.erase(position, );
else
return false;
}
return true;
}
};
别人的:
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int mp[] = {};
for(auto c : magazine) {
mp[c - 'a']++;
}
for(auto r : ransomNote) {
mp[r - 'a']--;
if(mp[r - 'a'] < ) return false;
}
return true;
}
};
LeetCode: 383 Ransom Note(easy)的更多相关文章
- 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, ...
- Java [Leetcode 383]Ransom Note
题目描述: Given an arbitrary ransom note string and another string containing letters from al ...
- 383. Ransom Note【easy】
383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- leetcode修炼之路——383. Ransom Note
题目是这样的 Given an arbitrary ransom note string and another string containing letters from a ...
- [LeetCode&Python] Problem 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- [LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
随机推荐
- vc2013使用经验
1 find all reference功能需要visual assist的帮助 vs2013自己的查找不行,所以可以安装visual assist X,这样的话,就可以支持快速准确的referenc ...
- 销售订单增强字段 bapi更新
如果增强字段在销售订单抬头(vbak)上,则要将增强字段一并append到如下四个表/结构中: VBAKKOZ VBAKKOZX BAPE_VBAK BAPE_VBAKX 在行项目(vbap)上: V ...
- IDEA:Application Server was not connected before run configuration stop, reason: Unable to ping 1099
原文链接 : http://blog.csdn.net/x6582026/article/details/70807269 最近第一次玩IDEA时碰到tomcat启动问题:Application Se ...
- centos7 永久修改主机名
hostnamectl set-hostname xxx 一劳永逸,永绝后患
- ss命令能识别的TCP状态的关键字
[TCP_ESTABLISHED] = "ESTAB", [TCP_SYN_SENT] = "SYN-SENT", [TCP_S ...
- BZOJ 1680 [Usaco2005 Mar]Yogurt factory:贪心【只用考虑上一个】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1680 题意: 在接下来的n周内,第i周生产一吨酸奶的成本为c[i],订单为y[i]吨酸奶. ...
- 分享知识-快乐自己: Oracle数据库实例、用户、表、表空间之间关系
数据库: Oracle数据库是数据的物理存储.这就包括(数据文件ORA或者DBF.控制文件.联机日志.参数文件). 其实Oracle数据库的概念和其它数据库不一样,这里的数据库是一个操作系统只有一个库 ...
- Linux_学习_02_ 重启tomcat与查看tomcat日志
一.重启tomcat服务器 cd /home/ehlhec/tomcat_dingtalk/bin ./shutdown.sh ps -ef|grep java ./startup.sh (1) 进入 ...
- 常见排序算法-php
1.归并排序 $a = [1, 4, 6, 8, 10, 14, 16]; $b = [2, 3, 5, 8, 9, 11]; function merge_sort($a, $b) { $a_i = ...
- bzoj 4514: 数字配对
题目大意 自己看 题解 我们打表观察规律发现一定能构成一张二分图 也就是不存在奇环 所以我们一般保证费用非负的最大流即可. #include <cstdio> #include <c ...