051104

题目

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

我的解题思路

这道题是查看字符串magazines是否包含了ransom note string,不管其出现的顺序只要每个字符及其次数能组会起来就行。因此,我们可以对magazines求一个map,存储其字符及其该字符出现的次数。之后再对ransom note string的字符进行判断,当ransom note string中出现该字符则减掉map中的数,若最后所有的字符对应的次数都大于等于0,则返回true,否则false。

class Solution {
public:bool canConstruct(string ransomNote, string magazine) {unordered_map<char, int> map(26);for (int i = 0; i < magazine.size(); ++i)++map[magazine[i]];for (int j = 0; j < ransomNote.size(); ++j)if (--map[ransomNote[j]] < 0)return false;return true;}
};

去看了大佬的解答,不用map之类的stl库实现,而是只用了string的函数。tql~


class Solution {
public:bool canConstruct(string ransomNote, string magazine) {for(char c: ransomNote){int found = magazine.find(c);if(found == string::npos){return false;}magazine[found] = ' ';  }return true;}
};

LeetCode:383. Ransom Note相关推荐

  1. [LeetCode] NO.383 Ransom Note

    [题目] Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all ...

  2. LeetCode 383. Ransom Note

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

  3. Leetcode 383 Ransom Note

    lc383 Ransom Note 两个for 第一个记录sourse字符串每种字母出现次数 第二个看现有字母是否能够填满target 1 class Solution { 2 public bool ...

  4. leetcode 383. Ransom Note(赎金票据)

    Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the ...

  5. 383. Ransom Note/691. Stickers to Spell Word-- String, Map, back tracking-- 未完待续

    383  easy 题,就是建立字母的hash 表 看第一个String 是否能被第二个String 所构建 canConstruct("aa", "aab") ...

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

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

  7. LeetCode之Ransom Note

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

  8. LeetCode:二叉树相关应用

    LeetCode:二叉树相关应用 基础知识 617.归并两个二叉树 题目 Given two binary trees and imagine that when you put one of the ...

  9. LeetCode383. Ransom Note

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

最新文章

  1. TCP、IP协议族之数字签名与HTTPS详解
  2. 全卷积神经网路【U-net项目实战】LUNA 2016 数据集详解
  3. phpMyAdmin常用设置
  4. Linux shell编程学习总结
  5. [转载] 基于LSTM的股票预测模型_python实现_超详细
  6. Python 安装第三方库,模块和包的安装方法
  7. python 条件语句、循环语句
  8. JSON金额解析BUG的解决过程
  9. 导入html文件到onenote,onenote怎么导入文件 onenote添加文件附件的图文步骤
  10. java分布式日志组件
  11. 《OpenGL编程指南(原书第9版)》——第3章 3.0OpenGL绘制方式
  12. 基于[三星6818]芯片超声波测距驱动编写
  13. Go语言-基本类型(int,float,bool,byte,string)
  14. 2022 年值得关注的 8 个人工智能趋势
  15. WordPress安装教程(2022)|详细
  16. 智慧校园之蓝牙定位在学校中的应用,蓝牙室内定位-新导智能
  17. 偷个懒,公号抠腚早报80%自动化——1.批量生成微信封面图
  18. SCI论文及期刊查询
  19. Nature重磅:华裔科学家成功解码脑电波,AI直接从大脑中合成语音
  20. Gitblit使用安装使用教程

热门文章

  1. win7 cmd 操作mysql数据库
  2. 快速定制SolidWorks Electrical清单模板
  3. Linux大文件传输(转)
  4. XenApp部署之配置XenApp Server
  5. [ruby] wxRuby安装
  6. FreeBSD 8.0 终于发布正式版了
  7. Predictably Irractional - 零成本的成本
  8. debian卸载php_如何卸载软件
  9. model.fit以及model.fit_generator区别及用法
  10. mxnet中的SoftmaxCrossEntropyLoss损失函数