Leetcode Note: Go - Second Largest Digit in a String

Second Largest Digit in a String - LeetCode
https://leetcode.com/problems/second-largest-digit-in-a-string/

  • Go 言語で取り組んだメモ

回答

Go O(n) with 2 ints memory - Second Largest Digit in a String - LeetCode
https://leetcode.com/problems/second-largest-digit-in-a-string/solutions/1124532/go-o-n-with-2-ints-memory/

func secondHighest(s string) int {
    first := -1
    second := -1
    for _,c := range s {
        if unicode.IsDigit(c) {
            digit := int(c - '0')
            if digit > first {
                first, second = digit, first
            } else if digit > second && digit != first {
                second = digit
            }
        }
    }
    return second
}