当前位置:网站首页>[leetcode 383] ransom letter
[leetcode 383] ransom letter
2022-04-23 06:23:00 【Don't steal my energy】
Topic introduction
Here are two strings :ransomNote and magazine , Judge ransomNote Can it be done by magazine The characters inside make up .
If possible , return true ; Otherwise return to false .
magazine Each character in can only be in ransomNote Used once in .
Example 1
Input :ransomNote = "a", magazine = "b"
Output :false
Example 2
Input :ransomNote = "aa", magazine = "ab"
Output :false
Example 3
Input :ransomNote = "aa", magazine = "aab"
Output :true
Tips
- 1 <= ransomNote.length, magazine.length <= 105
- ransomNote and magazine It's made up of lowercase letters
Ideas
The idea and of this question 【LeetCode 350 Intersection of two arrays II】 10 Fen is the same , The core is to use the idea of hash table . take magazine The letters in and the number of times they appear are stored separately , And then go through ransomNote Letters in , Judge , If the number of occurrences >0, The number of times it appears -1, Traverse the next letter ; If the number of occurrences ==0, shows ransomNote Can it be done by magazine The characters inside make up , return flase.
1. One dimensional array implementation
bool canConstruct(string ransomNote, string magazine) {
int a[123]={
0};// Small letters z Of ascii value 122
for(char x:magazine)
a[int(x)]++;
for(char y:ransomNote){
if(a[int(y)]>0)
a[int(y)]--;
else
return 0;
}
return 1;
}
2. Hash table implementation
bool canConstruct(string ransomNote, string magazine){
unordered_map<char,int>a; // take magazine The letters in and their occurrences are stored separately
for(char x:magazine)
a[x]++;
for(char y:ransomNote){
if(a[y]>0)
a[y]--;
else
return 0;
}
return 1;
}
On memory consumption , The array implementation is lower than the hash table implementation .
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617012053.html
边栏推荐
- [transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
- Create enterprise mailbox account command
- Common sense of thread pool
- Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
- 检测技术与原理
- Failure to deliver XID in Seata distributed transaction project
- Kalman filter and inertial integrated navigation
- Algèbre linéaire chapitre 1 - déterminants
- Calculation (enter the calculation formula to get the result)
- LockSupport. Park and unpark, wait and notify
猜你喜欢
Filebrowser realizes private network disk
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察
深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
lambda expressions
Kalman filter and inertial integrated navigation
Contrôle automatique (version Han min)
Fundamentals of digital image processing (Gonzalez) I
In depth understanding of the relationship between dncblevel and noise denoising in the paper
随机推荐
Pytorch learning record (7): skills in processing data and training models
Advanced operation of idea debug
Framework analysis 2 Source code - login authentication
How does MySQL convert stored seconds into dates
St table template
Understanding and installing MySQL
Linear algebra Chapter 1 - determinant
Techniques et principes de détection
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
The bottom implementation principle of thread - static agent mode
Illustrate the significance of hashcode
Guaba and Computational Geometry
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
C language file operation
Plane semi intersecting plate
Substring Inversion (Easy Version)
Installation and usage skills of idea
Best practices for MySQL storage time
线性代数第三章-矩阵的初等变换与线性方程组
20 excellent plug-ins recommended by idea