【leetcode】219(Easy)Contains Duplicate II

改进差分进化算法及其求解柔性作业车间调度问题(Python代码实现) 内容概要:本文系统探讨了改进差分进化算法在求解柔性作业车间调度问题中的应用,并结合Python代码实现,展示了该算法相较于传统方法在全局搜索能力和收敛速度方面的优势。研究不仅聚焦于算法本身的改进策略,如变异、交叉和选择操作的优化,还深入分析了其在复杂制造环境中应对多目标、多约束调度问题的有效性。文中通过标准测试实例验证了算法性能,并与其他智能优化算法进行了对比分析,体现了其在实际工程应用中的潜力。此外,文档还关联了多无人机路径规划、电力系统状态估计、微电网优化、图像与信号处理等多个前沿技术领域,展现了智能优化算法在跨学科工程仿真与决策支持中的广泛适用性。; 适合人群:具备一定编程基础和工程背景,从事智能制造、自动化、工业工程、电气工程、控制科学与工程、计算机科学及相关领域的科研人员、高校研究生及企业研发技术人员。; 使用场景及目标:①为科研工作者提供可复现的智能优化算法代码框架,支持柔性作业车间调度、多无人机协同路径规划、电力系统优化等复杂工程问题的建模仿真;②辅助完成学术论文复现、课程设计、毕业设计或科研项目开发;③推动智能算法在生产调度、能源管理、交通规划等实际场景中的落地应用。; 阅读建议:建议读者结合文中提供的算法原理与代码实现,动手调试并验证不同参数设置对优化结果的影响,同时可参考荔枝科研社提供的完整资源包进行扩展学习与实践,以加深对算法机制与工程应用的理解。 立即下载
解题思路

创建一个map,如果当前数字没有出现过,就存下这个数字和这个数字的位置
如果当前数字出现过就查看这个数字上一次出现的位置,如果二者之差小于k就返回true。

提交代码
class Solution {
	public boolean containsNearbyDuplicate(int[] nums, int k) {
		Map<Integer, Integer> map = new HashMap<>();

		for (int i = 0; i < nums.length; i++) {
			if (map.containsKey(nums[i])) {
				if (i - map.get(nums[i]) <= k)
					return true;
				map.put(nums[i], i);
			} else {
				map.put(nums[i], i);
			}
		}
		return false;
	}
}
运行结果

在这里插入图片描述

其他解法

这道题还有一种解法,思想是滑动窗口,使用set集合来实现,思想是保持窗口的长度为k,用set来保存所有的元素,滑动窗口时,如果set集合添加元素失败说明长度为k的窗口内有重复的元素,就直接返回true:

class Solution{
public boolean containsNearbyDuplicate(int[] nums, int k) {
        Set<Integer> set = new HashSet<Integer>();
        for(int i = 0; i < nums.length; i++){
            if(i > k) set.remove(nums[i-k-1]);
            if(!set.add(nums[i])) return true;
        }
        return false;
 }
}
运行结果

在这里插入图片描述

anaconda navigator 卡在loading application界面一直没有反应解决办法 在打开anaconda navigator 程序的时候,遇到了2个问题。第一个问题:打开的时候弹出提示:There is an instance of Anaconda Navigator already running.第二个问题:程序一直卡在loading application界面一直没有反应。以下是我的解决办法,有效果。 阅读详情

相关推荐

Codesys电子凸轮实战:从机械替代到虚轴设计的5个关键步骤

本文详细介绍了Codesys电子凸轮技术在运动控制领域的实战应用,从机械替代到虚轴设计的5个关键步骤。通过电子凸轮技术,操作员可以快速调整切割模式,显著提升产线效率。文章还探讨了虚轴架构、CAM表设计、动态耦合技巧及调试避坑指南,为工业自动化领域提供了实用的解决方案。

tech5的博客 767

【leetcode】【easy】219. Contains Duplicate II

