首页 > 其他分享 >1796.second-largest-digit-in-a-string 字符串中第二大的数字

1796.second-largest-digit-in-a-string 字符串中第二大的数字

时间:2022-12-06 20:03:22浏览次数:75  
标签:digit string int 1796 second 字符串 first

问题描述

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

解题思路

遍历就好了

代码

class Solution {
public:
    int secondHighest(string s) {
        int first = -1;
        int second = -1;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] <= '9') {
                if (s[i] - '0' > first) {
                    second = first;
                    first = s[i] - '0';
                } else if (s[i] - '0' < first && s[i] - '0' > second)
                    second = s[i] - '0';
            }
        }
        return second;
    }
};

标签:digit,string,int,1796,second,字符串,first
From: https://www.cnblogs.com/zwyyy456/p/16960338.html

相关文章