2840. 判断通过操作能否让字符串相等 II
This commit is contained in:
parent
51984f6f9b
commit
1d785da008
119
main.go
119
main.go
|
|
@ -1,91 +1,50 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
//type Test interface {
|
func isMapEqual(a map[uint8]int, b map[uint8]int) bool {
|
||||||
//}
|
for k, v := range a {
|
||||||
//
|
if val, ok := b[k]; ok {
|
||||||
//func condTest(cond *sync.Cond) {
|
if val != v {
|
||||||
// cond.L.Lock()
|
return false
|
||||||
// cond.Wait()
|
}
|
||||||
// fmt.Println("condTest")
|
} else {
|
||||||
// cond.L.Unlock()
|
return false
|
||||||
//}
|
|
||||||
//func chanTest(ch chan<- *Test) {
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
|
|
||||||
func reverse(x int) int {
|
|
||||||
neg := false
|
|
||||||
if x < 0 {
|
|
||||||
neg = true
|
|
||||||
x = -x
|
|
||||||
}
|
|
||||||
s := fmt.Sprintf("%d", x)
|
|
||||||
var sb strings.Builder
|
|
||||||
for i := len(s) - 1; i >= 0; i-- {
|
|
||||||
sb.WriteByte(s[i])
|
|
||||||
}
|
|
||||||
s = sb.String()
|
|
||||||
var ans int32 = 0
|
|
||||||
for c := range s {
|
|
||||||
t, _ := strconv.ParseUint(string(s[c]), 10, 16)
|
|
||||||
ans = ans*10 + int32(t)
|
|
||||||
}
|
|
||||||
if neg {
|
|
||||||
ans = -ans
|
|
||||||
x = -x
|
|
||||||
}
|
|
||||||
s1, s2 := fmt.Sprintf("%d", x), fmt.Sprintf("%d", ans)
|
|
||||||
if neg {
|
|
||||||
s1 = s1[1:]
|
|
||||||
s2 = s2[1:]
|
|
||||||
}
|
|
||||||
if len(s1) != len(s2) {
|
|
||||||
for len(s2) < len(s1) {
|
|
||||||
s2 = "0" + s2
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i := 0; i < len(s1); i++ {
|
for k, v := range b {
|
||||||
if s1[i] != s2[len(s2)-1-i] {
|
if val, ok := a[k]; ok {
|
||||||
return 0
|
if val != v {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return int(ans)
|
return true
|
||||||
|
}
|
||||||
|
func checkStrings(s1 string, s2 string) bool {
|
||||||
|
mp1 := make(map[uint8]int)
|
||||||
|
mp2 := make(map[uint8]int)
|
||||||
|
for i := 0; i < len(s1); i += 2 {
|
||||||
|
mp1[s1[i]]++
|
||||||
|
mp2[s2[i]]++
|
||||||
|
}
|
||||||
|
if !isMapEqual(mp1, mp2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
mp1 = make(map[uint8]int)
|
||||||
|
mp2 = make(map[uint8]int)
|
||||||
|
for i := 1; i < len(s1); i += 2 {
|
||||||
|
mp1[s1[i]]++
|
||||||
|
mp2[s2[i]]++
|
||||||
|
}
|
||||||
|
if !isMapEqual(mp1, mp2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
//9646324351
|
|
||||||
//2147483648
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(reverse(1534236469))
|
fmt.Printf("%v", checkStrings("abcdba", "cabdab"))
|
||||||
//var x int = 16
|
|
||||||
//var y any = x
|
|
||||||
//if _, ok := y.(int); ok {
|
|
||||||
// fmt.Println(x)
|
|
||||||
//}
|
|
||||||
//if _, ok := y.(int32); ok {
|
|
||||||
// fmt.Println(x)
|
|
||||||
//}
|
|
||||||
//var mu sync.Mutex
|
|
||||||
//sync.NewCond(&mu)
|
|
||||||
//make(chan *Test, 6)
|
|
||||||
//var wg sync.WaitGroup
|
|
||||||
//wg.Add(1)
|
|
||||||
//wg.Done()
|
|
||||||
//var mu sync.Mutex
|
|
||||||
//cond := sync.NewCond(&mu)
|
|
||||||
//
|
|
||||||
//go condTest(cond)
|
|
||||||
//go condTest(cond)
|
|
||||||
//
|
|
||||||
//time.Sleep(time.Second * 2)
|
|
||||||
//cond.L.Lock()
|
|
||||||
//cond.Broadcast()
|
|
||||||
//cond.L.Unlock()
|
|
||||||
//time.Sleep(time.Second * 2)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue