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.

二. 题目分析

题目的大意是,给定一个整数数组,推断数组中是否包括反复的元素。若数组中随意一个数字出现了至少两次,函数返回true;否则,返回false

这里提供两个方法:

  1. 先对数组进行排序。然后遍历数组,若出现两个相邻元素的值同样时。表明有反复元素。返回true。反之返回false。
  2. 使用map来实现。遍历数组。每訪问一个元素。看其是否在map中出现。如已出现过。则存在反复元素,返回true;如没有,则将元素增加到map中。

三. 演示样例代码

// 方法一
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        int n = nums.size();
        if (n < 2) return false;
        sort(nums.begin(), nums.end());
        for (int i = 1; i < n; ++i)
            if (nums[i] == nums[i - 1]) return true;
        return false;
    }
};
// 方法二
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_map<int, int> numsMap;
        for (int i = 0; i < nums.size(); ++i) {
            if(numsMap.count(nums[i])){
                return true;
            }
            numsMap.insert(pair<int, int>(nums[i], i));
        }
        return false;
    }
};

四. 小结

相关的题目有:Contains Duplicate II 和 Contains Duplicate III。

时间: 2024-12-30 03:56:28

leetcode笔记:Contains Duplicate的相关文章

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

LeetCode:Contains Duplicate - 判断数组内是否有重复元素

1.题目名称 Contains Duplicate(判断数组内是否有重复元素) 2.题目地址 https://leetcode.com/problems/contains-duplicate/ 3.题目内容 英文: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

leetcode笔记:Pascal&amp;#39;s Triangle

一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 二. 题目分析 关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk

LeetCode:Contains Duplicate II - 判断数组内是否有重复元素2

1.题目名称 Contains Duplicate II(判断数组内是否有重复元素2) 2.题目地址 https://leetcode.com/problems/contains-duplicate-ii/ 3.题目内容 英文: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] = nu

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

Leetcode解题笔记-Contains Duplicate &amp;&amp; Contains Duplicate II&amp;&amp;Contain Duplicate III

Contain Duplicate 题目要求: 给定一个数组如果有重复就返回true,如果没有就返回false 个人分析: 这个题比剔除重复的要简单许多,只需要判断就好 代码如下: public static boolean containsDuplicate(int[] nums){ if (nums.length==0) return false; Arrays.sort(nums); for(int i =1; i < nums.length; i++){ if(nums[i-1]==nu

leetcode笔记: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. 二. 题目分析 题目大意是,给定一个整数数组,判断其中是否存

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. 大意: 给出一个int型的数组,判断数组是否包含了重复的数.如果有任何