当前位置:网站首页>【每日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;
}
}
边栏推荐
- Written interview algorithm classic - longest palindrome substring
- O & M - unified gateway is very necessary
- 3. Caller 服务调用 - dapr
- Design details of the full stack CRM development tool webclient UI workbench
- 2019 CSP J2 entry group csp-s2 improvement group round 2 video and question solution
- 如何让你的 WordPress 网站更安全
- 【TcaplusDB】祝大家端午安康!
- On the design principle of price discount in SAP software
- 【尚硅谷与腾讯云官方合作】硅谷课堂项目视频发布
- 2022年暑期及9月份CSP-J1 CSP-S1初赛 培训计划及学习要点
猜你喜欢

Internet of things cloud convergence Security Guide

关于接口测试自动化的总结与思考

批量修改指定字符文件名 bat脚本

FS2K人脸素描属性识别

岛屿类问题通用解法与DFS框架

The future of platform as code is kubernetes extension

10.Hystrix断路器

On the design principle of price discount in SAP software

MATLB|电力系统优化运行与市场化

Super automation and the future of network security
随机推荐
免费、强大、高颜值的笔记软件评测: OneNote、Heptabase、氢图、FlowUs
[force button] 35 Search insert location
Convolutional neural network for machine learning uses cifar10 data set and alexnet network model to train classification model, install labelimg, and report error
【Hot100】4. Find the median of two positive arrays
Steps to be taken for successful migration to the cloud
MATLB|可视化学习(plot和bar)
Cardinality sorting - common sorting method (2/8)
CRM 全栈开发工具 WebClient UI Workbench 的设计细节介绍
Lenet5 training model of convolutional neural network for machine learning
R 编程语言 - 简介
NOIP2011-2018提高组解题报告
【TcaplusDB知识库】TcaplusDB技术支持介绍
中金证券经理给的开户链接安全吗?找谁可以开户啊?
Stm32cubemx usage and function introduction
Can SQL queries be used in the tablestore to find out all the data in the table?
怎么期货开户?在哪里期货开户比较安全?
GCC efficient graph revolution for joint node representationlearning and clustering
Practice of curve replacing CEPH in Netease cloud music
NOIP1998-2018年普及组 CSP-J2 2019 2020 解题报告及视频
Batchnorm2d principle, function and explanation of batchnorm2d function parameters in pytorch