219.Contains Duplicate II Given an array of integers and an integerk, find out whether there are two distinct indicesiandjin the array such thatnums[i] = nums[j]and theabsolutedifference be...

lemonade13的博客 144

FPGA实战:给你的UART串口通信加上Verilog奇偶校验位(Modelsim仿真验证)

本文详细介绍了如何在FPGA中实现UART串口通信的Verilog奇偶校验功能,包括发送端校验位生成和接收端校验检测的设计与实现。通过Modelsim仿真验证,确保数据传输的可靠性,适用于嵌入式系统和数字通信领域。

weixin_34216614的博客 341

[Leetcode 219, Easy] Contains Duplicate II

[Leetcode 219, Easy] Contains Duplicate II

算法的实践 625

Codex 命令行高效操作指南

本文详细介绍了Codex命令行工具的高效使用方法,包括安装配置、核心参数解析和实战技巧。通过`codex complete`、`codex explain`等指令,开发者可以快速生成代码、调试错误和重构项目,显著提升开发效率。特别推荐使用`--temperature`和`--max-tokens`参数优化生成结果。

weixin_29201313的博客 530

应广单片机 按键点灯 PMS171B GPIO LED

我也是第一次使用这款单片机,参考网友的资料,写个最近简单的demo 测试用例 #include "extern.h" /*端口定义*/ BIT KEY : PA.0; BIT SW : PA.4; /*相应于main函数*/ void FPPA0 (void) { /*单片机内部RC震动时钟为 IHRC默认为16M,因此,IHRC/2=8M,系统时钟为8M*/ .ADJUST_IC...

大江的专栏 999

Leetcode 219. Contains Duplicate II (Easy) (cpp)

Leetcode 219. Contains Duplicate II (Easy) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 342

告别Mac自带终端:iTerm2 + Oh My Zsh 保姆级配置指南(含国内镜像源)

本文提供了一份详细的Mac终端优化指南,涵盖iTerm2、Oh My Zsh和Homebrew的配置流程,特别针对国内用户提供镜像源解决方案。通过安装现代化工具链和优化配置,帮助开发者打造高效、美观的终端工作环境,提升日常开发效率。

weixin_42638178的博客 397

LeetCode 219. Contains Duplicate II-python(easy)

题目来源:      https://leetcode.com/problems/contains-duplicate-ii/description/题目分析:  本题是要给定一个整数数组和整数k,找出数组中是否存在两个不同的索引i和j,使得NUMS[i]=NUMS[j]并且i和j之间的绝对差值至多k。  最简单的想法是采用循环二重检索,但是时间复杂度太高不能通过,将代码写在下方:class So...

revivre的博客 733

NVIDIA DGX Spark实战指南:从开箱到部署200B参数大模型

本文详细介绍了NVIDIA DGX Spark桌面AI超级计算机的开箱体验与实战部署指南。文章深入解析了其Grace Blackwell架构与128GB统一内存的核心优势,并逐步演示了从系统初始化、运行200B参数大模型到进行QLoRA微调的全过程。该设备将高达1 PetaFLOP的AI算力浓缩于桌面,为开发者和研究团队提供了本地化、高性能的AI开发与部署一体化解决方案。

weixin_29274969的博客 2228

YOLO11改进|注意力机制篇|引入ELA注意力机制

YOLO11添加ELA注意力机制

A1983Z的博客 3647

LeetCode - Easy - 219. Contains Duplicate II

Topic Array Hash Table Sliding Window Description https://leetcode.com/problems/contains-duplicate-ii/ 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 a

KISS 181

利用人工神经网络在Abaqus中实现本构模型的网格粗化计算框架:详细指南与实践

在这篇文章中,我们详细介绍了如何在Abaqus中实现基于人工神经网络(ANN)的本构模型以进行网格粗化的计算框架。我们通过两个主要模块——数据生成器模块和ANN训练模块,实现了这一计算框架,并将训练好的ANN集成到Abaqus的用户材料子程序(UMAT)中,创建了一个基于ANN的本构模型。我希望这篇文章能帮助到那些希望在Abaqus中实现基于ANN的本构模型的读者,同时,我也非常欢迎大家提出宝贵的意见和建议,让我们共同进步。

