-
Notifications
You must be signed in to change notification settings - Fork 2
/
1880.检查某单词是否等于两单词之和.html
80 lines (68 loc) · 2.87 KB
/
1880.检查某单词是否等于两单词之和.html
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>1880. 检查某单词是否等于两单词之和</title>
</head>
<body>
<script>
// https://leetcode.cn/problems/check-if-word-equals-summation-of-two-words/
// 字母的 字母值 取决于字母在字母表中的位置,从 0 开始 计数。即,'a' -> 0、'b' -> 1、'c' -> 2,以此类推。
// 对某个由小写字母组成的字符串 s 而言,其 数值 就等于将 s 中每个字母的 字母值 按顺序 连接 并 转换 成对应整数。
// 提示:
// 例如,s = "acb" ,依次连接每个字母的字母值可以得到 "021" ,转换为整数得到 21 。
// 给你三个字符串 firstWord、secondWord 和 targetWord ,每个字符串都由从 'a' 到 'j' (含 'a' 和 'j' )的小写英文字母组成。
// 如果 firstWord 和 secondWord 的 数值之和 等于 targetWord 的数值,返回 true ;否则,返回 false 。
// 提示:
// 1 <= firstWord.length, secondWord.length, targetWord.length <= 8
// firstWord、secondWord 和 targetWord 仅由从 'a' 到 'j' (含 'a' 和 'j' )的小写英文字母组成。
/**
* @param {string} firstWord
* @param {string} secondWord
* @param {string} targetWord
* @return {boolean}
*/
var isSumEqual = function (firstWord, secondWord, targetWord) {};
// --- answer-1 ---
function convertNum(words = '') {
if (!words) return 0;
return Number(
words
.split('')
.map((s) => (s.charCodeAt() - 'a'.charCodeAt()).toString())
.join('')
);
}
// 数字不会超过最大安全数 直接字符串转数字计算
// 如果超过最大安全数 则需要字符串模拟
var isSumEqual = function (firstWord, secondWord, targetWord) {
return convertNum(firstWord) + convertNum(secondWord) === convertNum(targetWord);
};
// --- answer-1 ---
// --- answer-2 ---
// --- answer-2 ---
var firstWord = 'acb',
secondWord = 'cba',
targetWord = 'cdb';
var result = true;
// 解释:
var firstWord = 'aaa',
secondWord = 'a',
targetWord = 'aab';
var result = false;
// 解释:
var firstWord = 'aaa',
secondWord = 'a',
targetWord = 'aaaa';
var result = true;
// 解释:
console.log('firstWord = ', firstWord);
console.log('secondWord = ', secondWord);
console.log('targetWord = ', targetWord);
console.log('result = ', result);
console.log('isSumEqual = ', isSumEqual(firstWord, secondWord, targetWord));
</script>
</body>
</html>