题目描述


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的更多相关文章

  1. leetcode 383. Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  2. 14. leetcode 383. Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  3. Java [Leetcode 383]Ransom Note

    题目描述: Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 al ...

  4. LeetCode: 383 Ransom Note(easy)

    题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...

  5. [LeetCode] Ransom Note 赎金条

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  6. C#LeetCode刷题之#383-赎金信(Ransom Note)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...

  7. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  8. leetcode修炼之路——383. Ransom Note

    题目是这样的 Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 a ...

  9. [LeetCode&Python] Problem 383. Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

随机推荐

  1. Android Appliction 使用解析

    Application Base class for those who need to maintain global application state. You can provide your ...

  2. HDU3652:B-number——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=3652 题目大意:给一个数n,求1-n所有满足下列条件的数的个数: 1.包含一个子串为“13” 2.能被13整除. ...

  3. LeetCode中二叉树题目总结

    本文仅为博主个人总结,水平有限,欢迎大神指出不妥处. 关于二叉树的相关概念可以参见二叉树的百度百科,或binary tree Wiki. 二叉树结点类的常见定义为: /* Definition for ...

  4. BZOJ1042 [HAOI2008]硬币购物 【完全背包 + 容斥】

    1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2924  Solved: 1802 [Submit][St ...

  5. B树,B+树,B*树简介

    B树(有些人也叫B-树) 是一种多路搜索树 : 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点的儿子数为[M/2, M]: ...

  6. 使用 Intel HAXM 为eclipse安卓模拟器加速

    一.下载haxm安装 https://software.intel.com/zh-cn/android/articles/intel-hardware-accelerated-execution-ma ...

  7. Django问题 TypeError: __init__() missing 1 required positional argument: 'on_delete'

    问题:在执行python manage.py makemigrations learning_logs时,系统会报错,提示:TypeError: __init__() missing 1 requir ...

  8. std::string::find() 和 std::string::npos

    npos是一个常数,用来表示不存在的位置,string::npos代表字符串到头了结束了.   int idx = str.find("abc");if (idx == strin ...

  9. Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)

    C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...

  10. udhcpd源码分析3--IP租赁管理

    1:重要的结构体 全局链表的成员struct dhcpOfferedAddr *leases 记录了当前租赁出去的IP信息 /* leases.h */ struct dhcpOfferedAddr ...