Golang Leetcode 217. Contains Duplicate.go

2019-04-12 11:33:30 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412915

思路

用一个hash保存出现过的元素

code

代码语言:javascript复制
func containsDuplicate(nums []int) bool {
	if len(nums) < 2 {
		return false
	}
	m := make(map[int]bool)
	for _, v := range nums {
		_, ok := m[v]
		if ok {
			return true
		}
		m[v] = true
	}
	return false
}

0 人点赞