当前位置:网站首页>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
}
边栏推荐
- Go Chan's underlying principles
- Orthogonal test method and function diagram method for test case design
- C - derived classes and constructors
- Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
- 6.30 year end summary, end of student age
- Video cover image setting, put cover images into multiple videos in the simplest way
- ansible安装与使用
- Super detailed pycharm tutorial
- Solution: the agent throws an exception error
- C case of communication between server and client based on mqttnet
猜你喜欢
Detailed process of DC-1 range construction and penetration practice (DC range Series)
Markdown编辑语法
Social media search engine optimization and its importance
关于Steam 教育的知识整理
Virtual machine installation deepin system
解决:代理抛出异常错误
数学知识——快速幂的理解及例题
Mathematical knowledge (Euler function)
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
随机推荐
国产全中文-自动化测试软件Apifox
Analyzing the hands-on building tutorial in children's programming
MySQL table insert Chinese change? Solution to the problem of No
Realize the function of data uploading
06 decorator mode
洛谷入门3【循环结构】题单题解
How to modify data file path in DM database
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
What data does the main account of Zhengda Meiou 4 pay attention to?
Solution: the agent throws an exception error
Knowledge arrangement about steam Education
Win10 disk management compressed volume cannot be started
Getting started with pytest ----- confitest Application of PY
DMA Porter
培养中小学生对教育机器人的热爱之心
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
Summary of main account information of zhengdaliu 4
How to recover deleted data in disk
win10 磁盘管理 压缩卷 无法启动问题