当前位置:网站首页>[daily 3 questions (1)] the second largest number in the string
[daily 3 questions (1)] the second largest number in the string
2022-06-28 16:58:00 【Programmed ape without hair loss 2】
subject :
Here's a mixed string s , Please return s in Second largest The number of , If there is no second largest number , Please return -1 .
Mixed string It's made up of small letters and numbers .
Example 1:
Input :s = “dfa12321afd”
Output :2
explain : Appear in the s The numbers in include [1, 2, 3] . The second largest number is 2 .
Example 2:
Input :s = “abc1111”
Output :-1
explain : Appear in the s The number in contains only [1] . There is no second largest number .
Tips :
1 <= s.length <= 500
s Only lowercase letters and ( or ) Numbers .
Ideas :
This question is relatively simple , Direct traversal , Use two variables to record the 1 And the first 2 A large number is enough .
java Code :
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;
}
}
边栏推荐
- On the design principle of price discount in SAP software
- 软件快速交付真的需要以安全为代价吗?
- GCC efficient graph revolution for joint node representationlearning and clustering
- Cross cluster deployment of helm applications using karmada
- PotPlayer播放百度云盘视频
- 【Hot100】3. Longest substring without duplicate characters
- 【208】基于AccessToken方式实现API设计
- 3. caller service call - dapr
- [daily 3 questions (2)] maximum ascending subarray sum
- WPF video hard decoding, rendering and playing (no airspace) (support 4K, 8K and high frame rate video)
猜你喜欢

Flex布局

Cross cluster deployment of helm applications using karmada

After the first failure, AMEC rushed to the Hong Kong stock exchange for the second time, and the financial principal changed frequently

使用 Open Connector 进行 HubSpot 和 SAP 系统的集成工作

This simple little function saves 213 hours for our production research team in half a year

基数排序——【常见排序法(2/8)】

You have a chance to collect wool. Click "earn" and you will have a chance to earn a high commission

Csp-j1 csp-s1 preliminary training plan and learning points in summer and September 2022

The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure

7-用户输入和while循环
随机推荐
It's completely cold! Tencent's well-known software was taken off the shelves, and netizens were all sobbing...
3. caller service call - dapr
Tacacs+服务器部署
Must the database primary key be self incremented? What scenarios do not suggest self augmentation? ByteDance experience sharing using Flink state 𞓜 afternoon tea with sauce issue 16
Gartner announces five privacy trends from today to 2024
Batchnorm2d principle, function and explanation of batchnorm2d function parameters in pytorch
Noip popularization group 2006-2018 preliminary round 2019 csp-j1 2020 csp-j1 improvement program
StackOverflow 2022 开发者报告:PostgreSQL 超越 MySQL !
[tcaplusdb knowledge base] view tcapdir directory server
Subscription publishing mode bus in JS
硕士学位论文写作探索学习
【TcaplusDB知识库】TcaplusDB限制条件介绍
[golang] how to install iris
如何在网站上安装 WordPress
opencv 读取图片详解
【TcaplusDB知识库】WebClient用户如何读取和修改数据
Inspur network wins step by step
中能融合携手天翼云打造“能源大脑”
General solution of island problems and DFS framework
【TcaplusDB知识库】查看tcapdir目录服务器