mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
Update solution 0242
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
Reference in New Issue
Block a user