mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 18:34:42 +01:00
20201226
This commit is contained in:
54
20201226/main.go
Normal file
54
20201226/main.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func findThirdMax(nums []int) int {
|
||||
if len(nums) == 0 {
|
||||
return 0
|
||||
}
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
|
||||
if len(nums) < 3 {
|
||||
return nums[0]
|
||||
}
|
||||
v := nums[0]
|
||||
count := 0
|
||||
for _, n := range nums {
|
||||
v = n
|
||||
if count == 3 {
|
||||
break
|
||||
}
|
||||
if v > n {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func findThirdMax2(nums []int) int {
|
||||
vs := [3]int{math.MinInt64, math.MinInt64, math.MinInt64}
|
||||
found := false
|
||||
for _, n := range nums {
|
||||
if n == vs[0] || n == vs[1] || n == vs[2] {
|
||||
continue
|
||||
}
|
||||
if n > vs[0] {
|
||||
vs[0], vs[1], vs[2] = n, vs[0], vs[1]
|
||||
} else if n > vs[1] {
|
||||
vs[1], vs[2] = n, vs[1]
|
||||
} else if n > vs[2] {
|
||||
found = true
|
||||
vs[2] = n
|
||||
}
|
||||
}
|
||||
if found {
|
||||
return vs[2]
|
||||
}
|
||||
return vs[0]
|
||||
}
|
||||
90
20201226/main_test.go
Normal file
90
20201226/main_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_findThirdMax(t *testing.T) {
|
||||
type args struct {
|
||||
nums []int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"in:[3,2,1],out:1",
|
||||
args{
|
||||
[]int{3, 2, 1},
|
||||
},
|
||||
1,
|
||||
},
|
||||
{
|
||||
"in:[1,2],out:2",
|
||||
args{
|
||||
[]int{1, 2},
|
||||
},
|
||||
2,
|
||||
},
|
||||
{
|
||||
"in:[2,2,3,1],out:1",
|
||||
args{
|
||||
[]int{2, 2, 3, 1},
|
||||
},
|
||||
1,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := findThirdMax(tt.args.nums); got != tt.want {
|
||||
t.Errorf("findThirdMax() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_findThirdMax2(t *testing.T) {
|
||||
type args struct {
|
||||
nums []int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"in:[3,2,1],out:1",
|
||||
args{
|
||||
[]int{3, 2, 1},
|
||||
},
|
||||
1,
|
||||
},
|
||||
{
|
||||
"in:[1,2],out:2",
|
||||
args{
|
||||
[]int{1, 2},
|
||||
},
|
||||
2,
|
||||
},
|
||||
{
|
||||
"in:[2,2,3,1],out:1",
|
||||
args{
|
||||
[]int{2, 2, 3, 1},
|
||||
},
|
||||
1,
|
||||
},
|
||||
{
|
||||
"in:[2,2,3],out:3",
|
||||
args{
|
||||
[]int{2, 2, 3},
|
||||
},
|
||||
3,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := findThirdMax2(tt.args.nums); got != tt.want {
|
||||
t.Errorf("findThirdMax() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user