当前位置:网站首页>leetcode存在重复元素go实现
leetcode存在重复元素go实现
2022-07-02 04:58:00 【Jimmy_jimi】
目标:存在重复元素
给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false 。
结果产出:
### 暴力破解
func containsDuplicate(nums []int) bool {
flag := false
re:
for i:=0;i<len(nums);i++{
for j:=i+1;j<len(nums);j++{
if nums[i] == nums[j]{
flag =true
break re
}
}
}
return flag
}
使用排序sort.Ints
func containsDuplicate(nums []int) bool {
sort.Ints(nums)
for i := 1; i < len(nums); i++ {
if nums[i] == nums[i-1] {
return true
}
}
return false
}
使用hash表
func containsDuplicate(nums []int) bool {
set := map[int]struct{
}{
}
for _, v := range nums {
//set表中存在键值等于has的值
if _, has := set[v]; has {
return true
}
set[v] = struct{
}{
}
}
return false
}
边栏推荐
- Beginner crawler - biqu Pavilion crawler
- Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
- 案例分享|智慧化的西部机场
- Learn what definitelytyped is through the typescript development environment of SAP ui5
- Application of intelligent robot in agricultural ecology
- Introduction to Luogu 3 [circular structure] problem list solution
- Learn AI safety monitoring project from zero [attach detailed code]
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
- Embedded-c language-8-character pointer array / large program implementation
- How to configure PostgreSQL 12.9 to allow remote connections
猜你喜欢

How to write a client-side technical solution

Markdown编辑语法

Analyze the space occupied by the table according to segments, clusters and pages

C# 基于MQTTNet的服务端与客户端通信案例

ansible安装与使用

Keil compilation code of CY7C68013A

Pit encountered in win11 pytorch GPU installation

缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性

Learn BeanShell before you dare to say you know JMeter

Mathematical knowledge -- understanding and examples of fast power
随机推荐
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
Win10 disk management compressed volume cannot be started
Use of typescript classes
Introduction to Luogu 3 [circular structure] problem list solution
数学知识——快速幂的理解及例题
Beginner crawler - biqu Pavilion crawler
Orthogonal test method and function diagram method for test case design
Mysql重点难题(2)汇总
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Let genuine SMS pressure measurement open source code
How to modify data file path in DM database
Embedded-c language-9-makefile/ structure / Consortium
Simple and practical accounting software, so that accounts can be checked
js面试收藏试题1
Domestic all Chinese automatic test software apifox
洛谷入门3【循环结构】题单题解
Mathematical knowledge -- understanding and examples of fast power
Summary of database problems
DMA Porter
06 decorator mode