问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. Ethical Hacking - NETWORK PENETRATION TESTING(18)

    Session Hijacking What if the user uses the "remember me" feature? If the user uses this f ...

  2. 集训作业 洛谷P1433 吃奶酪

    嗯?这题竟然是个绿题. 这个题真的不难,不要被他的难度吓到,我们只是不会计算2点之间的距离,他还给出了公式,这个就有点…… 我们直接套公式去求出需要的值,然后普通的搜索就可以了. 这个题我用的深搜,因 ...

  3. scratch编程——画笔模块画各种同心图案

    我们今天是要用画笔来画出不同的同心图案,在画之前,我们先来了解一下画笔模块: 1.画笔模块的用法 画笔模块的用法就是在舞台上留下不同颜色粗细的线条,它的默认是情况是抬笔,我们在使用的时候要让角色移动到 ...

  4. lambda之美

    github源码 大前提:jdk8  允许lambda表达式  最好在maven中加入 <properties> <java.version>1.8</java.vers ...

  5. java基础(二)--main方法讲解

    main()函数是如下的固定格式,除了args可以修改名字,其余均不可以修改 public class TestBase02MainMath { public static void main(Str ...

  6. fiddler替换修改后的js文件绕过无限debugger

    转自:https://www.jianshu.com/p/38c4afae636c 1.在js文件右击, 然后点击save as ..., 把js文件保存到本地.(网站:https://taodaxi ...

  7. pandas之groupby分组与pivot_table透视

    一.groupby 类似excel的数据透视表,一般是按照行进行分组,使用方法如下. df.groupby(by=None, axis=0, level=None, as_index=True, so ...

  8. SPRING 阅读--JdkDynamicAopProxy

    一.简介 JdkDynamicAopProxy 代理类是spring 默认的JDK动态的代理类实现.它实现了Java 动态代理接口InvocationHandler接口和Spring定义的AopPro ...

  9. 如何用Excel进行预测分析?

      [面试题] 一个社交APP, 它的新增用户次日留存.7日留存.30日留存分别是52%.25%.14%. 请模拟出来,每天如果日新增6万用户,那么第30天,它的日活数会达到多少?请使用Excel进行 ...

  10. 【工具】 - BeanUtils增强篇

    public class BeanPlusUtils extends BeanUtils { public static <S, T> List<T> copyListProp ...