mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-14 15:00:42 +08:00
添加预判断长度的代码
在magazine中字符存入数组前,预先判断ransomNote和magazine的相对长度,进行处理。
This commit is contained in:
@ -84,6 +84,10 @@ class Solution {
|
|||||||
public:
|
public:
|
||||||
bool canConstruct(string ransomNote, string magazine) {
|
bool canConstruct(string ransomNote, string magazine) {
|
||||||
int record[26] = {0};
|
int record[26] = {0};
|
||||||
|
//add
|
||||||
|
if (ransomNote.size() > magazine.size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
for (int i = 0; i < magazine.length(); i++) {
|
for (int i = 0; i < magazine.length(); i++) {
|
||||||
// 通过recode数据记录 magazine里各个字符出现次数
|
// 通过recode数据记录 magazine里各个字符出现次数
|
||||||
record[magazine[i]-'a'] ++;
|
record[magazine[i]-'a'] ++;
|
||||||
|
Reference in New Issue
Block a user