LeetCode | 219. Contains Duplicate II 循环技巧题高效率方法
2018-01-07 15:58
405 查看
Given an array of integers and an integer k, find out whether there aretwo distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference
between i and j is at most k.
这题,给你一个数组,问你这个数组里面有没有值相等,但是索引距离小于k的两个数存在,
我的方法beat 94.74%的人
我的思路是首先把数组排序,然后相同的数一定会相邻,当一个数和它相邻的数值相等的时候,比较他们在索引数组里面的索引的值的差是否小于k,如果小于则输出true,这里的索引数组用pair<int,int>来实现了,前一个int用来记录这个数的索引,后一个int用来记录这个数的值
class Solution {
public:
static bool cmp(pair<int, int> a, pair<int, int> b)
{
return b.second > a.second;
}
bool containsNearbyDuplicate(vector<int>& nums, int k) {
vector<pair<int, int>> number;
for (int i = 0; i < nums.size(); i++)
{
number.push_back(pair<int, int>(i, nums[i]));
}
sort(number.begin(), number.end(), cmp);
bool mark = false; int end;
for (int i = 0; i < number.size(); i++)
{
if (i + 1 < number.size() && number[i + 1].second == number[i].second)
{
end = i + 2;
while (end < number.size() && number[end].second == number[i].second) end++;
for(int l=i;l<end-1;l++)
for (int p = l + 1; p < end; p++)
{
if (abs(number[l].first - number[p].first) <= k) return true;
}
i = end;
}
}
return false;
}
};
between i and j is at most k.
这题,给你一个数组,问你这个数组里面有没有值相等,但是索引距离小于k的两个数存在,
我的方法beat 94.74%的人
我的思路是首先把数组排序,然后相同的数一定会相邻,当一个数和它相邻的数值相等的时候,比较他们在索引数组里面的索引的值的差是否小于k,如果小于则输出true,这里的索引数组用pair<int,int>来实现了,前一个int用来记录这个数的索引,后一个int用来记录这个数的值
class Solution {
public:
static bool cmp(pair<int, int> a, pair<int, int> b)
{
return b.second > a.second;
}
bool containsNearbyDuplicate(vector<int>& nums, int k) {
vector<pair<int, int>> number;
for (int i = 0; i < nums.size(); i++)
{
number.push_back(pair<int, int>(i, nums[i]));
}
sort(number.begin(), number.end(), cmp);
bool mark = false; int end;
for (int i = 0; i < number.size(); i++)
{
if (i + 1 < number.size() && number[i + 1].second == number[i].second)
{
end = i + 2;
while (end < number.size() && number[end].second == number[i].second) end++;
for(int l=i;l<end-1;l++)
for (int p = l + 1; p < end; p++)
{
if (abs(number[l].first - number[p].first) <= k) return true;
}
i = end;
}
}
return false;
}
};
相关文章推荐
- 【leetcode】219. Contains Duplicate II(Python & C++)
- Populating Next Right Pointers in Each Node II [leetcode] 空间O(1)的基于循环和基于递归的两种方法
- Leetcode 219. Contains Duplicate II (Easy) (cpp)
- LeetCode 219. Contains Duplicate II ***** 滑动窗口,查找表 217.Contains Duplicate
- Leetcode:219. Contains Duplicate II(数组是否含有重复元素)
- leetcode_219. Contains Duplicate II ? 带解决
- LeetCode 219. Contains Duplicate II(检查重复)
- LeetCode 219. Contains Duplicate II (包含重复项之二)
- leetcode 219. Contains Duplicate II | 滑窗, Set
- Leetcode219. Contains Duplicate II(重新修改)
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
- LeetCode 219. Contains Duplicate II(C++版)
- leetCode 219. Contains Duplicate II 数组
- Leetcode:219. Contains Duplicate II(JAVA)
- leetcode 219. Contains Duplicate II---java
- LeetCode: Remove Duplicates from Sorted List II
- [leetcode笔记] Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted Array II -- LeetCode
- LeetCode | 6. ZigZag Conversion 循环技巧题