国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

219. Contains Duplicate II---數組中兩個重復的數字的下標最多相差k

2019-11-08 03:23:38
字體:
來源:轉載
供稿:網友

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. 方法一、hash

bool containsNearbyDuplicate(vector<int>& nums, int k){ int len = nums.size(); if(len<0) { return false; } map<int,int> mp; for(int i=0; i<len; i++) { if(mp.find(nums[i]) != mp.end()) { if((i-mp[nums[i]])<=k) { return true; break; } else { mp[nums[i]] = i; } } else { mp.insert(pair<int,int>(nums[i],i)); } } return false;}

方法二、集合的方法

bool containsNearbyDuplicate(vector<int>& nums, int k) { set<int> cand; for (int i = 0; i < nums.size(); i++) { if (i > k) cand.erase(nums[i-k-1]); if (!cand.insert(nums[i]).second) return true; } return false; }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日照市| 龙泉市| 合川市| 金塔县| 公安县| 谢通门县| 辉县市| 额敏县| 海盐县| 准格尔旗| 江油市| 海城市| 荔波县| 根河市| 灌南县| 岫岩| 奇台县| 龙门县| 长葛市| 嘉荫县| 甘泉县| 闻喜县| 莱阳市| 昌都县| 内黄县| 丽江市| 友谊县| 台安县| 拉萨市| 田林县| 环江| 曲阳县| 泸水县| 峨眉山市| 武功县| 玛曲县| 碌曲县| 普格县| 稷山县| 锡林浩特市| 中山市|