首页 > 其他分享 >1796. 字符串中第二大的数字

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

时间:2022-12-03 03:11:06浏览次数:64  
标签:ch 数字 int 1796 && 字符串

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

class Solution {
    public int secondHighest(String s) {
        int max1 = -1;
        int max2 = -1;
        char[] ch = s.toCharArray();
        int n = ch.length;
        for (int i = 0; i < n; i++) {
            if (ch[i] >= '0' && ch[i] <= '9') {
                int num = ch[i] - '0';
                max1 = Math.max(max1, num);
            }
        }
        for (int i = 0; i < n; i++) {
            if (ch[i] >= '0' && ch[i] <= '9') {
                int num = ch[i] - '0';
                if (num < max1) {
                    max2 = Math.max(max2, num);
                }
            }
        }
        return max2;
    }
}

标签:ch,数字,int,1796,&&,字符串
From: https://www.cnblogs.com/eiffelzero/p/16946289.html

相关文章