当前位置:网站首页>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
}
边栏推荐
- Domestic all Chinese automatic test software apifox
- Rhcsa --- work on the fourth day
- Summary of main account information of zhengdaliu 4
- Video multiple effects production, fade in effect and border background are added at the same time
- Gin framework learning code
- June book news | 9 new books are listed, with a strong lineup and eyes closed!
- Online incremental migration of DM database
- Analyzing the hands-on building tutorial in children's programming
- 数学知识(欧拉函数)
- How to modify data file path in DM database
猜你喜欢

ansible安装与使用

Cultivate primary and secondary school students' love for educational robots

农业生态领域智能机器人的应用

Solution: the agent throws an exception error

06 decorator mode

How to configure PostgreSQL 12.9 to allow remote connections
![Learn AI safety monitoring project from zero [attach detailed code]](/img/a9/cb93f349229e86cbb05ad196ae9553.jpg)
Learn AI safety monitoring project from zero [attach detailed code]

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

Promise all()

Common errors of dmrman offline backup
随机推荐
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
Let genuine SMS pressure measurement open source code
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
LM09丨费雪逆变换反转网格策略
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
Orthogonal test method and function diagram method for test case design
Leetcode- insert and sort the linked list
CubeMx DMA笔记
List of common bugs in software testing
解析少儿编程中的动手搭建教程
Solution: the agent throws an exception error
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
Keil compilation code of CY7C68013A
Online incremental migration of DM database
Cannot activate CONDA virtual environment in vscode
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Getting started with pytest ----- confitest Application of PY
[Yu Yue education] autumn 2021 reference materials of Tongji University