Given an alphanumeric string
s, return the second largest numerical digit that appears ins, or-1if it does not exist.An alphanumeric string is a string consisting of lowercase English letters and digits.
快快学完今天的,然后去做菜
思路:遍历字符串,使用变量记录字符串中第一大 f i r s t H firstH firstH和第二大的整数值 s e c o n d H secondH secondH,当遇到一个整数 n u m num num时进行判断
实现
class Solution {
public int secondHighest(String s) {
int firstH = -1;
int secondH = -1;
for (char c : s.toCharArray()){
if (c >= '0' && c <= '9'){
int num = c -'0';
if (num > firstH) {
secondH = firstH;
firstH = num;
}else if (num < firstH && num > secondH) {
secondH = num;
}
}
}
return secondH;
}
}
复杂度