Skip to content

Commit

Permalink
Sort,Hash Table
Browse files Browse the repository at this point in the history
  • Loading branch information
gzwl committed Jun 24, 2015
1 parent a8bd9f6 commit af00752
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Contains Duplicate II/Contains Duplicate II.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//哈希表做法
//哈希表做法

class Solution {
public:
Expand All @@ -7,18 +7,18 @@ class Solution {
for(int i = 0;i < nums.size();i++){
int id = mapint[nums[i]];
if(id == 0){
mapint[nums[i]] = i + 1; //这里存的是值的下标加1 因为0表示没有这个元素
mapint[nums[i]] = i + 1; //这里存的是值的下标加1 因为0表示没有这个元素
}
else if(i - id < k) return true;
else{
mapint[nums[i]] = i + 1; //如果相同元素下标的差大于k,要用后面的下标代替前面的
mapint[nums[i]] = i + 1; //如果相同元素下标的差大于k,要用后面的下标代替前面的
}
}
return false;
}
};

//排序做法 从结果看比上面要快一些 但复杂度其实是一样的
//排序做法 从结果看比上面要快一些 但复杂度其实是一样的
struct state
{
int val,id;
Expand Down Expand Up @@ -48,3 +48,4 @@ class Solution {
return false;
}
};

0 comments on commit af00752

Please sign in to comment.