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

leetcode 217

时间:2016-12-18 23:23:12      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:duplicate   sof   div   end   tor   vector   any   log   mil   

217. Contains Duplicate

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.

题意:判断数组中是否有重复的元素,如果没有返回false,反之返回true.

解法:先对数组进行排序,然后比较排序之后数组相邻元素。

代码如下:

 

 1 class Solution {
 2 public:
 3     bool containsDuplicate(vector<int>& nums) {
 4         int size = nums.size();
 5         if(size == 0 || size == 1)
 6         {
 7             return false;
 8         }
 9         sort(nums.begin(), nums.end());
10         for(int i = 1; i < nums.size(); i++)
11         {
12             if(nums[i-1] == nums[i])
13             {
14                 return true;
15             }
16         }
17         return false;
18     }
19 };

 

leetcode 217

标签:duplicate   sof   div   end   tor   vector   any   log   mil   

原文地址:http://www.cnblogs.com/shellfishsplace/p/6195498.html

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