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, ...
随机推荐
- BZOJ1293:[SCOI2009]生日礼物——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1293 https://www.luogu.org/problemnew/show/P2564#sub ...
- [Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- HDOJ.1342 Lotto (DFS)
Lotto [从零开始DFS(0)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tempter of ...
- 项目管理---git----快速使用git笔记(三)------coding.net注册和新建项目(远程仓库)
我们在第一章已经了解了github和coding.net的区别: github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开. codin ...
- G - YYS FZU - 2278 数学期望 (大数)
Yinyangshi is a famous RPG game on mobile phones. Kim enjoys collecting cards in this game. Suppose ...
- springcloud文章推荐
有幸加入到springcloud中国社区,在这里获取到了不一样的技术,也接触到了不少的大牛! 这篇文章对于springcloud能否在企业中应用或者对springcloud想了解的人个人感觉写的非常好 ...
- 南阳ACM 题目71:独木舟上的旅行 Java版
独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客的总 ...
- [洛谷3457][POI2007]POW-The Flood
洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...
- jedis,spring-redis-data 整合使用,版本问题异常
jedis,spring-redis-data 整合使用,版本不匹配的时候经常会报一些异常,例如1: java.lang.NoClassDefFoundError: org/springframewo ...
- java 8新特性 instant
Java 8目前已经开始进入大众的视线,其中笔者在写本文之前,留意到其中Java 8预览版中将会出现新的关于日期和时间的API(遵守JSR310规范).在本系列文章中,将对这些新的API进行举例说明. ...