LeetCode: 383 Ransom Note(easy)
题目:
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 == "")
return true;
for (auto c : ransomNote){
int position = magazine.find_first_of(c);
if (position != magazine.npos)
magazine.erase(position, );
else
return false;
}
return true;
}
};
别人的:
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int mp[] = {};
for(auto c : magazine) {
mp[c - 'a']++;
}
for(auto r : ransomNote) {
mp[r - 'a']--;
if(mp[r - 'a'] < ) return false;
}
return true;
}
};
LeetCode: 383 Ransom Note(easy)的更多相关文章
- 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 ...
- 383. Ransom Note【easy】
383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...
- 【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, ...
- 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- [LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
随机推荐
- Grunt学习笔记【6】---- grunt-contrib-requirejs插件详解
本文主要讲如何使用Grunt实现RequireJS文件压缩. 一 说明 ES6出来前,RequireJS是JavaScript模块化最常用的方式之一.对于使用RequireJS构建的项目,要实现打包压 ...
- requests不加代理
requests里的proxies不加代理可以设置为空,就会使用本机IP proxies={}
- ZOJ - 3430 Detect the Virus —— AC自动机、解码
题目链接:https://vjudge.net/problem/ZOJ-3430 Detect the Virus Time Limit: 2 Seconds Memory Limit: 6 ...
- 51nod 1537
题目 神犇题解 证明好巧妙,给跪OTZ 题目的式子:$ {\left( {1{\rm{ + }}\sqrt 2 } \right)^{\rm{n}}} $,设其乘开之后为 $ {\rm{a + b}} ...
- zabbix haproxy 监控
摘自: http://www.tuicool.com/articles/JrYNNrm 写的非常好,步步紧逼,环环相扣.直到成功! 文章首发站点:OpensGalaxy 这是一个HAProxy的zab ...
- Custom Database Integration Guide
Introduction This document provides instructions for integrating Openfire authentication, users, and ...
- Zabbix监控华为交换机
一. 监控交换机首先要在交换机开通snmp协议. 有两种方式开通,web界面,及交换机的配置界面 Web界面开通: 交换机配置界面 有web界面的,使用web界面相对简单,本项目就是用web界面 ...
- svn安装以及汉化过程
第一步 下载svn地址 https://tortoisesvn.net/downloads.html 第二步一路next 最后finish 完成 就是完成svn的流程 第三步就是安装语言包 安装包语言 ...
- linux命令学习笔记:cut详解
cut命令从文件的每一行剪切字节.字符和字段并将它们写至标准输出.它是以文件的每一行作为处理对象的. 命令格式:cut [选项] [范围] 文件.选项用来指定单位(字节.字符还是字段),范围指定选项的 ...
- 【Opencv】直方图函数 calchist()
calchist函数需要包含头文件 #include <opencv2/imgproc/imgproc.hpp> 函数声明(三个重载 calchist函数): //! computes t ...