LeetCode-217
Links:https://leetcode.com/problems/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.
思路:STL模板库的unique函数判重即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public: bool containsDuplicate(vector<int>& nums) { //如果每一个元素都唯一,返回false,否则返回true vector<int> temp = nums; sort(temp.begin(),temp.end()); if(unique(temp.begin(),temp.end())-temp.begin() == nums.end()-nums.begin()) { return false; } else { return true; } } }; |
【LeetCode】217. Contains Duplicate