【LeetCode】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.

Solution 1: sort后相邻位比较

 1 class Solution {
 2 public:
 3     bool containsDuplicate(vector<int>& nums) {     //runtime:40ms
 4         if(nums.size()<=1)return false;
 5         sort(nums.begin(),nums.end());
 6         for(int i=1;i<nums.size();i++){
 7             if(nums[i-1]==nums[i])return true;
 8         }
 9         return false;
10     }
11 };

Solution 2: map记录已存在的int

 1 class Solution {
 2 public:
 3     bool containsDuplicate(vector<int>& nums) {     //runtime:104ms
 4         if(nums.size()<=1)return false;
 5         map<int,bool> m;
 6         for(int i=0;i<nums.size();i++){
 7             if(m[nums[i]]==true)return true;
 8             m[nums[i]]=true;
 9         }
10         return false;
11     }
12 };
时间: 2024-11-10 19:38:24

【LeetCode】217 - Contains Duplicate的相关文章

【LeetCode】217. Contains Duplicate (2 solutions)

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. 解法一: 排序,然后比较相邻元素是否相同

【LeetCode】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. 这题利用unordered_map来解决应该还是很简单的. class

【LeetCode】219 - Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k. 1 class Solution { 2 public: 3 bool containsNearbyDupl

【LeetCode】220. Contains Duplicate III

题目: Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 提示: 看到题目之后,马上想到了题目应该是要利用一个长度为k的划窗,

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n