0
点赞
收藏
分享

微信扫一扫

「 每日一练,快乐水题 」1796. 字符串中第二大的数字


文章目录

  • ​​🔴力扣原题:​​
  • ​​🟠题目简述:​​
  • ​​🟡解题思路:​​
  • ​​🟢C++代码:​​
  • ​​🔵结果展示:​​

🔴力扣原题:

​​1796. 字符串中第二大的数字​​

🟠题目简述:

给你一个混合字符串 s ,请你返回 s 中 第二大 的数字,如果不存在第二大的数字,请你返回 -1 。

混合字符串 由小写英文字母和数字组成。

🟡解题思路:

  1. 使用​​isdigit​​函数筛选出数字;
  2. 使用​​set​​对数字进行排序;
  3. over;

🟢C++代码:

class Solution {
public:
int secondHighest(string s) {
int ret = -1;
set<int> st;

for (auto c : s)
{
if (isdigit(c))
{
int num = c - '0';

st.emplace(num);
}
}

if(st.size() >= 2)
{
auto it = st.end();
--it;
ret = *(--it);
}

return ret;
}
};

🔵结果展示:

「 每日一练,快乐水题 」1796. 字符串中第二大的数字_git


举报

相关推荐

0 条评论