LeetCode 383. 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
思路:
先分别计算ransomNote和magazine的长度,如果ransomNote的长度大,直接返回false。如果ransomNote的长度大,直接返回false; 如果在magazine中找到第一个ransomNode[i],如果这个位置不是字符串中的最后(不过npos一般是-1),就将magazine中这个索引对应的字符删掉,继续循环,否则返回false。如果循环结束没有返回false,就可以构建,返回true。
代码:
class Solution {
public:bool canConstruct(string ransomNote, string magazine) {int lenran=ransomNote.length();//分别计算ransomNote和magazine的长度int lenmag=magazine.length();if(lenran>lenmag){//如果ransomNote的长度大,直接返回falsereturn false;}for(int i=0;i<lenran;++i){//遍历ransomNoteint index=magazine.find_first_of(ransomNote[i]);//如果在magazine中找到第一个ransomNode[i]if(index!=string::npos){//如果这个位置不是字符串中的最后(不过npos一般是-1)magazine.erase(index,1);//就将magazine中这个索引对应的字符删掉,继续循环continue;}else{//否则返回falsereturn false;}}return true;//如果循环结束没有返回false,就可以构建,返回true}
};
**输出结果:*42ms
LeetCode 383. Ransom Note相关推荐
- Leetcode 383 Ransom Note
lc383 Ransom Note 两个for 第一个记录sourse字符串每种字母出现次数 第二个看现有字母是否能够填满target 1 class Solution { 2 public bool ...
- leetcode 383. Ransom Note(赎金票据)
Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the ...
- LeetCode:383. Ransom Note
051104 题目 Given an arbitrary ransom note string and another string containing letters from all the m ...
- [LeetCode] NO.383 Ransom Note
[题目] Given an arbitrary ransom note string and another string containing letters from all ...
- LeetCode之Ransom Note
1.题目 Given an arbitrary ransom note string and another string containing letters from all the magazi ...
- 383. Ransom Note/691. Stickers to Spell Word-- String, Map, back tracking-- 未完待续
383 easy 题,就是建立字母的hash 表 看第一个String 是否能被第二个String 所构建 canConstruct("aa", "aab") ...
- C#LeetCode刷题之#383-赎金信(Ransom Note)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...
- Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the ...
- LeetCode.383. 赎金信
LeetCode.383. 赎金信 难度:easy 利用数组来做哈希表,分别统计两个字符串中各个字母的出现次数: class Solution {public boolean canConstruct ...
最新文章
- cookie和session 创建和验证 原始的servlet
- leetcode-14-最长公共前缀
- javaone_JavaOne 2014:会议与合同利益冲突
- Spring Cloud应用监控与管理Actuator
- Flash of Unstyled Content (FOUC)
- 【报告分享】二次元衍生创作行业报告.pdf(附下载链接)
- GTID复制异常的解决步骤
- dubbo原理和机制_面试官几个 Dubbo 微服务框架问题,把我整懵了?
- springboot13 页面国际化(i18n)
- AJAX用法、HTTP头部信息
- linux mysql配置_Linux下MySQL安装配置 MySQL配置参数详解
- 星环分析型数据库Inceptor中database link的使用总结
- RL78开发问题记录
- 【Python】字谜游戏
- 一条互联网广告多少钱?
- 模仿支付宝支付成功特效:svg圆圈转动+打钩
- 面试经历---UC(2016年01月11日下午面试)
- 3.GoolgeProtoBuffer序列化反序列化
- leetcode.377. 组合总和 Ⅳ---dp问题
- 学籍管理系统c语言,c语言学籍信息管理系统设计
热门文章
- java cookie secure_Cookie的Secure属性
- OpenStack Nova核心组件和RabbitMQ通信流程分析
- L13操作系统之树(过程)
- phpcmsV9 完整更新ckeditor编辑器到最新版 - 源码篇
- css实现发光的input输入框
- Do not use built-in or reserved HTML elements as component id等等vue warn问题
- wordpress插件-WP Rocket3.8.8与Rocket3.9.1双版本/火箭缓存插件免授权汉化版
- mfc 子窗体 按钮不触发_资深程序员用c++开发MFC银行排队叫号系统,小白看了也能学会...
- python发送邮件并返回提示_python-邮件提醒功能
- 高并发处理方案_高并发系统下的缓存解决方案