当前位置:网站首页>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
}
边栏推荐
- GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
- Keil compilation code of CY7C68013A
- 農業生態領域智能機器人的應用
- [Yu Yue education] autumn 2021 reference materials of Tongji University
- Idea automatic package import and automatic package deletion settings
- Analyzing the hands-on building tutorial in children's programming
- Mysql表insert中文变?号的问题解决办法
- Save the CDA from the disc to the computer
- Record the bug of unity 2020.3.31f1 once
- Social media search engine optimization and its importance
猜你喜欢

Save the CDA from the disc to the computer

Cannot activate CONDA virtual environment in vscode

Social media search engine optimization and its importance

Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data

Mathematical knowledge (Euler function)

Markdown编辑语法

LeetCode-对链表进行插入排序

解决:代理抛出异常错误

数学知识(欧拉函数)

Starting from the classification of database, I understand the map database
随机推荐
Leetcode- insert and sort the linked list
Vmware安装win10报错:operating system not found
MySQL table insert Chinese change? Solution to the problem of No
Let正版短信测压开源源码
DMA Porter
oracle 存储过程与job任务设置
Go GC garbage collection notes (three color mark)
Go Chan's underlying principles
Several methods of capturing packets under CS framework
06 装饰(Decorator)模式
win10 磁盘管理 压缩卷 无法启动问题
How to configure PostgreSQL 12.9 to allow remote connections
Social media search engine optimization and its importance
A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
C # picture display occupancy problem
Ansible installation and use
C case of communication between server and client based on mqttnet
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
cs架构下抓包的几种方法
Analyzing the hands-on building tutorial in children's programming