mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
update 0383.赎金信: 完善注释
This commit is contained in:
@ -147,11 +147,11 @@ class Solution:
|
||||
|
||||
arr = [0] * 26
|
||||
|
||||
for x in magazine:
|
||||
for x in magazine: # 记录 magazine里各个字符出现次数
|
||||
arr[ord(x) - ord('a')] += 1
|
||||
|
||||
for x in ransomNote:
|
||||
if arr[ord(x) - ord('a')] == 0:
|
||||
for x in ransomNote: # 在arr里对应的字符个数做--操作
|
||||
if arr[ord(x) - ord('a')] == 0: # 如果没有出现过直接返回
|
||||
return False
|
||||
else:
|
||||
arr[ord(x) - ord('a')] -= 1
|
||||
@ -234,12 +234,12 @@ Go:
|
||||
```go
|
||||
func canConstruct(ransomNote string, magazine string) bool {
|
||||
record := make([]int, 26)
|
||||
for _, v := range magazine {
|
||||
for _, v := range magazine { // 通过recode数据记录 magazine里各个字符出现次数
|
||||
record[v-'a']++
|
||||
}
|
||||
for _, v := range ransomNote {
|
||||
for _, v := range ransomNote { // 遍历ransomNote,在record里对应的字符个数做--操作
|
||||
record[v-'a']--
|
||||
if record[v-'a'] < 0 {
|
||||
if record[v-'a'] < 0 { // 如果小于零说明ransomNote里出现的字符,magazine没有
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -258,12 +258,12 @@ javaScript:
|
||||
var canConstruct = function(ransomNote, magazine) {
|
||||
const strArr = new Array(26).fill(0),
|
||||
base = "a".charCodeAt();
|
||||
for(const s of magazine) {
|
||||
for(const s of magazine) { // 记录 magazine里各个字符出现次数
|
||||
strArr[s.charCodeAt() - base]++;
|
||||
}
|
||||
for(const s of ransomNote) {
|
||||
for(const s of ransomNote) { // 对应的字符个数做--操作
|
||||
const index = s.charCodeAt() - base;
|
||||
if(!strArr[index]) return false;
|
||||
if(!strArr[index]) return false; // 如果没记录过直接返回false
|
||||
strArr[index]--;
|
||||
}
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user