给你一个混合字符串 s ,请你返回 s 中 第二大 的数字,如果不存在第二大的数字,请你返回 -1 。
混合字符串 由小写英文字母和数字组成。
示例 1:
输入:s = “dfa12321afd”
输出:2
解释:出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。
示例 2:
输入:s = “abc1111”
输出:-1
解释:出现在 s 中的数字只包含 [1] 。没有第二大的数字。
提示:
1 <= s.length <= 500
s 只包含小写英文字母和(或)数字。
https://leetcode.cn/problems/second-largest-digit-in-a-string/
class Solution {
public:int secondHighest(string s) {vector nums;for(int i=0;i
时间复杂度:O(n)
空间复杂度:O(n)
class Solution {
public:int secondHighest(string s) {set a;for(auto c: s)if(isdigit(c)) a.insert(c-'0');if(a.size()<2)return -1;auto it=a.rbegin();return *++it;}
};
时间复杂度:O(n)
空间复杂度:O(n)
class Solution {
public:int secondHighest(string s) {int first = -1, second = -1;for (auto c : s) {if (isdigit(c)) {int num = c - '0';if (num > first) {second = first;first = num;} else if (num < first && num > second) {second = num;}}}return second;}
};
时间复杂度:O(n)
空间复杂度:O(1)