383. Ransom Note
来源:互联网 发布:矩阵的秩怎么算 编辑:程序博客网 时间:2024/05/22 06:36
原题链接:https://leetcode.com/problems/ransom-note/
原题:
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
题意:
给你两个字符串1和2,要去你判断能否用字符串2里面的字母组成字符串1,要求字符串2里面的字母一个只能用一次,能则return true 反之return false,给的字母都是小写字母。
思路:
用一个int数组保存26个小写字母在字符串2的出现次数,然后开始遍历字符串1,然后每次遇到一个字母,判断一次“库存”,不够,择return false,够则库存-1;遍历完成后,发现都够,则return true。
AC代码
class Solution {public: bool canConstruct(string ransomNote, string magazine) { int b[26]={0}; for(char s:magazine){ b[s-'a']++; } for(char s:ransomNote){ if(b[s-'a']==0) return false; b[s-'a']--; } return true; }};
- leetcode-383. Ransom Note
- [leetcode] 383. Ransom Note
- LeetCode 383. Ransom Note
- 383. Ransom Note*
- 383. Ransom Note
- leetcode 383. Ransom Note
- leetcode 383. Ransom Note
- 383. Ransom Note
- 383.[LeetCode]Ransom Note
- 383. Ransom Note
- 383. Ransom Note【E】
- leetcode 383. Ransom Note
- 383. Ransom Note
- 383. Ransom Note
- leetcode 383. Ransom Note
- Leetcode 383. Ransom Note
- 【leetcode】383. Ransom Note
- Leetcode 383. Ransom Note
- STL hash_map使用
- jQuery中常用动画效果函数(日常整理)
- java编程思想读书笔记 第十一章 持有对象(总结)
- 10-08
- Moving Tables
- 383. Ransom Note
- 第三十六讲项目 用循环处理文字(血型统计,消除空格等)
- linux信号量简介(内核态)
- 写VS配置文件时遇到的一个操蛋问题
- android程序设计中输入手机号,验证格式,并倒计时几秒后再次发送
- 隐藏EditText输入下划线
- 欢迎使用CSDN-markdown编辑器
- 深度学习 VS 传统的机器学习
- Android 6.0新特性之WebView不能适配屏幕解决方案