Problem Description
Give you a mixed string s
, please return the second largest number in s
, if there is no second largest number, please return -1
.
Mixed strings consist of lowercase English letters and numbers.
Example 1:
输入:s = "dfa12321afd"
输出:2
解释:出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。
Example 2:
输入:s = "abc1111"
输出:-1
解释:出现在 s 中的数字只包含 [1] 。没有第二大的数字。
Leetcode original title address: https://leetcode.cn/problems/second-largest-digit-in-a-string
Thought analysis
The first way is to traverse and then deduplicate and then sort to get the last second largest number
First of all, in addition to numeric characters, there are also alphabetic characters in the string. So when we traverse, we only need to keep the numeric characters and ignore the alphabetic characters. Here use the
charCodeAt
method of the string to make a conversion judgment. String 0 to string 9 correspond to Unicode respectively:console.log( '0'.charCodeAt() ) // 48 console.log( '1'.charCodeAt() ) // 49 ...... // 依次递增 console.log( '8'.charCodeAt() ) // 56 console.log( '9'.charCodeAt() ) // 57
- After excluding alphabetic characters, each numeric character obtained by traversing can be appended to the array. In this way, all the numeric characters are recorded in the array
- Then do a deduplication of the number characters in the array, after deduplication, do a sort again, and then take the second largest number. code show as below:
var secondHighest = function (s) {
let arr = [] // 1. 定义一个空数组,用于存储
for (let i = 0; i < s.length; i++) { // 2. 遍历,并剔除字母字符,只保留数字字符
let item = s[i]
if (item.charCodeAt() >= 48 & item.charCodeAt() <= 57) { // charCodeAt()方法过滤
arr.push(item)
}
}
let newArr = Array.from(new Set([...arr])) // 3. 去个重
newArr.sort((a, b) => { // 4. 排个序
return b - a
})
return newArr[1] ? newArr[1] : -1 // 5. 有第二项,就返回第二项,没有就返回-1
};
The second way is to define two variables to represent the first and second largest numbers and update them iteratively.
- Define two variables to represent the first and second largest numbers
- The first and second largest numbers are continuously updated in the traversal
This kind of definition variable is constantly compared and compared through traversal, and the operation force buckle has a similar problem
- For example: the third largest number in force deduction
- Combined with the author's previous articles, it is helpful for a higher understanding: https://segmentfault.com/a/1190000042188016
- The code for this question is as follows:
var secondHighest = function (s) {
let firstMax = -Infinity // 1. 定义一个最大的数变量(大当家)
let secondMax = -Infinity // 2. 定义一个第二大的数变量(二当家)
for (let i = 0; i < s.length; i++) { // 3. 遍历操作
let item = s[i]
if (item.charCodeAt() >= 48 & item.charCodeAt() <= 57) { // 4. 只对数字字符做操作
if (item > firstMax) { // 5. 若新来的这个比大当家还大
secondMax = firstMax // 6. 那么大当家只能屈尊成为二当家了
firstMax = item // 7. 因为新来的成为大当家了
} else if (item == firstMax) { // 8. 若新来的和大当家一样,那就不变吧,忽略之
} else if (item > secondMax) { // 9. 若新来的比二当家大(但是没有大当家大)
secondMax = item // 10. 那大当家就不用换,只需新来的成为二当家即可
}
}
}
// 11. 最后把 二当家返回出去(注意要做一个判断,因为有可能secondMax还是-Infinity)
return secondMax == -Infinity ? -1 : secondMax
};
Summarize
After brushing a few more questions, you will find that this question seems to have been done before.
Yes, so when we brush the questions, pay attention to the classification of the questions, which will get twice the result with half the effort.
And for front-end students, you can give priority to the topics of string and array classification (because the front-end of binary trees and linked lists will be slightly less used)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。