add new solutions
This commit is contained in:
parent
e1b702657c
commit
59b71480d4
11 changed files with 343 additions and 0 deletions
26
solutions/2/q205/solution.go
Normal file
26
solutions/2/q205/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q205
|
||||
|
||||
func isIsomorphic(s string, t string) bool {
|
||||
if len(s) != len(t) {
|
||||
return false
|
||||
}
|
||||
|
||||
ab := make(map[byte]byte, 256)
|
||||
ba := make(map[byte]byte, 256)
|
||||
for i := range len(s) {
|
||||
if b, ok := ab[s[i]]; ok {
|
||||
if t[i] != b {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
if _, ok := ba[t[i]]; ok {
|
||||
return false
|
||||
}
|
||||
ab[s[i]] = t[i]
|
||||
ba[t[i]] = s[i]
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var _ = isIsomorphic
|
||||
18
solutions/2/q219/solution.go
Normal file
18
solutions/2/q219/solution.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package q219
|
||||
|
||||
func containsNearbyDuplicate(nums []int, k int) bool {
|
||||
seen := make(map[int]struct{}, k)
|
||||
|
||||
for i, num := range nums {
|
||||
if _, ok := seen[num]; ok {
|
||||
return true
|
||||
}
|
||||
seen[num] = struct{}{}
|
||||
if i >= k {
|
||||
delete(seen, nums[i-k])
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var _ = containsNearbyDuplicate
|
||||
30
solutions/2/q290/solution.go
Normal file
30
solutions/2/q290/solution.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package q290
|
||||
|
||||
import "strings"
|
||||
|
||||
func wordPattern(pattern string, s string) bool {
|
||||
index := map[byte]string{}
|
||||
invIndex := map[string]struct{}{}
|
||||
|
||||
fields := strings.Fields(s)
|
||||
if len(fields) != len(pattern) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i, word := range fields {
|
||||
if w, ok := index[pattern[i]]; ok {
|
||||
if w != word {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
if _, ok := invIndex[word]; ok {
|
||||
return false
|
||||
}
|
||||
index[pattern[i]] = word
|
||||
invIndex[word] = struct{}{}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var _ = wordPattern
|
||||
Loading…
Add table
Add a link
Reference in a new issue