qq_38334677的博客 1714

LeetCode Array Easy 219. Contains Duplicate II

---恢复内容开始--- Description Given an array of integers and an integerk, find out whether there are two distinct indicesiandjin the array such thatnums[i] = nums[j]and theabsolutedif...

baobo4566的博客 127

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【python3】leetcode 219. Contains Duplicate II(easy)

 219. Contains Duplicate II(easy) 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 diffe...

momottyy的专栏 250

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【LeetCode】219. Contains Duplicate II 存在重复元素 II(Easy)(JAVA)

【LeetCode】219. Contains Duplicate II 存在重复元素 II(Easy)(JAVA) 题目地址: https://leetcode.com/problems/contains-duplicate-ii/ 题目描述: 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]

qq_16927853的博客 126

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

leetcode 219. Contains Duplicate II (easy)

维护一个hash表,key为数组的值,value为下标 将数组中的元素不断添加进这个HashMap, 碰到重复时,计算二者的下标距离,如果距离小于或等于k,则返回true, 如果直到数组扫描完,距离都大于k,则返回false class Solution { public: bool containsNearbyDuplicate(vector<int> &nums, i...

Rh's blog 135

leetcode219[easy]----Contains Duplicate II

难度:easy 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

fawnzou2017的博客 222

219.leetcode Contains Duplicate II(easy)[数组 滑动窗口]

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 difference between i and j is at most k. 查

qz530308783的专栏 318

LeetCode 219.Contains Duplicate II

今天是大年初七,祝大家新年快乐!万事如意!恭喜发财! 同时也是今年第一篇文章:LeetCode 219 题解 说说LeetCode 作为大名鼎鼎的面试刷题网站,最近也出了中文版,如果之前没有了解过的童鞋,建议去网站看看,里面有各大互联网公司的算法面试题,包括谷歌,Fackbook,微软,亚马逊等等,近年来国内的互联网公司也越来越重视算法和数据结构了,特别是后端,经常看到高级工程师 职位以上的jd都...

BlueLzy的个人博客 358

219. Contains Duplicate II [easy] (Python)

题目链接https://leetcode.com/problems/contains-duplicate-ii/题目原文 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

Coder_Orz的博客 4155

leetcode 219. Contains Duplicate II 存在重复元素 II python3

时间:2020-6-10 题目地址:https://leetcode-cn.com/problems/contains-duplicate-ii/ 题目难度:Easy 题目描述: 给定一个整数数组和一个整数k,判断数组中是否存在两个不同的索引i和j,使得nums [i] = nums [j],并且 i 和 j的差的 绝对值 至多为 k。 示例1: 输入: nums = [1,2,3,1], k = 3 输出: true 示例 2: 输入: nums = [1,0,1,1], k ...

isabloomingtree的博客 217

LeetCode //C - 219. Contains Duplicate II

【代码】LeetCode //C - 219. Contains Duplicate II。

Made in Code 382

LeetCode数组专题: 217. Contains Duplicate && 219. Contains Duplicate II

“来啦来啦!!!” 217.Contains Duplicate 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. Example 1:I

qq_36010523的博客 218

Contains Duplicate系列三连- set,map的应用

又做完了一个系列,感觉收获良多。一共三道题,第一道和第二道都是easy,第三道是medium,不过我感觉第三道更接近hard。217. Contains DuplicateGiven an array of integers, find if the array contains any duplicates. Your function should return true if any val...

lujie1996的博客 266
上一篇: 【JAVA】leetcode刷题基础:Queue队列(Queue、PriorityQueue)
下一篇: 【JPA】JPA中的API
AXIMI
博客等级 码龄10年 53粉丝 455原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值