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. 错误编码 = 10022 错误消息 = SDK 组件 Qupaisdk 启动出错,错误消息为 [Qupaisdk], the android stack error message is Fail to start the plugin, which is caused by No implem

     so没有load到.几个可能,1.缺少so--在群共享下载拷贝到armeabi-v7a 2.so没有打入apk--检查打出来的apk.解压打开看下libs下面有没有so. 3.abi平台问题.检查平 ...

  2. json 转 javaBean

    前言:经常在网络上看见一些关于json自动转换成javaBean的jar包,项目组里的人也在用,稍稍研究了下,都是用的反射来做的.我细细想了下里面的逻辑,我觉得直接生成JavaBean代码岂不是更加直 ...

  3. WildFly 9.0.2 启用 SSL

    一.最近做个项目是需要在WildFly中启用https,但是由于WildFly的中文文档比较少所以google了一下,先是通过JBOSS的官方文档了解了一下,但是官方文档这块的配置介绍有些不全面.所以 ...

  4. c常用字符串函数

    获取字符串长度 : size_t strlen(const char *str); 字符串拷贝函数: 把src中内容拷贝到dest中,它会覆盖原来的内容,它会把src中的\0,没有覆盖内容不变 如果s ...

  5. webapi之jsonp调用

    定义跨域handle public class CorsHandler : DelegatingHandler { const string Origin = "Origin"; ...

  6. spring mvc 笔记

    springmvc 课堂笔记 1.Springmvc是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想, ...

  7. asmdisk 丢失问题一次记录

    环境 vm12 workstation ,11.2R 在安装RAC 第二台机器不显示磁盘的是问题 , oracleasm listdisks 查询没有结果 , 于是执行 oracleasm scand ...

  8. mysql主从之主键冲突

    收到短信报警,两台数据库都报slave同步失败了,先说明一下环境,架构:lvs+keepalived+amoeba+mysql,主主复制,单台写入, 主1:192.168.0.223(写) 主2:19 ...

  9. input表单

    submit:点击submit按钮表单就会被提交给服务器,中文IE下默认按钮文本为“提交查询”,可以设置value属性修改按钮的显示文本 text:size属性为宽度,value为值,maxlengt ...

  10. dom三个事件

    1,页面加载后 window.onload=function(){}; 2,页面(关闭)卸载后触发 window.onunload=function(){}; 3,页面关闭前触发 window.onb ...