leetcode之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
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
/*第一种方法
if(ransomNote.size() > magazine.size()){
return false;
}
for(int i = 0; i<ransomNote.size() ;){
char ch = ransomNote[i];
std::size_t found = magazine.find(ch);
if(found != std::string::npos){
ransomNote.erase(i,1);
magazine.erase(found,1);
continue;
}
i++;
}
if(ransomNote.length() == 0)
return true;
else
return false;
}*//*第二种检测方法*/
int* arr = new int[];
for(int i=;i<;i++){
arr[i] = ;
}
for(int i=;i<magazine.length();i++)
{
char c = magazine[i];
arr[c-'a']++;
} for(int i=;i<ransomNote.length();i++)
{
char c = ransomNote[i];
arr[c-'a']--;
if(arr[c-'a']<)
return false;
} return true;
}
};
leetcode之Ransom Note的更多相关文章
- 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 ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
- [LeetCode] Ransom Note 赎金条
Given an arbitrary ransom note string and another string containing letters from all th ...
- C#LeetCode刷题之#383-赎金信(Ransom Note)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...
- 【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, ...
随机推荐
- P2805 [NOI2009]植物大战僵尸(最小割+拓扑排序)
题意: n*m的矩阵,每个位置都有一个植物.每个植物都有一个价值(可以为负),以及一些它可以攻击的位置.从每行的最右面开始放置僵尸,僵尸从右往左行动,当僵尸在植物攻击范围内时会立刻死亡.僵尸每到一个位 ...
- Codeforces Round #469 (Div. 2) F. Curfew
贪心 题目大意,有2个宿管分别从1和n开始检查房间,记录人数不为n的房间个数,然后锁住房间. 没有被锁的房间中的学生可以选择藏在床底,留在原地,或者转移(最远转移d个房间) 然后抄了网上大神的代码 ...
- BZOJ3526 [Poi2014]Card 【线段树】
题目链接 BZOJ3526 题解 思来想去,发现很显然可以用线段树维护 每个区间保存所有合法方案的左右端点[当左端点一定是,右端点当然存最小的那个就行了] 这么整的数,\(\frac{1}{1000} ...
- HDOJ.1789 Doing Homework again (贪心)
Doing Homework again 点我挑战题目 题意分析 给出n组数据,每组数据中有每份作业的deadline和score,如果不能按期完成,则要扣相应score,求每组数据最少扣除的scor ...
- 微信小程序将view动态填满全屏
一.在app.js利用官方方法获取设备信息,将获取到的screenHeight.windowHeight度量单位统一由rpx换算为px 注:官方文档给出 [rpx换算px (屏幕宽度/750) ][ ...
- 如何在Linux上安装QQ
我一直无法解决Ubuntu QQ问题,而最近我重装ubuntu之后在网络上找到与QQ相关的内容,网上有大神开发出了新版的wineQQ,解决了我们对QQ的需求.经过尝试,完成了QQ安装 如图 安装的是 ...
- POJ_2112_Optimal Milking 这里有超级快的网络流板子
Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among ...
- Django请求原理(二)
1,Web服务器(中间件)收到一个http请求 2,Django在URLconf里查找对应的视图(View)函数来处理http请求 3,视图函数调用相应的数据模型来存取数据.调用相应的模板向用户展示页 ...
- HBase客户端访问超时的多个因素及参数
在一个需要低延时响应的hbase集群中,使用hbase默认的客户端超时配置简直就是灾难. 但是我们可以考虑在客户端上加上如下几个参数,去改变这种状况: 1. hbase.rpc.timeout: RP ...
- js和jq实现全选反选
在前端中用到全选反选的案例并不少,在这里呢我就实现这个功能给大家参考参考. 这里呢就先贴上我的html和css代码 <div class="wrap"> <tab ...