LeetCode 383:Ransom Note
2016-09-12 20:00
501 查看
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.
给定两个字符串,如果后一个字符串包含了前一个字符串所有出现的字母,返回真,否则返回假。
第二个字符串中的字母一个只能使用一次。(即如果前面出现了两个‘a',那么后面的字符串也要出现两次或两次以上的'a',否则算错)
注意:
你可以假定两个字符串里都只有小写字母。
考研党诈尸啦
这道题没啥难的,我自己用了一个26元素的int数组,统计一下各字母出现的次数就完了。。。
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
给定两个字符串,如果后一个字符串包含了前一个字符串所有出现的字母,返回真,否则返回假。
第二个字符串中的字母一个只能使用一次。(即如果前面出现了两个‘a',那么后面的字符串也要出现两次或两次以上的'a',否则算错)
注意:
你可以假定两个字符串里都只有小写字母。
canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true
考研党诈尸啦
这道题没啥难的,我自己用了一个26元素的int数组,统计一下各字母出现的次数就完了。。。
class Solution { public: bool canConstruct(string ransomNote, string magazine) { int temp[26]; for(int i=0;i<26;i++) temp[i]=0; int mlen=magazine.length(); for(int i=0;i<mlen;i++) temp[magazine[i]-'a']++; int rlen=ransomNote.length(); for(int i=0;i<rlen;i++){ int num=ransomNote[i]-'a'; if(temp[num]==0) return false; temp[num]--; } return true; } };
相关文章推荐
- 并查集
- 160809132 梁佳佳
- Django踩坑
- Android Volley使用之二:Volley请求网络图片
- sass实战演练06 - 把布局拆分为12列(2):拆分和偏移
- 行内元素和块元素
- MongoDB 的用户认证
- 【JZOJ4771】爬山
- 第9章:面向对象变量与关键字
- Mac 下gdb 的安装过程
- opencv实现多图像读取并显示,sprintf_s函数,static_cast
- stream_context_create() 模拟 POST、GET数据
- HDU 5873 - Football Match【2016大连区域赛网络赛1006】
- Hive在spark2.0.0启动时无法访问../lib/spark-assembly-*.jar: 没有那个文件或目录的解决办法
- javascript网页特效——table
- java util :获取国家省份城市工具类
- 关闭不断重启的进程
- MySQL 数据库增量数据恢复案例
- graphx之图迭代
- matlab .p文件