Leetcode- 383. Ransom Note(string)
来源:互联网 发布:java中绝对值函数 编辑:程序博客网 时间:2024/06/06 20:31
题目链接:点击打开链接
题目大意:假设有一个任意的赎金的字符串和另一个包含所有杂志的信件的字符串,写一个函数,如果可以从杂志上构造出赎金的话,这个函数就会返回true;否则,它将返回false。
其实就是问在magazine里面能不能构造出ransomNote字符串,且magazine中的字符都只能用一次,可以的话就返回true,否则返回false,前面一直没看懂题目、、、、以为是查找子串
解题思路:因为只有26个字母,直接开两个数组去比较个数就好啦,看代码吧。
解题代码:
class Solution {public: bool canConstruct(string ransomNote, string magazine) { int a[26]={0},b[26]={0}; int len1=ransomNote.length(); int len2=magazine.length(); if(len1>len2) return false; if(ransomNote == "" && magazine == "") return true; for(int i=0;i<len1;i++) { a[ransomNote[i]-'a']++; } for(int j=0;j<len2;j++) { b[magazine[j]-'a']++; } for(int i=0;i<26;i++) { if(a[i]>b[i]) return false; } return true; }};
阅读全文
0 0
- Leetcode - String - 383. Ransom Note(水题)
- Leetcode- 383. Ransom Note(string)
- String:383. Ransom Note
- leetcode-383. Ransom Note
- [leetcode] 383. Ransom Note
- LeetCode 383. Ransom Note
- leetcode 383. Ransom Note
- leetcode 383. Ransom Note
- 383.[LeetCode]Ransom Note
- leetcode 383. Ransom Note
- leetcode 383. Ransom Note
- Leetcode 383. Ransom Note
- 【leetcode】383. Ransom Note
- Leetcode 383. Ransom Note
- LeetCode 383. Ransom Note
- [LeetCode]383. Ransom Note
- LeetCode 383. Ransom Note
- [LeetCode]--383. Ransom Note
- 牢记这五点炒股技巧 越简单越挣钱
- 基本排序算法
- js的基本包装模型
- 《C++ Primer Plus》ch05笔记
- Eclipse git图文教程
- Leetcode- 383. Ransom Note(string)
- SpringMVC源码分析(三)之Controller方法参数的工作原理
- 关于using namespace std;使用经验
- 插入排序--折半插入
- (crm笔记1-6)struts2和hibernate的整合
- 使用Pycharm创建virtualenv虚拟环境
- NC开发笔记——NC57开发方法总结
- hdu3081 二分 网络流
- 听说中秋你要吃螃蟹?这有一款自动剥螃蟹机器人 ‖ 小视频