首页 > 其他 > 详细

leetcode Contains Duplicate

时间:2015-11-13 14:40:30      阅读:308      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!