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

方法一:遍历

时间复杂度 $O(n)$,空间复杂度 $O(1)$。

func secondHighest(s string) int {
	nums := [10]bool{}
	for i := range s {
		if s[i] >= '0' && s[i] <= '9' {
			nums[s[i]-'0'] = true
		}
	}
	c := 0
	for i := 9; i >= 0; i-- {
		if nums[i] {
			c++
		}
		if c == 2 {
			return i
		}
	}
	return -1
}