leetcode 383. Ransom Note
2017-07-12 14:28
417 查看
1. 题目分析
题目描述: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.
题目含义:题目描述的是从magazines中挑选字符组合成一个ransom note。并且组成两个string的元素都是小写字母。
题目分析:这个题目的意思就是从string magazines中抽取元素组合成一个string ransom是否能够成立。即ransom中的元素都能够在magazines中找到,无序的一一对应。结题思路即对两个字符串中的元素进行计数,如果同一个元素在ransom中的数量不超过magazines即满足条件。解体工具使用vector容器即可。
2. 题目解答–cpp
class Solution { public: bool canConstruct(string ransomNote, string magazine) { //定义一个含有26个成员的空容器,并且每一个初始值都为0 vector<int> v(26, 0); //遍历magazines中的成员,其ascii码相对a的差值为期索引,每检索到一个,值加一 for (auto c : magazine) v[c-'a']++; //遍历ransomNote中的成员,每检索到一次就对vector中对应值减1 for (auto c : ransomNote) { if (--v[c-'a'] < 0) return false; } return true; } };
3. 心得体会
主要问题还是出现在题目分析的过程中。起初自己的想法是有序挑选magazines中的成员,但是题目的含义是无序抽取。相关文章推荐
- leetcode - 383. Ransom Note
- 383. Ransom Note [LeetCode]
- LeetCode | 383. Ransom Note
- [LeetCode] 383. Ransom Note
- Leetcode 383. Ransom Note 构造字符串 解题报告
- leetcode_383. Ransom Note-近似子串问题
- leetcode(85).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 解题报告
- leetcode 383. Ransom Note
- 【Leetcode】383. Ransom Note
- leetcode 383. Ransom Note
- LeetCode笔记:383. Ransom Note
- Leetcode383. Ransom Note