mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0383.赎金信.md
This commit is contained in:
@ -111,7 +111,31 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
public boolean canConstruct(String ransomNote, String magazine) {
|
||||||
|
//记录杂志字符串出现的次数
|
||||||
|
int[] arr = new int[26];
|
||||||
|
int temp;
|
||||||
|
for (int i = 0; i < magazine.length(); i++) {
|
||||||
|
temp = magazine.charAt(i) - 'a';
|
||||||
|
arr[temp]++;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < ransomNote.length(); i++) {
|
||||||
|
temp = ransomNote.charAt(i) - 'a';
|
||||||
|
//对于金信中的每一个字符都在数组中查找
|
||||||
|
//找到相应位减一,否则找不到返回false
|
||||||
|
if (arr[temp] > 0) {
|
||||||
|
arr[temp]--;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user