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
要完成的函数:
bool canConstruct(string ransomNote, string magazine)
说明:
1、古时候……绑架者会写勒索信,要求给赎金,不然就撕票。但为了不被认出自己的字迹,就会从报纸上剪出文字,拼凑成一封勒索信。这道题给定两个字符串,第一个是绑匪要写的勒索信的字符串,第二个是报纸上能提供的文字的字符串,要求判断能不能从第二个字符串中构建出第一个字符串。
第二个字符串中的每个字母只能用一次,两个字符串中只有小写字母。
2、这道题有三种做法:
①双重循环,第一个字符串碰到一个字符就去找第二个字符串中有没有,这是最慢最没有效率的做法。
②先排序,再比较,这种做法时间复杂度比①小,但也不快。
③以空间换时间,定义一个长度为26的vector,遍历一遍第二个字符串,统计所有字母的出现次数,再遍历一遍第一个字符串,逐个在vector中相应位置上减1。
最后再遍历一遍vector,看是否存在小于0的数值,如果有,返回false。如果没有,返回true。
时间复杂度是O(n)
我们采用第三种做法,构造代码如下:
bool canConstruct(string ransomNote, string magazine)
{
vector<int>count(26,0);
for(char i:magazine)//统计第二个字符串中所有的字母出现次数
count[i-'a']++;
for(char i:ransomNote)//在对应的位置上减去1
count[i-'a']--;
for(int i:count)//遍历一遍vector,看是否存在小于0的数值
{
if(i<0)
return false;
}
return true;
}
上述代码实测24ms,beats 89.66% of cpp submissions。
leetcode-383-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 ...
- JDK1.8 LongAdder 空间换时间: 比AtomicLong还高效的无锁实现
我们知道,AtomicLong的实现方式是内部有个value 变量,当多线程并发自增,自减时,均通过CAS 指令从机器指令级别操作保证并发的原子性. // setup to use Unsafe.co ...
- Redis学习笔记~关于空间换时间的查询案例
回到目录 空间与时间 空间换时间是在数据库中经常出现的术语,简单说就是把查询需要的条件进行索引的存储,然后查询时为O(1)的时间复杂度来快速获取数据,从而达到了使用空间存储来换快速的时间响应!对于re ...
- Redis基础知识之————空间换时间的查询案例
空间与时间 空间换时间是在数据库中经常出现的术语,简单说就是把查询需要的条件进行索引的存储,然后查询时为O(1)的时间复杂度来快速获取数据,从而达到了使用空间存储来换快速的时间响应!对于redis这个 ...
- 你好,C++(28)用空间换时间 5.2 内联函数 5.3 重载函数
5.2 内联函数 通过5.1节的学习我们知道,系统为了实现函数调用会做很多额外的幕后工作:保存现场.对参数进行赋值.恢复现场等等.如果函数在程序内被多次调用,且其本身比较短小,可以很快执行完毕,那么 ...
- 计数排序(O(n+k)的排序算法,空间换时间)
计数排序就是利用空间换时间,时间复杂度O(n+k) n是元素个数,k是最大数的个数: 统计每个数比他小的有多少,比如比a[i]小的有x个,那么a[i]应该排在x+1的位置 代码: /* * @Auth ...
随机推荐
- was not registered for synchronization because synchronization is not active
报SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7862f70e] was not registered for s ...
- MongoDB--CSharp Driver Quickstart .
原文链接 http://www.mongodb.org/display/DOCS/CSharp+Driver+Quickstart?showComments=true&showCommentA ...
- sql添加/移除约束
唯一:ALTER TABLE TableName ADD CONSTRAINT UQ_NickName UNIQUE(NickName) 主键:ALTER TABLE TableName ADD CO ...
- 8-5 Navicat工具与pymysql模块
一 Navicat 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库 需要掌握的基本操作 ...
- [解决]--java_out: User.proto: User.proto: Cannot generate Java output because the file 's
在使用 protocol buffer 的时候,用.proto文件生成代码文件时报错 使用命令 protoc.exe --java_out c:\logs\ User.proto User.proto ...
- VC 调试技术与异常(错误)处理 VC 调试技术与异常(错误)处理
调试技术与异常(错误)处理 (1) 转载自 52PK游戏论坛 跟踪与中间过程输出 也许一个开发人员一半以上的时间都是在面对错误,所以好的调试/查错方法(工具)会减轻我们工作的负担,也可以让枯燥的D ...
- HDU1412:{A} + {B}
Problem Description 给你两个集合,要求{A} + {B}. 注:同一个集合中不会有两个相同的元素. Input 每组输入数据分为三行,第一行有两个数字n,m(0<n,m& ...
- [C#]如何解决修改注册表受限问题(转)
在项目中添加一个Application Manifest File,名字默认为app.manifest,内容中应该有一行: <requestedExecutionLevellevel=" ...
- 生成Webservice的两种方式(Axis2,CXf2.x)
一天之中,用了各种方式生成webservice,就是为了node.js能和程序顺利通信.最终还是用axis2成功了.工作基本完成了,现在可以总结一下. 关于生成方式,推荐使用eclipse,比较方便, ...
- 在linux中使用包管理器安装node.js
网上文章中,在linux下安装node.js都是使用源码编译,其实node的github上已经提供了各个系统下使用各自的包管理器(package manager)安装node.js的方法. 1. 在U ...