码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode Contains Duplicate

时间:2015-11-13 14:43:32      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

Subscribe to see which companies asked this question

 

用map做的很简单,不赘述

 1 class Solution {
 2 public:
 3     bool containsDuplicate(vector<int>& nums) {
 4         map<int,int> M1;
 5         int temp;
 6         if (nums.empty()) return false;
 7         for(auto i=nums.begin();i!=nums.end();i++){
 8             temp=(*i);
 9             ++M1[temp];
10         }
11         for(auto j=M1.begin();j!=M1.end();j++){
12             if((*j).second>1) return true;
13         }
14         return false;
15         
16     }
17 };

 

leetcode Contains Duplicate

标签:

原文地址:http://www.cnblogs.com/LUO77/p/4961849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!