解法题目描述:
给你一个混合字符串 s ,请你返回 s 中 第二大 的数字,如果不存在第二大的数字,请你返回 -1 。
混合字符串 由小写英文字母和数字组成。
示例 :
输入:s = “dfa12321afd”
输出:2
解释:出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。
提示:
- 1 <= s.length <= 500
- s 只包含小写英文字母和(或)数字。
遍历即可。
代码class Solution:
def secondHighest(self, s: str) -> int:
a, b = -1, -1
for ch in s:
if ch.isdigit():
num = int(ch)
if num > a:
a, b = num, a
elif a > num > b:
b = num
return b
测试结果
说明执行用时:40 ms, 在所有 Python3 提交中击败了 48.73% 的用户
内存消耗:14.8 MB, 在所有 Python3 提交中击败了 93.82% 的用户
算法题来源:力扣(LeetCode)



