当前位置:网站首页>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
}
边栏推荐
- 2022-003arts: recursive routine of binary tree
- 社交媒体搜索引擎优化及其重要性
- Summary of main account information of zhengdaliu 4
- Here comes the chicken soup! Keep this quick guide for data analysts
- 國產全中文-自動化測試軟件Apifox
- Virtual machine installation deepin system
- 数据库问题汇总
- Comp 250 parsing
- Cannot activate CONDA virtual environment in vscode
- Oracle和MySQL的基本区别(入门级)
猜你喜欢

DC-1靶场搭建及渗透实战详细过程(DC靶场系列)

2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路

idea自动导包和自动删包设置

奠定少儿编程成为基础学科的原理

AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道

Research on the security of ognl and El expressions and memory horse

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

Alibaba cloud polkit pkexec local rights lifting vulnerability

idea自動導包和自動删包設置

Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
随机推荐
Social media search engine optimization and its importance
June book news | 9 new books are listed, with a strong lineup and eyes closed!
Use of Baidu map
10 minute quick start UI automation ----- puppeter
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
geotrust ov多域名ssl证书一年两千一百元包含几个域名?
Change deepin to Alibaba image source
MMAP zero copy knowledge point notes
Typescript function details
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
数学知识(欧拉函数)
JS interview collection test question 1
DJB Hash
Common errors of dmrman offline backup
Pit encountered in win11 pytorch GPU installation
Mysql表insert中文变?号的问题解决办法
LeetCode-对链表进行插入排序
Rhcsa --- work on the fourth day
TypeScript类的使用