Given an alphanumeric string
s
, return the second largest numerical digit that appears ins
, or-1
if it does not exist.An alphanumeric string is a string consisting of lowercase English letters and digits.
快快学完今天的,然后去做菜
思路:遍历字符串,使用变量记录字符串中第一大firstHfirstHfirstH和第二大的整数值secondHsecondHsecondH,当遇到一个整数numnumnum时进行判断
实现
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;}
}
复杂度