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.

解答说明:这个题目简直就是超级无敌太简单了好吧。在一个数组里面判断是否包含着重复元素。不说废话,贴代码了。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        map<int,int> shownums;
        for(int i = 0;i < nums.size();i++)
        {
            if(shownums.find(nums[i]) == shownums.end())
            {
                shownums[nums[i]] = 1;
            }
            else
            {
                return true;
            }
        }
        return false;
    }
};

时间: 2024-08-29 03:05:17

Leetcode题目:Contains Duplicate的相关文章

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: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题目:Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each n

leetcode题目思路以及部分解答(一)

为了进好公司这一个多月就得抽时间刷leetcode了..感觉这个OJ很不严谨...好多边界条件都没说清处..不过还好可以推测.唯一的好处就是不用自己编译调试,可以直接在网上显示出结果.当然,复杂一点的题目为了调试自己构建题目的结构也是很麻烦的...所以我发现提交里面错误好多.....再就是在笔记本上会时不时的变卡...每次提交都得等个3,4分钟才成功.要不就502错误... 我的题目按照通过率来.从通过率最高的题目开始讲解.每题不一定是最优解,都是我想得.仅供参考. 题目标题我都标好了.可以用c

leetcode题目思路以及部分解答(二)

又刷了30题了,这速度还不错.因为还有别的东西要复习,所以进度并不快.感觉还是能学到很多新东西的.早知道这个就不用去其他地方刷了.这个难度不高,还可以知道哪些情况没考虑.比其他OJ那种封闭式的好多了.还是进入正题吧. 1.Rotate Image 这个做过两三次了,但每次还是得重新开始推导..这次又推导了很久..不过好在做过,代码也写得比较简洁. 主要思路就是第一层循环按层次深入.第二层把旋转后对应替代的4个位置循环更新.swap就是用来更新用的.做完发现讨论里的最高票代码就是我这样子= =  

leetcode题目:Palindrome Partitioning 和Palindrome Partitioning II

题目一: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",

leetcode题目:Sum Root to Leaf Numbers和Longest Consecutive Sequence

题目一: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 /

leetcode题目:Copy List with Random Pointer

题目: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路: 主要是深层复制的问题: 本题比较简单,具体实现见代码: /** * Definition for singly-linked list with a ran

LeetCode 196. Delete Duplicate Emails (删除重复的电子邮箱)

题目标签: 题目给了我们一个 email 的表格,让我们删除重复的. 建立Person p1,Person p2,当email 相同时,而且 p1 id 要大于 p2 id 时候,删除这一行. Java Solution: Runtime:  869 ms, faster than 33 % Memory Usage: N/A 完成日期:06/01/2019 关键点:p1.Id > p2.Id # Write your MySQL query statement below DELETE p1