题目:
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.
这题利用unordered_map来解决应该还是很简单的。
class Solution { public: bool containsDuplicate(vector<int>& nums) { unordered_map<int, int> map; for (int i = 0; i < nums.size(); i++){ if (map.find(nums[i]) != map.end()){ return true; } else{ map[nums[i]] = i; } } return false; } };
时间: 2024-10-01 04:54:42