Leecode刷题之旅-C语言/python-383赎金信
/*
* @lc app=leetcode.cn id=383 lang=c
*
* [383] 赎金信
*
* https://leetcode-cn.com/problems/ransom-note/description/
*
* algorithms
* Easy (44.98%)
* Total Accepted: 5K
* Total Submissions: 11.1K
* Testcase Example: '"a"\n"b"'
*
* 给定一个赎金信 (ransom)
* 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回
* true ;否则返回 false。
*
* (题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)
*
* 注意:
*
* 你可以假设两个字符串均只含有小写字母。
*
*
* canConstruct("a", "b") -> false
* canConstruct("aa", "ab") -> false
* canConstruct("aa", "aab") -> true
*
*
*/
bool canConstruct(char* ransomNote, char* magazine) {
if (ransomNote == NULL || magazine == NULL) {
return false;
}
int index[];
for(int i=;i<;i++){
index[i]=;
}
for (int i = ; i <strlen(magazine); i++) {
index[magazine[i]-'a']++;
}
for (int j = ; j <strlen(ransomNote); j++) {
if (--index[ransomNote[j]- 'a'] < ) {
return false;
}
}
return true;
}
这里的思路就是,建立一个数组,26个位置,0代表a,以此类推。
然后统计杂志中也就是magazine中所有各个字符出现的次数。
然后在杂志中出现一个字符就减掉index中的字符次数,其实相当于 买东西 和 库存 的含义。如果小于零的话就证明那个字符不够用,返回false;
这里index不初始化的话就不行。。。(不知道为什么)
-----------------------------------------------------------------------
python:
#
# @lc app=leetcode.cn id=383 lang=python3
#
# [383] 赎金信
#
# https://leetcode-cn.com/problems/ransom-note/description/
#
# algorithms
# Easy (44.98%)
# Total Accepted: 5K
# Total Submissions: 11.1K
# Testcase Example: '"a"\n"b"'
#
# 给定一个赎金信 (ransom)
# 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true
# ;否则返回 false。
#
# (题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)
#
# 注意:
#
# 你可以假设两个字符串均只含有小写字母。
#
#
# canConstruct("a", "b") -> false
# canConstruct("aa", "ab") -> false
# canConstruct("aa", "aab") -> true
#
#
#
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
return all(ransomNote.count(c)<=magazine.count(c) for c in string.ascii_lowercase)
python粗暴式解法。
Leecode刷题之旅-C语言/python-383赎金信的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
		
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
 - Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
		
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
 - Leecode刷题之旅-C语言/python-28.实现strstr()
		
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
 - Leecode刷题之旅-C语言/python-7.整数反转
		
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
 - Leecode刷题之旅-C语言/python-434 字符串中的单词数
		
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
 - Leecode刷题之旅-C语言/python-326 3的幂
		
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
 - Leecode刷题之旅-C语言/python-263丑数
		
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
 - Leecode刷题之旅-C语言/python-349两整数之和
		
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
 - Leecode刷题之旅-C语言/python-349两个数组的交集
		
/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...
 
随机推荐
- C# 委托/Func() 中 GetInvocationList() 方法的使用 | 接收委托多个返回值
			
在日常使用委托时,有以下常用方法 方法名称 说明 Clone 创建委托的浅表副本. GetInvocationList 按照调用顺序返回此多路广播委托的调用列表. GetMethodIm ...
 - 连续支付的年金(continuously payable annuity)
			
一.含义 假设连续不断地付款,但每年的付款总量仍然为1元. 二. 连续支付年金是年支付次数m趋于无穷大时的年金,故 连续支付年金与基本年金的关系: 连续支付,每年的支付总量为1,支付期限为无穷: 积累 ...
 - [EffectiveC++]item23:Prefer non-member non-friend functions to member functions
			
99页 导致较大封装性的是non-member non-friend函数,因为它并不增加“能否访问class内之private成分”的函数数量.
 - require.js+backbone 使用r.js 在本地与生产环境 一键压缩的实现方式
			
require.js+backbone 使用r.js 在本地与生产环境 一键压缩的实现方式 时间:2017-07-03 17:18:11 阅读:210 评论:0 收藏:0 ...
 - 原生js模仿jq  fadeIn  fadeOut效果 兼容IE低版本
			
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
 - Java虚拟机11:内存分配原则
			
前言 JVM的自动内存管理要自动化的解决两个问题:对象分配内存以及回收分配给对象的内存.对象的内存分配一般是指在堆上分配,少数情况下也可能会直接分配在老年代上,对象主要分配在新生代的Eden 区上,如 ...
 - iOS动画暂停和继续-本质是速度控制和时间坐标转换
			
时间永不停止! 写一个CALayer的分类,控制动画的暂停与继续 extension CALayer { ///暂停动画 func pauseAnimation() { //取出当前时间,转成动画暂停 ...
 - apache2 重启、停止、优雅重启、优雅停止
			
停止或者重新启动Apache有两种发送信号的方法 第一种方法: 直接使用linux的kill命令向运行中的进程发送信号.你也许你会注意到你的系统里运行着很多httpd进程.但你不应该直接对它们中的任何 ...
 - 【jQuery】jQuery与Ajax的应用
			
1.demo1 <script language="javascript" type="text/javascript"> //通过这个函数来异步获 ...
 - AttributeError: 'module' object has no attribute get'
			
最近在写python requests相关内容易,突然报错AttributeError: 'module' object has no attribute 'get'" 脚本肯定没问题 怎么 ...