Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (47 sloc) 1 KB
/*
* @lc app=leetcode.cn id=217 lang=cpp
*
* [217] 存在重复元素
*
* https://leetcode-cn.com/problems/contains-duplicate/description/
*
* algorithms
* Easy (46.26%)
* Total Accepted: 49.6K
* Total Submissions: 104.2K
* Testcase Example: '[1,2,3,1]'
*
* 给定一个整数数组,判断是否存在重复元素。
*
* 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。
*
* 示例 1:
*
* 输入: [1,2,3,1]
* 输出: true
*
* 示例 2:
*
* 输入: [1,2,3,4]
* 输出: false
*
* 示例 3:
*
* 输入: [1,1,1,3,3,4,3,2,4,2]
* 输出: true
*
*/
//hash_table unoredered_map
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int,int> m;
for(int i=0;i<nums.size();i++)
{
//if(m.find(nums[i])!=m.end()) return true;
if(m.count(nums[i])!=0) return true;
++m[nums[i]];
}
return false;
}
};
You can’t perform that action at this time.