Contains Duplicate

 1 class Solution {
 2 public:
 3     bool containsDuplicate(vector<int>& nums) {
 4         int n=nums.size();
 5         if(n==0)
 6             return 0;
 7         int i;
 8         map<int,int> mp;
 9         for(i=0;i<n;i++)
10         {
11             if(mp[nums[i]]==0)
12                 mp[nums[i]]=1;
13             else
14                 return 1;
15         }
16         return 0;
17     }
18 };
View Code

使用stl 中的map,map是一个关联容器 ,存储<关键字,值>对,自动按关键字排序,使用rbtree实现,查询,插入 ,删除都能在O(logN)内快速完成

原文地址:https://www.cnblogs.com/varcom/p/4557769.html