-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1796.go
More file actions
45 lines (39 loc) · 940 Bytes
/
solution1796.go
File metadata and controls
45 lines (39 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package solution1796
import (
"slices"
)
// ============================================================================
// 1796. Second Largest Digit in a String
// URL: https://leetcode.com/problems/second-largest-digit-in-a-string/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/1796---Second-Largest-Digit-in-a-String
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_secondHighest
Benchmark_secondHighest-24 13378276 82.89 ns/op 56 B/op 3 allocs/op
PASS
*/
func secondHighest(s string) int {
val := 0
st := make([]int, 0)
outer:
for _, ru := range s {
ch := byte(ru)
if '0' <= ch && ch <= '9' {
val = int(ch - 48)
for _, v := range st {
if v == val {
continue outer
}
}
st = append(st, int(ch-48))
}
}
slices.Reverse(st)
if len(st) > 1 {
return st[1]
}
return -1
}