首页 > 编程语言 > 详细

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

时间:2017-04-12 02:38:39      阅读:296      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!