Skip to content

Commit

Permalink
min max: change to generic version
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxifeng committed Jul 24, 2022
1 parent 6d50878 commit 64282d5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
15 changes: 7 additions & 8 deletions go/demo/leetcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package demo
import (
"math/bits"
"unicode/utf8"

"golang.org/x/exp/constraints"
)

func TwoSum(nums []int, target int) []int {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
}
Expand Down
1 change: 1 addition & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 64282d5

Please sign in to comment.