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

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

【leetcode】Contains Duplicate的相关文章

【Leetcode】Delete Duplicate Emails

题目链接:https://leetcode.com/problems/delete-duplicate-emails/ 题目: Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +--+------+ | Id | Email | +--+------+ | 1 | [email 

【leetcode】Contains Duplicate &amp; Rectangle Area(easy)

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. 思路:简单题用set bool cont

【LeetCode】Contains Duplicate II

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 i and j is at most k. 算法思想 思想一: 两个数组下标i,j,在叫j-i<

【leetcode】1089. Duplicate Zeros

题目如下: Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written. Do the above modifications to the input arr

【LeetCode】Contains Duplicate II 解题小结

题目:Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 题目意思就是判断在一个数组中是否存在两个相同的元素,他们之间的距离在k以内. 自己开始的思路是嵌套的两个for循

【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】树(共94题)

[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 - n 的所有形态的BST. 题解:枚举每个根节点 r, 然后递归的生成左右子树的所有集合,然后做笛卡尔积. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *

【LeetCode】二叉查找树 binary search tree(共14题)

链接:https://leetcode.com/tag/binary-search-tree/ p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [220]Contains Duplicate III [315]Count of Smaller Numbers After Self [327]Count of Range Sum [352]Data Stream as Disjoint Intervals [493]