C#LeetCode刷题之#219-存在重复元素 II(Contains Duplicate II)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3704 访问。
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k。
输入: nums = [1,2,3,1], k = 3
输出: true
输入: nums = [1,0,1,1], k = 1
输出: true
输入: nums = [1,2,3,1,2,3], k = 2
输出: false
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Input: nums = [1,2,3,1], k = 3
Output:true
Input: nums = [1,0,1,1], k = 1
Output: true
Input: nums = [1,2,3,1,2,3], k = 2
Output:false
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3704 访问。
public class Program {public static void Main(string[] args) {int[] nums = null;nums = new int[] { 1, 2, 3, 1, 2, 3 };var res = ContainsNearbyDuplicate(nums, 2);Console.WriteLine(res);nums = new int[] { 1, 0, 1, 1 };res = ContainsNearbyDuplicate(nums, 1);Console.WriteLine(res);Console.ReadKey();}private static bool ContainsNearbyDuplicate(int[] nums, int k) {//暴力解法,此解法超时,LeetCode没有ACfor(int i = 0; i < nums.Length; i++) {for(int j = 1; j <= k; j++) {if(i + j < nums.Length && nums[i] == nums[i + j]) {return true;}}}return false;}private static bool ContainsNearbyDuplicate2(int[] nums, int k) {//哈希法var dic = new Dictionary<int, int>();//用字典存放键值对,key为数组中的值,value为数组的索引for(int i = 0; i < nums.Length; i++) {if(dic.ContainsKey(nums[i])) {//如果已经包含键int diss = i - dic[nums[i]];//记录索引差if(diss <= k) {//达到题目要求,返回truereturn true;} else {//达不到题目要求时,记录值和索引dic[nums[i]] = i;}} else {//如果不包含,记录值和索引dic[nums[i]] = i;}}return false;}}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3704 访问。
False
True
分析:
显而易见,ContainsNearbyDuplicate的时间复杂度为: ,ContainsNearbyDuplicate2的时间复杂度为: 。
C#LeetCode刷题之#219-存在重复元素 II(Contains Duplicate II)相关推荐
- C#LeetCode刷题之#217-存在重复元素(Contains Duplicate)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3772 访问. 给定一个整数数组,判断是否存在重复元素. 如果任何 ...
- 【快乐水题】219. 存在重复元素 II
原题: 力扣链接:219. 存在重复元素 II 题目简述: 给你一个整数数组 nums 和一个整数 k ,判断数组中是否存在两个 不同的索引 i 和 j ,满足 nums[i] == nums[j] ...
- leetcode刷题:1.无重复字符的最长字串
题目: 方法一: 首先我们可以想到暴力解法,就是 ·逐个生成字符串 ·看他受否含有重复字符 如下代码暴力法: int LenOfUniqueStr(char* Start) {int Validity ...
- 卷进大厂系列之LeetCode刷题笔记:移除元素(简单)
学算法,刷力扣,加油卷,进大厂! 题目描述 力扣题目链接 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度. 不要使用额外的数组空间, ...
- LeetCode刷题(48)--Remove Duplicates from Sorted List II
cur表示当前所在的Node,对于重复出现的Node会移到最后一个. pre记录结果,res用于返回,pre的更改会体现在res上. 如果pre.next == cur,则说明没有重复,此时pre = ...
- LeetCode刷题(44)--Remove Duplicates from Sorted Array II
允许重复两次,关注结果,快的n扫描列表,慢的i记录位置. class Solution(object):def removeDuplicates(self, nums):""&qu ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode刷题——哈希表(python语言)
LeetCode刷题--哈希表(python语言) 一.哈希表 1.1 哈希表的概念 哈希表,也叫散列表.其实可以很像python的字典,也就是键(key)值(Hash(key))对,最简单也最常用的 ...
最新文章
- UVA 1331 Minimax Triangulation DP, 三角剖分
- Linux命令详解:[7]获得命令帮助
- javascript设计模式系列 - LukeLin - 博客园
- mysql dump gtid_mysqldump命令详解 Part 3- 备份全库
- Chromium OS 开源项目
- server2019 sqlcmd命令安装_Ubuntu20.04LTS安装MS sql-server2019的方法
- 打败 IE 的葵花宝典:CSS Bug Table
- 查询oracle表空间有什么数据,oracle查询表空间使用情况与查询有哪些数据库实例在运行...
- React Native 介绍
- (莱昂氏unix源代码分析导读-49) 字符缓冲区
- c语言实现鼠标驱动,鼠标驱动程序
- 计算机专业论文的创新点怎么说,计算机专业毕业生如何写毕业论文
- 程序员必知之浮点数运算原理详解
- 量化投资学习——中证500期现套利
- IDEA maven 项目 POM文件变灰色或有个虫子(蜘蛛标记)
- 360全景摄影的逆光问题如何解决?
- 【嵌入式学习-STM32F103-TIM-编码器接口】
- 独家专访阿里集团副总裁贾扬清:我为什么选择加入阿里巴巴?
- WebAPI压力测试
- 搭建 WNMP 环境