码迷,mamicode.com
首页 > 编程语言 > 详细

219. 数组重复元素2 Contains Duplicate II

时间:2017-04-12 02:38:39      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:mil   whether   dict   duplicate   back   contains   near   class   size   

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.


  1. public class Solution {
  2. public bool ContainsNearbyDuplicate(int[] nums, int k) {
  3. Dictionary<int, int> dict = new Dictionary<int, int>();
  4. for (int i = 0; i < nums.Length; i++) {
  5. int index;
  6. if (dict.TryGetValue(nums[i], out index)) {
  7. if (i - index <= k) {
  8. return true;
  9. }
  10. }
  11. dict[nums[i]] = i;
  12. }
  13. return false;
  14. }
  15. }







219. 数组重复元素2 Contains Duplicate II

标签:mil   whether   dict   duplicate   back   contains   near   class   size   

原文地址:http://www.cnblogs.com/xiejunzhao/p/9c2a98f21d2e33e6a295a9cf45a58da1.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!