当前位置:网站首页>【每日3题(1)】字符串中第二大的数字
【每日3题(1)】字符串中第二大的数字
2022-06-28 16:38:00 【程序猿不脱发2】
题目:
给你一个混合字符串 s ,请你返回 s 中 第二大 的数字,如果不存在第二大的数字,请你返回 -1 。
混合字符串 由小写英文字母和数字组成。
示例 1:
输入:s = “dfa12321afd”
输出:2
解释:出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。
示例 2:
输入:s = “abc1111”
输出:-1
解释:出现在 s 中的数字只包含 [1] 。没有第二大的数字。
提示:
1 <= s.length <= 500
s 只包含小写英文字母和(或)数字。
思路:
此题比较简单,直接遍历,用两个变量记录第1大和第2大的数即可。
java代码:
class Solution {
public int secondHighest(String s) {
int firstMax = -1;
int secondMax = -1;
for(int i=0;i<s.length();i++) {
char ch = s.charAt(i);
if('0'<=ch && ch<='9') {
int num = ch-'0';
if(firstMax<num) {
secondMax = firstMax;
firstMax = num;
}else if(firstMax>num && secondMax<num) {
secondMax = num;
}
}
}
return secondMax;
}
}
边栏推荐
- Yesterday, yuancosmos | meta's "yuancosmos" Department lost $2.96 billion in the first quarter, and Liufu jewelry issued Digital Collections
- 【TcaplusDB知识库】查看业务密码
- WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)
- The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
- Flex布局
- The intelligent transformation is accelerated, and enterprises need a new toolbox
- You have a chance to collect wool. Click "earn" and you will have a chance to earn a high commission
- 【Redis】2021/01/31 Redis的简单归纳 No.01
- The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure
- 【208】基于AccessToken方式实现API设计
猜你喜欢
MATLB|可视化学习(plot和bar)
使用Karmada实现Helm应用的跨集群部署
The future of platform as code is kubernetes extension
基数排序——【常见排序法(2/8)】
NOIP普及组2006-2018初赛 2019 CSP-J1 2020 CSP-J1 完善程序题
2022年暑期及9月份CSP-J1 CSP-S1初赛 培训计划及学习要点
Practice of curve replacing CEPH in Netease cloud music
After the first failure, AMEC rushed to the Hong Kong stock exchange for the second time, and the financial principal changed frequently
FS2K人脸素描属性识别
The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure
随机推荐
免费、强大、高颜值的笔记软件评测: OneNote、Heptabase、氢图、FlowUs
大型体育赛事与犯罪风险
Gartner announces five privacy trends from today to 2024
StackOverflow 2022 开发者报告:PostgreSQL 超越 MySQL !
【Golang】安装 iris 的方法
关于接口测试自动化的总结与思考
Summer Challenge ohos build custom service practice
天翼云Web应用防火墙(边缘云版)通过首批可信认证
QQ appears large-scale number theft, why is this? Is there no solution?
中能融合携手天翼云打造“能源大脑”
【Hot100】4. Find the median of two positive arrays
Internet of things cloud convergence Security Guide
Lucky draw animation - Carp jumps over the dragon's gate
Batchnorm2d principle, function and explanation of batchnorm2d function parameters in pytorch
【尚硅谷与腾讯云官方合作】硅谷课堂项目视频发布
6 - 字典
[tcapulusdb knowledge base] Introduction to tcapulusdb restrictions
7-用户输入和while循环
GCC efficient graph revolution for joint node representationlearning and clustering
Please ask me, the queries written in my database account for 99%. Is it better to use pay as you go mode or reservation mode?