教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 golang key map 所有_Golang:map的比较-Go语言中文社区

golang key map 所有_Golang:map的比较-Go语言中文社区

发布时间:2021-05-21   编辑:jiaochengji.com
教程集为您提供golang key map 所有,Golang:map的比较-Go语言中文社区等资源,欢迎您收藏本站,我们将为您提供最新的golang key map 所有,Golang:map的比较-Go语言中文社区资源

在提交Leetcode 242. 有效的字母异位词代码时碰到了如下编译错误:

map can only be compared to nil

查看文档发现Golang中要比较两个map实例需要使用reflect包的DeepEqual()方法。如果相比较的两个map满足以下条件,方法返回true:

Map values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they are the same map object or their corresponding keys (matched using Go equality) map to deeply equal values.

1.两个map都为nil或者都不为nil,并且长度要相等

they are both nil or both non-nil, they have the same length

2.相同的map对象或者所有key要对应相同

either they are the same map object or their corresponding keys

3.map对应的value也要深度相等

map to deeply equal values

题目提交改为以下即可。

func isAnagram(s string, t string) bool {

sDir := map[string]int{}

tDir := map[string]int{}

for _,ss:= range s {

sDir[string(ss)]

}

for _,tt:= range t {

tDir[string(tt)]

}

return reflect.DeepEqual(sDir,tDir)

}

参考:

到此这篇关于“golang key map 所有_Golang:map的比较-Go语言中文社区”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
golang key map 所有_Golang:map的比较-Go语言中文社区
golang url 收集
想系统学习GO语言(Golang
go语言中map的实现原理
golang map 初始化 和 使用
golang 并发访问map遇到的问题
golang map 排序 key value
golang中map的一些注意事项
Go基础编程:Map
golang key map 所有_golang之map

[关闭]
~ ~