Problem
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
ExampleGiven nums = [1,3,1], k = 1, t = 1, return false.
Solution Brute forcepublic class Solution { /** * @param nums: the given array * @param k: the given k * @param t: the given t * @return: whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. */ public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { // Write your code here for (int i = 0; i < nums.length; i++) { for (int j = i+1; j < nums.length; j++) { if (Math.abs(j-i) <= k && Math.abs(nums[i]-nums[j]) <= t) return true; } } return false; } }Maintain a size k window
public class Solution { /** * @param nums: the given array * @param k: the given k * @param t: the given t * @return: whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. */ public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { // Write your code here if (k < 1 || t < 0) return false; Mapmap = new HashMap<>(); for (int i = 0; i < nums.length; i++) { long reMappedNum = (long)nums[i] - Integer.MIN_VALUE; long bucket = reMappedNum / ((long)t + 1); if (map.containsKey(bucket) || (map.containsKey(bucket-1) && Math.abs(reMappedNum - map.get(bucket-1)) <= t) || (map.containsKey(bucket+1) && Math.abs(reMappedNum - map.get(bucket+1)) <= t)) { return true; } if (i >= k) { long lastBucket = ((long)nums[i-k] - Integer.MIN_VALUE) / ((long)t + 1); map.remove(lastBucket); } map.put(bucket, reMappedNum); } return false; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/76466.html
Problem 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 ...
Problem Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical...
摘要:和唯一的不同是組合中不能存在重復(fù)的元素,因此,在遞歸時將初始位即可。 Combination Sum I Problem Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T...
摘要:解法真的非常巧妙,不過這道題里仍要注意兩個細(xì)節(jié)。中,為時,返回長度為的空數(shù)組建立結(jié)果數(shù)組時,是包括根節(jié)點的情況,是不包含根節(jié)點的情況。而非按左右子樹來進(jìn)行劃分的。 Problem The thief has found himself a new place for his thievery again. There is only one entrance to this area,...
摘要:去掉最后一個保留最后一個保留最后一個保留第一個這道題在論壇里參考了和兩位仁兄的解法。思想是將中所有的數(shù)進(jìn)行異或運算。不同的兩個數(shù)異或的結(jié)果,一定至少有一位為。最后,將和存入數(shù)組,返回。 Problem Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example Given [1,2,2...
閱讀 2000·2023-04-25 16:53
閱讀 1442·2021-10-13 09:39
閱讀 606·2021-09-08 09:35
閱讀 1639·2019-08-30 13:03
閱讀 2121·2019-08-30 11:06
閱讀 1831·2019-08-30 10:59
閱讀 3188·2019-08-29 17:00
閱讀 2288·2019-08-23 17:55