LeetCode 383:Ransom Note (c++)
来源:互联网 发布:上海每日房屋成交数据 编辑:程序博客网 时间:2024/05/29 14:50
一:题目
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") -> falsecanConstruct("aa", "ab") -> falsecanConstruct("aa", "aab") -> true
二:解题思路
一开始看题,并没有看懂题目的意思,查阅了网友们的博客,该题可以理解为:
给定一个字符串A,然后再给定一个字符串B。要求如果A字符串能够从B字符串的字符中构造,那么返回true,否则返回false
即,在字符串A中出现的字符个数要小于字符串B相应的字符个数
则只需统计字符串A中,出现了哪些字符,出现多少次,字符串B中出现哪些字符,出现多少次,然后两者进行比较
三:代码实现
class Solution {public: bool canConstruct(string ransomNote, string magazine) { int i,j; int valueOfRansomNote[26]={0}; int valueOfMagazine[26]={0}; //统计ransomNote中的26个字母出现的个数 for(i=0;i<ransomNote.length();i++) valueOfRansomNote[ransomNote[i]-'a']++; //统计magazine中的26个字母出现的个数 for(j=0;j<magazine.length();j++) valueOfMagazine[magazine[j]-'a']++; //两个字符串26个字母出现次数比较 for(i=0;i<26;i++) if(valueOfRansomNote[i]>valueOfMagazine[i]) return false; return true; }};
阅读全文
0 0
- LeetCode 383:Ransom Note (c++)
- 【LeetCode-383】Ransom Note(C++)
- LeetCode(383)Ransom Note
- LeetCode 383. Ransom Note 题解(C++)
- 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(java)
- LeetCode--No.383--Ransom Note
- leetcode 383 Ransom Note C++
- https://sqlwhisper.wordpress.com/2013/03/24/stuff-and-for-xml-path-for-string-concatenation/
- 反射再学习
- JSP&Servlet中request.getParameter() 和request.getAttribute() 区别
- 静态成员函数
- 深度学习笔记空间金字塔池化阅读笔记Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
- LeetCode 383:Ransom Note (c++)
- U盘修复工具哪个好?7款U盘低格工具详解
- 我的第一篇博客
- My Blog
- 创业大佬说
- 微信(Javascript 语言的模板引擎)中的Mustache
- caffe 学习笔记之DropoutParameter
- 6月21日云栖精选夜读:国内首位!Node.js社区将阿里云工程师张秋怡吸纳为CTC成员
- jvm的原理及內存優化