标签:pack return import fun rdp 输出 port div UNC
package main import ( "fmt" "strings" ) func main() { /* 示例1: 输入: pattern = "abba", str = "dog cat cat dog" 输出: true 示例 2: 输入:pattern = "abba", str = "dog cat cat fish" 输出: false */ var pattern = "abba" var str = "dog cat cat dog" res := wordPattern(pattern, str) fmt.Println(res) } func wordPattern(pattern string, str string) bool { var slice []int for i := 0; i < len(pattern); i++ { s := pattern[i] if s == pattern[0] { slice = append(slice, 0) } if s != pattern[0] { slice = append(slice, 1) } } strSlice := strings.Split(str, " ") var slice2 []int for i := 0; i < len(strSlice); i++ { s := pattern[i] if s == pattern[0] { slice2 = append(slice2, 0) } if s != pattern[0] { slice2 = append(slice2, 1) } } fmt.Println(slice, slice2) // 引用类型的比较实际判断的是两个变量是不是指向同一份数据,它不会去比较实际指向的数据 for i, i2 := range slice { // slice2[i] 警告是因为可能超出范围 if i2 == slice2[i] { return true } } return false }
标签:pack return import fun rdp 输出 port div UNC
原文地址:https://www.cnblogs.com/yzg-14/p/13343035.html