383. Ransom Note
来源:互联网 发布:sql中like用法 编辑:程序博客网 时间:2024/06/01 07:33
原本打算使用list的<运算符,因为set具有比较两个set是否是包含关系的<运算符,但是list的<运算符比较的是list中的相应元素的大小,第一个元素优先比较,如果第一个元素是相等的,则比较第二个元素
class Solution(object): def canConstruct(self, ransomNote, magazine): """ :type ransomNote: str :type magazine: str :rtype: bool """ r = list(ransomNote) m = list(magazine) d= dict() for i in range(len(m)): if m[i] in d: d[m[i]] += 1 else: d[m[i]] = 1 for i in range(len(r)): if r[i] in d: if d[r[i]] > 0: d[r[i]] -= 1 else: return False else: return False return True
将magazine中的字母按照字母作为key,出现的次数作为value的形式存储在dict中,ransomNote中的字母挨个去dict中检查,当dict中没有相应字母或者相应字母的次数已经小于零的时候,返回False即可
- 使用collections.Counter
Counter的介绍: http://www.2cto.com/kf/201303/196938.html
def canConstruct(self,ransomNote,magazine): return not collections.Counter(ransomNote) - collections.Counter(magazine)
0 0
- 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
- (不ROOT/不安装软件/安全)--Android/安卓手机手动冻结/禁用系统应用方法
- PHP 中使用explode()函数切割字符串为数组
- MySQL Order By实现原理分析和Filesort优化
- 各种面试题-虐虐虐
- [cv] filters as templates-normxcorr2(template,img)
- 383. Ransom Note
- java的本地方法
- 开博客了
- 如何解决回调地狱问题(任何语言)
- InstantRun框架源码分析之一
- Java面试知识点(全面)
- CSU 1913: 送礼物 (二分答案 + 线段树)
- WAMPServer自定义端口号
- 不得不知的 load 与 initialize