1 题目

Contains Duplicate I

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.

接口: public boolean containsDuplicate(int[] nums)

Contains Duplicate II

Given an array of integers and an integer k, find out whether there 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.

接口public boolean containsNearbyDuplicate(int[] nums, int k)

2 思路

I:判断数组中有无重复元素。用HashTable的思想,实现用HashSet来做。

复杂度: Time:O(n);Space:O(n)

II : 在I的基础上,判断重复的元素相距离的位置是否在k范围之内。用HashTable的思想,实现用HashMap来做,<key, value> = <数组值,下标index>

复杂度: Time:O(n);Space:O(n)

注意:如何更新Map的值?考虑一下注意的测试用例 {1,0,1,1},1

3 代码

I

	public boolean containsDuplicate(int[] nums) {
int len = nums.length;
Set<Integer> set = new HashSet<Integer>(len);
for (int num : nums) {
if (set.contains(num)) {
return true;
} else {
set.add(num);
}
}
return false;
}

II

	public boolean containsNearbyDuplicate(int[] nums, int k) {
int len = nums.length;
Map<Integer, Integer> map = new HashMap<Integer, Integer>(len);
for (int i = 0; i < len; i++) {
if (map.containsKey(nums[i])) {
int diff = i - map.get(nums[i]);
if (diff <= k)
return true;
}
map.put(nums[i], i); // 不管有没有这个num,都要更新Map的值。为了通过这样的测试用例:{1,0,1,1},1
}
return false;
}

4 总结

解题的思想,主要是HashTable。想到就比较简单了。

5 参考

leetcode面试准备:Contains Duplicate I && II的更多相关文章

  1. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  2. leetcode面试准备:Reverse Words in a String

    leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...

  3. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  4. leetcode面试准备: Game of Life

    leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...

  5. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  6. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  7. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  8. leetcode面试准备:Triangle

    leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...

  9. leetcode面试准备:Sliding Window Maximum

    leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...

随机推荐

  1. java Spring 基于注解的配置(一)

    注解引用:1.service.xml 配置注解模式 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  2. Android测试分析二

    什么是android测试,分为黑盒测试和白盒测试. 黑盒就是测试人员看不到代码的,针对需求而进行的一系列测试动作,看代码所展现出来的效果是否和需求一样,或者有什么意外的情况没有处理等,一般开发交给测试 ...

  3. Sidebar Enhancements使用说明

    SideBarEnhancements是Sublime侧栏文件与文件夹的增强工具 Version: 2015.5.27 通过使用Sumbime Text3安装包工具(Package Control,快 ...

  4. 例子: 自制Flask首页导航.

    # -*- coding:utf-8 -*- ''' Created on 2015年10月19日 ''' from flask import Flask, render_template impor ...

  5. (hdu)5652 India and China Origins 二分+dfs

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...

  6. (hdu)5547 Sudoku (4*4方格的 数独 深搜)

    Problem Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game ...

  7. 切割TOMCAT日志

    tomcat的catalina.out日志如果不做操作的话,日志就会日积月累的不断增加.我刚入职的时候发现某台服务器的硬盘报警,排查之后我慌了,一个tomcat的日志居然有100G,这怎么可以,在网上 ...

  8. 菜鸟的MySQL学习笔记(五)

    7.自定义函数 用户自定义函数(user-defined function,UDF)是一种对MySQL扩展的途径,其用法与内置函数相同.包含了两个必要条件,参数与返回值.没有必然内在联系. 函数可以返 ...

  9. ubuntu 12.04 mysql转移目录后 无法 启动

    http://www.boyunjian.com/do/article/snapshot.do?uid=com.iteye.xgbjmxn%2Fblog%2F1208086(转,)     我是用ap ...

  10. leetcode problem 10 Regular Expression Matching(动态规划)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...