java:
class Solution {
public int secondHighest(String s) {
int max = -1;
for (char ch : s.toCharArray()) {
if (Character.isDigit(ch)) {
max = Math.max(max, ch - '0');
}
}
int ans = -1;
for (char ch : s.toCharArray()) {
if (Character.isDigit(ch)) {
int a = ch - '0';
if (a < max && a > ans) {
ans = a;
}
}
}
return ans;
}
}
class Solution {
public int secondHighest(String s) {
int first = -1, second = -1;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
int num = c - '0';
if (num > first) { // 不断更新最大值
second = first;
first = num;
} else if (num < first && num > second) {
second = num;
}
}
}
return second;
}
}