Skip to content

Commit 2f2ba72

Browse files
committed
Update solution 0242
1 parent 0547b8b commit 2f2ba72

File tree

6 files changed

+36
-91
lines changed

6 files changed

+36
-91
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@
16601660
|1519|Number of Nodes in the Sub-Tree With the Same Label||38.2%|Medium||
16611661
|1520|Maximum Number of Non-Overlapping Substrings||37.0%|Hard||
16621662
|1521|Find a Value of a Mysterious Function Closest to Target||43.8%|Hard||
1663-
|1522|Diameter of N-Ary Tree||70.6%|Medium||
1663+
|1522|Diameter of N-Ary Tree||70.7%|Medium||
16641664
|1523|Count Odd Numbers in an Interval Range||53.9%|Easy||
16651665
|1524|Number of Sub-arrays With Odd Sum||41.5%|Medium||
16661666
|1525|Number of Good Ways to Split a String||69.7%|Medium||
@@ -1900,7 +1900,7 @@
19001900
|1759|Count Number of Homogenous Substrings||44.0%|Medium||
19011901
|1760|Minimum Limit of Balls in a Bag||54.5%|Medium||
19021902
|1761|Minimum Degree of a Connected Trio in a Graph||39.5%|Hard||
1903-
|1762|Buildings With an Ocean View||81.3%|Medium||
1903+
|1762|Buildings With an Ocean View||81.4%|Medium||
19041904
|1763|Longest Nice Substring||61.5%|Easy||
19051905
|1764|Form Array by Concatenating Subarrays of Another Array||53.1%|Medium||
19061906
|1765|Map of Highest Peak||57.6%|Medium||
Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
11
package leetcode
22

3-
// suggestion
4-
func isAnagram2(s string, t string) bool {
5-
hash := map[rune]int{}
6-
7-
for _, value := range s {
8-
hash[value]++
9-
}
10-
11-
for _, value := range t {
12-
hash[value]--
13-
}
14-
15-
for _, value := range hash {
16-
if value != 0 {
17-
return false
18-
}
19-
}
20-
21-
return true
22-
}
23-
243
// 解法一
254
func isAnagram(s string, t string) bool {
265
alphabet := make([]int, 26)
@@ -45,44 +24,17 @@ func isAnagram(s string, t string) bool {
4524

4625
// 解法二
4726
func isAnagram1(s string, t string) bool {
48-
if s == "" && t == "" {
49-
return true
50-
}
51-
if s == "" || t == "" {
52-
return false
27+
hash := map[rune]int{}
28+
for _, value := range s {
29+
hash[value]++
5330
}
54-
sBytes := []byte(s)
55-
tBytes := []byte(t)
56-
if len(sBytes) != len(tBytes) {
57-
return false
31+
for _, value := range t {
32+
hash[value]--
5833
}
59-
quickSortByte(sBytes, 0, len(sBytes)-1)
60-
quickSortByte(tBytes, 0, len(tBytes)-1)
61-
62-
for i := 0; i < len(sBytes); i++ {
63-
if sBytes[i] != tBytes[i] {
34+
for _, value := range hash {
35+
if value != 0 {
6436
return false
6537
}
6638
}
6739
return true
6840
}
69-
func partitionByte(a []byte, lo, hi int) int {
70-
pivot := a[hi]
71-
i := lo - 1
72-
for j := lo; j < hi; j++ {
73-
if a[j] > pivot {
74-
i++
75-
a[j], a[i] = a[i], a[j]
76-
}
77-
}
78-
a[i+1], a[hi] = a[hi], a[i+1]
79-
return i + 1
80-
}
81-
func quickSortByte(a []byte, lo, hi int) {
82-
if lo >= hi {
83-
return
84-
}
85-
p := partitionByte(a, lo, hi)
86-
quickSortByte(a, lo, p-1)
87-
quickSortByte(a, p+1, hi)
88-
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
func minPairSum(nums []int) int {
6+
sort.Ints(nums)
7+
n, res := len(nums), 0
8+
for i, val := range nums[:n/2] {
9+
res = max(res, val+nums[n-1-i])
10+
}
11+
return res
12+
}
13+
14+
func max(a, b int) int {
15+
if a > b {
16+
return a
17+
}
18+
return b
19+
}

leetcode/1877.Minimize-Maximum-Pair-Sum-in-Array/1877. Minimize Maximum Pair Sum in Arrayz_test.go

Whitespace-only changes.

leetcode/1877.Minimize-Maximum-Pair-Sum-in-Array/README.md

Whitespace-only changes.

website/content/ChapterFour/0200~0299/0242.Valid-Anagram.md

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,47 +72,21 @@ func isAnagram(s string, t string) bool {
7272

7373
// 解法二
7474
func isAnagram1(s string, t string) bool {
75-
if s == "" && t == "" {
76-
return true
75+
hash := map[rune]int{}
76+
for _, value := range s {
77+
hash[value]++
7778
}
78-
if s == "" || t == "" {
79-
return false
79+
for _, value := range t {
80+
hash[value]--
8081
}
81-
sBytes := []byte(s)
82-
tBytes := []byte(t)
83-
if len(sBytes) != len(tBytes) {
84-
return false
85-
}
86-
quickSortByte(sBytes, 0, len(sBytes)-1)
87-
quickSortByte(tBytes, 0, len(tBytes)-1)
88-
89-
for i := 0; i < len(sBytes); i++ {
90-
if sBytes[i] != tBytes[i] {
82+
for _, value := range hash {
83+
if value != 0 {
9184
return false
9285
}
9386
}
9487
return true
9588
}
96-
func partitionByte(a []byte, lo, hi int) int {
97-
pivot := a[hi]
98-
i := lo - 1
99-
for j := lo; j < hi; j++ {
100-
if a[j] > pivot {
101-
i++
102-
a[j], a[i] = a[i], a[j]
103-
}
104-
}
105-
a[i+1], a[hi] = a[hi], a[i+1]
106-
return i + 1
107-
}
108-
func quickSortByte(a []byte, lo, hi int) {
109-
if lo >= hi {
110-
return
111-
}
112-
p := partitionByte(a, lo, hi)
113-
quickSortByte(a, lo, p-1)
114-
quickSortByte(a, p+1, hi)
115-
}
89+
11690

11791
```
11892

0 commit comments

Comments
 (0)