Leetcode #217 Contains Duplicate

来源:互联网 发布:屏幕视频截取软件 编辑:程序博客网 时间:2024/06/07 05:03

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.


bool containsDuplicate(vector<int>& nums) {map<int ,int> hashmap;int position = 0;for(vector<int>::iterator it = nums.begin(); it != nums.end(); ++ it){map<int,int>::iterator mit = hashmap.find(*it);if(mit != hashmap.end())return true;else hashmap[*it] = position ++;}return false;}


0 0
原创粉丝点击