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 ...
随机推荐
- [翻译] BBCyclingLabel
BBCyclingLabel BBCyclingLabel is just like a UILabel but allows you to perform custom animations whe ...
- 排序算法(1) 快速排序 C++实现
快速排序基本特性 时间复杂度:O(n*lgn) 最坏:O(n^2) 空间复杂度:最好情况下:O(lgn),最坏情况:O(n),平均情况:O(lgn) 不稳定. 关于快速排序的空间复杂度,谢谢@命运他爹 ...
- Python学习---重点模块的学习【all】
time [时间模块] import time # print(help(time)) # time模块的帮助 print(time.time()) # 时间戳 print(time.cloc ...
- 将旧的时间字符串转换为新的string时间字符串
旧的时间字符串-->simpledataformat1.parse(该字符串) 获得date类型 -->simpledataformat2.format(date) simpledatef ...
- Bearer Token的加密解密规则(OAuth中间件)
在OAuthBearerAuthenticationMiddleware中使用Microsoft.Owin.Security.DataHandler. SecureDataFormat<TDat ...
- nginx+php+swoole安装记录
领了台阿里服务器1vCPU 1G,做下测试研究. 系统 centos7,使用yum安装. Nginx yum install nginx ##开启nginx service nginx start 安 ...
- stderr: xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
错误提示: (1). stderr: xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer direc ...
- U-Mail:邮件营销如何大量获取并筛选有效地址
工欲善其事必先利其器,在所有的邮件推广中,最基础的工作就是收集到足够多的有效邮箱地址,俗话说韩信将兵多多益善,邮箱地址越多,也就意味着潜在的转化为消费者的数量众多,但是如果大量滥竽充数的地址被搜罗进来 ...
- pip-修改为国内镜像源
pip 常用命令 pip install ./downloads/SomePackage-1.0.4.tar.gz pip install http://my.package.repo/SomePac ...
- Hadoop学习之路(二十五)MapReduce的API使用(二)
学生成绩---增强版 数据信息 computer,huangxiaoming,85,86,41,75,93,42,85 computer,xuzheng,54,52,86,91,42 computer ...