Contains Duplicate
题目:难度(Easy)
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.
Tags:Array, Hash Table
分析:统计数字出现的次数是否有大于2的,另外return len(nums) != len(set(nums))一句话可实现
代码实现:
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
from collections import Counter
hash = Counter(nums)
for item in hash.items():
if item[1] > 1:
return True
return False
#return len(nums) != len(set(nums))#一句话可实现
Contains Duplicate II
题目:难度(Easy)
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 difference between i and j is at most k.
Tags: Array, Hash Table
分析:使用字典,注意dict.get()方法再没有找到对应键值时会返回默认值None,而不是抛出KeyError,且该默认值可以自己指定。字典中存储数组的元素值以及对应的下标。
代码实现:
class Solution(object):
def containsNearbyDuplicate(self, nums, k):
n = len(nums)
if n == 0 or k < 1:
return False
myDict = dict()#键是num值,值是num的下标
for i in range(n):
j = myDict.get(nums[i])
#nums[i]这个键已经存在在dict中
if j >= 0 and i-j<=k:
return True
myDict[nums[i]] = i#更新新的nums[i]这个键对应的下标
return False
Contains Duplicate III
题目:难度(Medium)
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.
Tags: Binary Search Tree
分析:“滑动窗口”+Java的TreeSet,TreeSet数据结构(Java)使用红黑树实现,是平衡二叉树的一种。该数据结构支持如下操作:
1. floor(num)方法返set中≤num的最大元素;如果不存在这样的元素,则返回 null。
2. ceiling(num)方法返回set中≥num的最小元素;如果不存在这样的元素,则返回 null。
代码实现:
public class Solution {
//法1:TreeSet
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(k < 1 || t < 0 || nums.length < 2)
return false;
TreeSet<Integer> set = new TreeSet<Integer>();
for(int j = 0; j < nums.length; j++ ){
//nums=[-1,2147483647],k=1,t=2147483647,使用下面的条件判断在计算<pre name="code" class="java">set.ceiling(nums[j])-nums[j]<=t
即2147483647-(-1)会溢出,影响结果 //if(set.ceiling(nums[j])!=null&&(set.ceiling(nums[j])-nums[j]<=t)||set.floor(nums[j])!=null&&(nums[j]-set.floor(nums[j])<=t)) if(set.ceiling(nums[j])!=null&&set.ceiling(nums[j])<=nums[j]+t || set.floor(nums[j])!=null&&nums[j]<=set.floor(nums[j])+t) return true; if(j>=k) set.remove(nums[j-k]); set.add(nums[j]); } return false; } }
参考:
http://bookshadow.com/weblog/2015/06/03/leetcode-contains-duplicate-iii/
import java.util.SortedSet;
//法1:SortedSet
public class Solution {
public boolean containsNearbyAlmostDuplicate1(int[] nums, int k, int t) {
if(k < 1 || t < 0 || nums.length < 2)
return false;
SortedSet<Long> set = new TreeSet<Long>();
for(int j = 0; j < nums.length; j++ ){
//如果nums[j]=0,t=1,nums[j]-t=-1会导致fromkey > tokey,所以用long类型
if(!(set.subSet((long)nums[j]-t, (long)nums[j]+t+1)).isEmpty())
return true;
if(j >= k)
set.remove((long)nums[j-k]);
set.add((long)nums[j]);
}
return false;
}
}
参考: http://blog.csdn.net/xudli/article/details/46323247