题目描述:

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(以空间换时间)的更多相关文章

  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. JDK1.8 LongAdder 空间换时间: 比AtomicLong还高效的无锁实现

    我们知道,AtomicLong的实现方式是内部有个value 变量,当多线程并发自增,自减时,均通过CAS 指令从机器指令级别操作保证并发的原子性. // setup to use Unsafe.co ...

  6. Redis学习笔记~关于空间换时间的查询案例

    回到目录 空间与时间 空间换时间是在数据库中经常出现的术语,简单说就是把查询需要的条件进行索引的存储,然后查询时为O(1)的时间复杂度来快速获取数据,从而达到了使用空间存储来换快速的时间响应!对于re ...

  7. Redis基础知识之————空间换时间的查询案例

    空间与时间 空间换时间是在数据库中经常出现的术语,简单说就是把查询需要的条件进行索引的存储,然后查询时为O(1)的时间复杂度来快速获取数据,从而达到了使用空间存储来换快速的时间响应!对于redis这个 ...

  8. 你好,C++(28)用空间换时间 5.2 内联函数 5.3 重载函数

    5.2  内联函数 通过5.1节的学习我们知道,系统为了实现函数调用会做很多额外的幕后工作:保存现场.对参数进行赋值.恢复现场等等.如果函数在程序内被多次调用,且其本身比较短小,可以很快执行完毕,那么 ...

  9. 计数排序(O(n+k)的排序算法,空间换时间)

    计数排序就是利用空间换时间,时间复杂度O(n+k) n是元素个数,k是最大数的个数: 统计每个数比他小的有多少,比如比a[i]小的有x个,那么a[i]应该排在x+1的位置 代码: /* * @Auth ...

随机推荐

  1. sql判断表是否已经存在

    if (object_id(N'td_VipExchangeCodeInfo',N'U') is not null)print '存在'else print '不存在'

  2. UVa 11136 Hoax or what (STL)

    题意:有 n 天,每天有m个数,开始的前一天没有数据,然后每天从这个里面拿出一个最大的和最小的,求 n 天的最大的和最小的差值相加. 析:一看就知道用set啊,多简单的STL,不过要注意,开long ...

  3. 复杂HTML页面解析

    1.层叠样式表CSS可以让html元素呈现出差异化,网络爬虫可以通过class属性的值,轻松分出不同标签 findAll函数通过标签的名称和属性来查找标签 from urllib.request im ...

  4. Linux 基础教程 45-read命令

    基本用法     read命令主要用于从标准输入读取内容或从文件中读取内容,并把信息保存到变量中.其常用用法如下所示: read [选项] [文件] 选项 解释 -a array 将内容读取到数值中, ...

  5. 移动端html5页面导航栏悬浮遮挡内容第一行解决办法

    参考:https://zhidao.baidu.com/question/1608232105428062147.html 1.设置导航栏div属性position:fixed; .nav-fixed ...

  6. linux 搭建php网站许愿墙

    网站素材在:https://i.cnblogs.com/Files.aspx 首先需要搭建本地yum源,详情参考: http://www.cnblogs.com/jw35/p/5967677.html ...

  7. Asp.NetCore Razor 模式 Web 应用

    Razor 页面是 ASP.NET Core MVC 的一个新功能,它可以使基于页面的编码方式更简单高效. Razor 页面是 ASP.NET Core 2.0 中的一个新选择,它是基于页面的编程模型 ...

  8. task3:词频统计

    相关的类: java.util.regex.Pattern static Pattern compile(String regex) //编译模式 static Pattern compile(Str ...

  9. IKAnalyzer兼容Lucene 5.4.0版本抛出异常?

    ava.lang.AbstractMethodError: org.apache.lucene.analysis.Analyzer.createComponents(Ljava/lang/String ...

  10. Linux socat轻松实现TCP/UDP端口转发

    1.TCP端口转发 socat -d TCP4-LISTEN:,reuseaddr,fork TCP4: 2.UDP端口转发 socat -T UDP4-LISTEN:,reuseaddr,fork ...