标签:
Problem:
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.
Solution:用hashmap来计数判断重复次数
class Solution { //map计数 public: bool containsDuplicate(vector<int>& nums) { unordered_map<int,int> mapping; if(nums.size()==0) return false; //初始化 for(int i=0;i<nums.size();i++) { mapping[nums[i]]=0; } for(int i=0;i<nums.size();i++) { if(++mapping[nums[i]]>=2) return true; } return false; } };
标签:
原文地址:http://www.cnblogs.com/xiaoying1245970347/p/4560258.html