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

@ -1660,7 +1660,7 @@
|1519|Number of Nodes in the Sub-Tree With the Same Label||38.2%|Medium||
|1520|Maximum Number of Non-Overlapping Substrings||37.0%|Hard||
|1521|Find a Value of a Mysterious Function Closest to Target||43.8%|Hard||
|1522|Diameter of N-Ary Tree||70.6%|Medium||
|1522|Diameter of N-Ary Tree||70.7%|Medium||
|1523|Count Odd Numbers in an Interval Range||53.9%|Easy||
|1524|Number of Sub-arrays With Odd Sum||41.5%|Medium||
|1525|Number of Good Ways to Split a String||69.7%|Medium||
@ -1900,7 +1900,7 @@
|1759|Count Number of Homogenous Substrings||44.0%|Medium||
|1760|Minimum Limit of Balls in a Bag||54.5%|Medium||
|1761|Minimum Degree of a Connected Trio in a Graph||39.5%|Hard||
|1762|Buildings With an Ocean View||81.3%|Medium||
|1762|Buildings With an Ocean View||81.4%|Medium||
|1763|Longest Nice Substring||61.5%|Easy||
|1764|Form Array by Concatenating Subarrays of Another Array||53.1%|Medium||
|1765|Map of Highest Peak||57.6%|Medium||

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
}

View File

@ -72,47 +72,21 @@ 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)
}
```