-
Notifications
You must be signed in to change notification settings - Fork 2
/
1796.字符串中第二大的数字.html
64 lines (54 loc) · 1.71 KB
/
1796.字符串中第二大的数字.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
<!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>1796. 字符串中第二大的数字</title>
</head>
<body>
<script>
// https://leetcode.cn/problems/second-largest-digit-in-a-string/
// 给你一个混合字符串 s ,请你返回 s 中 第二大 的数字,如果不存在第二大的数字,请你返回 -1 。
// 混合字符串 由小写英文字母和数字组成。
// 提示:
// 1 <= s.length <= 500
// s 只包含小写英文字母和(或)数字。
/**
* @param {string} s
* @return {number}
*/
var secondHighest = function (s) {};
// --- answer-1 ---
var secondHighest = function (s = '') {
let one = -1;
let two = -1;
for (let c of s) {
if (/\d/.test(c)) {
const n = Number(c);
if (n > one) {
two = one;
one = n;
}
if (n !== one && n > two) {
two = n;
}
}
}
return two;
};
// --- answer-1 ---
// --- answer-2 ---
// --- answer-2 ---
var s = 'dfa12321afd';
var result = 2;
// 解释:出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。
var s = 'abc1111';
var result = -1;
// 解释:出现在 s 中的数字只包含 [1] 。没有第二大的数字。
console.log('s = ', s);
console.log('result = ', result);
console.log('secondHighest = ', secondHighest(s));
</script>
</body>
</html>