Contain Duplicate III*******

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.

Analyse:

1. Traversal each element with the remaining elements in the array. If requirements satisfied, return true.

Time Exceeded Limited.

 1 class Solution {
 2 public:
 3     bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
 4         for(int i = 0; i < nums.size(); i++){
 5             for(int j = i + 1; j < nums.size() && j - i <= k; j++){
 6                 if(nums[j] - nums[i] <= t && nums[j] - nums[i] >= -t) return true;
 7             }
 8         }
 9         return false;
10     }
11 };

 1 class Solution {
 2 public:
 3     bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
 4         for(int i = 0; i < nums.size(); i++){
 5             for(int j = i + 1; j < nums.size(); j++){
 6                 if(nums[j] - nums[i] > t || nums[j] - nums[i] < -t) continue;
 7                 if(j - i <= k) return true;
 8             }
 9         }
10         return false;
11     }
12 };

2.

Runtime: 20ms.

 1 class Solution {
 2 public:
 3     static bool cmpptr(int *a, int *b){
 4         return *a < *b;
 5     }
 6     bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
 7         const int N = nums.size();
 8         vector<int*> numptrs(N);
 9         for(int i = 0; i < N; ++i){
10             numptrs[i] = &nums[i];
11         }
12         sort(numptrs.begin(), numptrs.end(), cmpptr);
13         for(int i = 0; i < N; i++){
14             for(int j = i + 1; j < N; j++){
15                 //nums[i] and nums[j] is at most t
16                 if((*numptrs[j]) > (*numptrs[i]) + t)
17                     break;
18                 //the difference between i and j is at most k
19                 if(abs(numptrs[j] - numptrs[i]) <= k) return true;
20             }
21         }
22         return false;
23     }
24
25 };
时间: 2024-12-14 09:17:35

Contain Duplicate III*******的相关文章

[LeetCode][Java]Contains Duplicate III

Contains Duplicate III 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. 数组中是否存在两个元素,他们的

Leetcode解题笔记-Contains Duplicate &amp;&amp; Contains Duplicate II&amp;&amp;Contain Duplicate III

Contain Duplicate 题目要求: 给定一个数组如果有重复就返回true,如果没有就返回false 个人分析: 这个题比剔除重复的要简单许多,只需要判断就好 代码如下: public static boolean containsDuplicate(int[] nums){ if (nums.length==0) return false; Arrays.sort(nums); for(int i =1; i < nums.length; i++){ if(nums[i-1]==nu

Contains Duplicate,Contains Duplicate II,Contains Duplicate III

217. Contains Duplicate 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. 判断数组是是否含有重复元素.

Contains Duplicate III -leetcode

Contains Duplicate III 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. 关于上述题意,我们可以转化为两

leetcode笔记:Contains Duplicate III

一. 题目描述 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. 二. 题目分析 题目大意是,给定一个整数数组,判断其中是否存

Leetcode 220 Contains Duplicate III

1. 问题描述 给定一个整数数组nums[],查找是否存在两个下标i和j,满足|numsi?numsj|≤t 且 |i?j|≤k. 2. 方法与思路 总得思路就是:"滑动窗口"+unordered_map. 推理过程如下: |numsi?numsj|≤t?|numsi/t?numsj/t|≤1: 由上式可以推出:|?numsi/t???numsj/t?|≤1 等价于:?numsi/t?∈{?numsj/t?,?numsj/t??1,?numsj/t?+1}. 我们只需要维持一个大小为K

Leetcode: Contains Duplicate III

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. 暴力枚举会LTE,解题思路是用sortedSet来解决,sortedSet可

LeetCode——Contains Duplicate III

Description: 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. 题目大意:给定一个数组,和两个数t和k.判断是否满

Contains Duplicate III

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. ref: http://bookshadow.com/weblog/2015

220. Contains Duplicate III

1 class Solution { 2 public: 3 bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { 4 if (nums.size() < 2) return false; 5 multimap<int, int> m; 6 for (int i = 0; i < nums.size(); ++i) { 7 m.insert(pair<int, int&g