From 64282d58361c41581514e55021d0bde17977d2e9 Mon Sep 17 00:00:00 2001 From: David Feng Date: Sun, 24 Jul 2022 12:23:09 +0900 Subject: [PATCH] min max: change to generic version --- go/demo/leetcode.go | 15 +++++++-------- go/go.mod | 1 + 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/go/demo/leetcode.go b/go/demo/leetcode.go index abdd45b..fd81cee 100644 --- a/go/demo/leetcode.go +++ b/go/demo/leetcode.go @@ -3,6 +3,8 @@ package demo import ( "math/bits" "unicode/utf8" + + "golang.org/x/exp/constraints" ) func TwoSum(nums []int, target int) []int { @@ -157,19 +159,17 @@ func NumToList(n int) *ListNode { return head } -func Max(a, b int) int { - if a > b { - return a - } else { +func Max[T constraints.Ordered](a, b T) T { + if a < b { return b } + return a } -func Min(a, b int) int { +func Min[T constraints.Ordered](a, b T) T { if a > b { return b - } else { - return a } + return a } func LengthOfLongestSubstring(s string) int { @@ -254,7 +254,6 @@ func LongestPalindrome(s string) string { for i := l; i > 1; i-- { for a, b := 0, 0+i; b <= l; a, b = a+1, b+1 { - // fmt.Printf("substr is %s\n", s[a:b]) if IsPalindrome(s[a:b]) { return s[a:b] } diff --git a/go/go.mod b/go/go.mod index 8123c78..2e4df0a 100644 --- a/go/go.mod +++ b/go/go.mod @@ -7,6 +7,7 @@ require ( github.com/rivo/tview v0.0.0-20220610163003-691f46d6f500 github.com/spf13/cobra v1.5.0 github.com/spf13/viper v1.12.0 + golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e ) require (