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 ...
随机推荐
- jquery遍历之children()与find()的区别
hildren(selector) 方法是返回匹配元素集合中每个元素的所有子元素(仅儿子辈).参数可选,添加参数表示通过选择器进行过滤,对元素进行筛选. .find(selector)方法是返回匹配元 ...
- 卸载Sharepoint2016后。重新安装提示 系统从以前的安装重新启动,或更新正在等待错误
卸载Sharepoint2016 重启N遍,不停地重启.需要删除注册表项 下的 .将PendingFileRenameOperations键项删除,再重新安装就可以安装成功.
- January 08 2017 Week 2nd Sunday
Believe not all that you see nor half what you hear. 眼见的不能全信,耳闻的也不能半信. What you hear, what you see, ...
- December 25th 2016 Week 53rd Sunday
Patience is bitter, but its fruit is sweet. 忍耐是痛苦的,但它的果实是甜蜜的. What can we do if there is no fruit of ...
- 无法打开项目文件“Web.csproj” 此安装不支持该项目类型
今天把另外一个项目拷贝到我自己的机器上运行,发现突然打不开webapplication项目了,提示:无法打开项目文件“Web.csproj” 此安装不支持该项目类型.,就是那个网站,用度娘网上搜了一大 ...
- Java 集合框架(常用数据结构)
早在Java 2中之前,Java就提供了特设类.比如:向量(Vector).栈(Stack).字典(Dictionary).哈希表(Hashtable)这些类(数据结构)用来存储和操作对象组.虽然这些 ...
- 【HNOI2009】梦幻布丁
题目描述 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. 输入输出格式 输入格式 第一 ...
- TensorFlow函数(五)参数初始化方法
1.初始化为常量 tf.constant_initializer(value, dtype) 生成一个初始值为常量value的tensor对象 value:指定的常量 dtype:数据类型 tf.ze ...
- 深入学习c++--重新审视auto
1. auto推断变量类型 2. auto遍历 3. 自定义类可使用auto
- Redis的安装和部署(windows )
Redis是一个开源的试用ANSI C语言编写的.遵守BSD协议.支持网络.可基于内存可持久化的日志型.key-value数据库.通常被称为数据结构服务器. redis的数据类型有:字符串(strin ...