当前位置:网站首页>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
}
边栏推荐
- Getting started with pytest ----- confitest Application of PY
- ansible安装与使用
- 汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
- Several methods of capturing packets under CS framework
- Cultivate primary and secondary school students' love for educational robots
- Pytest learning ----- pytest assertion of interface automation testing
- Orthogonal test method and function diagram method for test case design
- National all Chinese Automatic Test Software apifox
- What data does the main account of Zhengda Meiou 4 pay attention to?
- Summary of main account information of zhengdaliu 4
猜你喜欢
Solution of DM database unable to open graphical interface
Gin framework learning code
Several methods of capturing packets under CS framework
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
[bus interface] Axi interface
Learn AI safety monitoring project from zero [attach detailed code]
正大留4的主账户信息汇总
Starting from the classification of database, I understand the map database
cs架构下抓包的几种方法
TypeScript函数详解
随机推荐
Pytest learning ----- pytest assertion of interface automation testing
Mysql表insert中文变?号的问题解决办法
Common errors of dmrman offline backup
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Oracle stored procedure and job task setting
Mathematical knowledge (Euler function)
June book news | 9 new books are listed, with a strong lineup and eyes closed!
Getting started with pytest ----- confitest Application of PY
Record the bug of unity 2020.3.31f1 once
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
Mapping location after kotlin confusion
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
win11安装pytorch-gpu遇到的坑
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
LM09丨费雪逆变换反转网格策略
Cultivate primary and secondary school students' love for educational robots
Video cover image setting, put cover images into multiple videos in the simplest way