问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3772 访问。

给定一个整数数组,判断是否存在重复元素。

如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。

输入: [1,2,3,1]

输出: true

输入: [1,2,3,4]

输出: false

输入: [1,1,1,3,3,4,3,2,4,2]

输出: true


Given an array of integers, find if the array contains any duplicates.

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Input: [1,2,3,1]

Output: true

Input: [1,2,3,4]

Output: false

Input: [1,1,1,3,3,4,3,2,4,2]

Output: true


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3772 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = null; nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 7 };
var res = ContainsDuplicate(nums);
Console.WriteLine(res); nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
res = ContainsDuplicate2(nums);
Console.WriteLine(res); nums = new int[] { 1, 2, 3, 3, 5, 6, 7, 8 };
res = ContainsDuplicate3(nums);
Console.WriteLine(res); Console.ReadKey();
} private static bool ContainsDuplicate(int[] nums) {
//暴力解法
for(int i = 0; i < nums.Length; i++) {
for(int j = i + 1; j < nums.Length; j++) {
if(nums[i] == nums[j]) return true;
}
}
return false;
} private static bool ContainsDuplicate2(int[] nums) {
//先排序,再使用单循环比较相邻值
Array.Sort(nums);
for(int i = 0; i < nums.Length - 1; i++) {
if(nums[i] == nums[i + 1]) return true;
}
return false;
} private static bool ContainsDuplicate3(int[] nums) {
//哈希法
var dic = new Dictionary<int, int>();
foreach(var num in nums) {
if(dic.ContainsKey(num)) return true;
dic[num] = 0;
}
return false;
} }

以上给出3种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3772 访问。

True
False
True

分析:

显而易见,ContainsDuplicate的时间复杂度为:  ,ContainsDuplicate2的时间复杂度需要考虑Array.Sort()具体使用的排序算法,ContainsDuplicate3的时间复杂度为:  。

C#LeetCode刷题之#217-存在重复元素(Contains Duplicate)的更多相关文章

  1. C#LeetCode刷题之#219-存在重复元素 II​​​​​​​(Contains Duplicate II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3704 访问. 给定一个整数数组和一个整数 k,判断数组中是否存在 ...

  2. leetcode刷题笔记-3. 无重复字符的最长子串(java实现)

    题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "ab ...

  3. #leetcode刷题之路3-无重复字符的最长子串

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc" ...

  4. #leetcode刷题之路27-移除元素

    给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. ...

  5. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  6. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  7. leetcode刷题目录

    leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...

  8. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  9. LeetCode刷题总结之双指针法

    Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...

随机推荐

  1. git本地创建分支,并提交到github上去

    很多时候,我们再开发的时候需要分支. 那么怎么在本地创建分支,并提交到github或者是远程仓库中呢? 其实很简单: 第一步: git checkout -b dev     创建新的分支 第二步: ...

  2. 没内鬼,来点干货!SQL优化和诊断

    SQL优化与诊断 Explain诊断 Explain各参数的含义如下: 列名 说明 id 执行编号,标识select所属的行.如果在语句中没有子查询或关联查询,只有唯一的select,每行都将显示1. ...

  3. springboot application.yml配置学习

    一.背景 为了更好的使用springboot,所以看一下application.yml配置这块.主要是看数据绑定这块. 主要参考:https://www.hangge.com/blog/cache/d ...

  4. 关于maven的一份小笔记

    简介 项目里一直用的 maven,几乎天天和这个"熟知"的工具打交道,但是,最近我发觉自己对 maven 了解的还不够,例如,什么是 goal?什么是 phase?等等.趁着最近有 ...

  5. 从element-ui按需引入去探索

    element-ui的按需引入的配置:文档地址 npm install babel-plugin-component -D { "presets": [["es2015& ...

  6. .Net Core in Docker极简入门(下篇)

    Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章. 目录 前言 开始 Docker-Compose 代码修改 yml file up & down 镜像仓库 最后 前言 上一篇[. ...

  7. MySQL 删除表中所有数据

    方法一:使用 delete from [表名]    生成日志 方法二:使用 truncate table [表名]    无日志生成 两种方式删除后再插入数据,第一条id的值不一样 方法一: 方法二 ...

  8. 数据库事务的四个特性ACID

    原子性[Atomicity] 原子性指的指的就是这个操作,要么全部成功,要么全部失败回滚.不存在其他的情况. 一致性(Consistency) 一致性是指事务必须使数据库从一个一致性状态变换到另一个一 ...

  9. 关于C语言内存占用

    struct T { char a; int *d; int b; int c:16; double e; }; T *p; 在64位系统以及64位编译器下,以下描述正确的是 A. sizeof(p) ...

  10. leetcode 翻转字符串

    https://leetcode-cn.com/problems/reverse-words-in-a-string/ TLE代码: class Solution { public: string r ...