Update solution 0242

This commit is contained in:
halfrost
2021-07-23 21:21:21 +08:00
parent 0547b8b075
commit 2f2ba72e92
6 changed files with 36 additions and 91 deletions

View File

@ -1,26 +1,5 @@
package leetcode
// suggestion
func isAnagram2(s string, t string) bool {
hash := map[rune]int{}
for _, value := range s {
hash[value]++
}
for _, value := range t {
hash[value]--
}
for _, value := range hash {
if value != 0 {
return false
}
}
return true
}
// 解法一
func isAnagram(s string, t string) bool {
alphabet := make([]int, 26)
@ -45,44 +24,17 @@ func isAnagram(s string, t string) bool {
// 解法二
func isAnagram1(s string, t string) bool {
if s == "" && t == "" {
return true
hash := map[rune]int{}
for _, value := range s {
hash[value]++
}
if s == "" || t == "" {
return false
for _, value := range t {
hash[value]--
}
sBytes := []byte(s)
tBytes := []byte(t)
if len(sBytes) != len(tBytes) {
return false
}
quickSortByte(sBytes, 0, len(sBytes)-1)
quickSortByte(tBytes, 0, len(tBytes)-1)
for i := 0; i < len(sBytes); i++ {
if sBytes[i] != tBytes[i] {
for _, value := range hash {
if value != 0 {
return false
}
}
return true
}
func partitionByte(a []byte, lo, hi int) int {
pivot := a[hi]
i := lo - 1
for j := lo; j < hi; j++ {
if a[j] > pivot {
i++
a[j], a[i] = a[i], a[j]
}
}
a[i+1], a[hi] = a[hi], a[i+1]
return i + 1
}
func quickSortByte(a []byte, lo, hi int) {
if lo >= hi {
return
}
p := partitionByte(a, lo, hi)
quickSortByte(a, lo, p-1)
quickSortByte(a, p+1, hi)
}

View File

@ -0,0 +1,19 @@
package leetcode
import "sort"
func minPairSum(nums []int) int {
sort.Ints(nums)
n, res := len(nums), 0
for i, val := range nums[:n/2] {
res = max(res, val+nums[n-1-i])
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}