-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path299.猜数字游戏.js
More file actions
36 lines (34 loc) · 924 Bytes
/
299.猜数字游戏.js
File metadata and controls
36 lines (34 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* @lc app=leetcode.cn id=299 lang=javascript
*
* [299] 猜数字游戏
*/
// @lc code=start
/**
* @param {string} secret
* @param {string} guess
* @return {string}
*/
var getHint = function (secret, guess) {
let a = 0; // 公牛
let b = 0; // 奶牛
const sMap = new Map();
const gMap = new Map();
for (let i = 0; i < secret.length; i++) {
// 每个位置相等时直接记录公牛
if (secret[i] === guess[i]) {
a++;
} else {
sMap.set(secret[i], (sMap.get(secret[i]) || 0) + 1);
gMap.set(guess[i], (gMap.get(guess[i]) || 0) + 1);
}
}
// 只由数字组成,最多十个
for (let i = 0; i < 10; i++) {
let s = sMap.get(i + "") || 0; // 不存在的数字 记0
let g = gMap.get(i + "") || 0;
b += Math.min(s, g); // 哪个数字出现的次数少,这个数字一定可以转换成公牛数字。
}
return a + "A" + b + "B";
};
// @lc code=end