leetcode之Contains Duplicate

本文将展示如何利用Python的Counter函数和集合(set)特性来高效判断数组中是否存在重复元素,并提供了一种更快的解决方案。通过实际代码演示,旨在帮助读者理解并应用这些技巧解决类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继续写leetcode啦,既上次发现了Counter函数之后,又发现了用武之地啦。这次的题目简直就是撞枪口了。检查是否有重复数。太简单啦。代码如下:

class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        from collections import Counter
        c = Counter(nums)
        for i in c.keys():
        	if c[i] > 1:
        		return True
        else:
        	return False
洗澡的时候想起来一种更简单的方法,用了以后果然更快了。就是用set的自动消除重复的特点
<pre name="code" class="python">class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        if len(set(nums)) != len(nums):
            return True
        else:
            return False




### LeetCode MySQL Problems and Solutions #### Problem 1: Sales Analysis III Given three tables `Product`, `Sales` with the following structure: | Column Name | Type | |-------------|----------| | product_id | int | | product_name| varchar | | Column Name | Type | |-------------|----------| | seller_id | int | | product_id | int | | buyer_id | int | | sale_date | date | | quantity | int | | price | int | The task is to find products that were only sold in a specific year. ```sql SELECT DISTINCT p.product_id, p.product_name FROM Product AS p JOIN Sales AS s ON p.product_id = s.product_id WHERE YEAR(s.sale_date) = '2018' AND p.product_id NOT IN ( SELECT product_id FROM Sales WHERE YEAR(sale_date) != '2018') ``` This query selects distinct product IDs and names from the `Product` table where sales occurred exclusively in 2018 by filtering out any products sold outside this period[^1]. #### Problem 2: Big Countries A world table contains columns like name, continent, area, population, gdp. The goal is to list all countries larger than 3 million square kilometers or having more than 25 million people. ```sql SELECT name, population, area FROM world WHERE area > 3000000 OR population > 25000000; ``` This SQL statement retrieves country information based on specified size criteria using logical operators[^2]. #### Problem 3: Duplicate Emails With a Person table containing id and email fields, identify duplicate emails within it. ```sql SELECT Email , COUNT(*) as num FROM Person GROUP BY Email HAVING COUNT(*) > 1; ``` By grouping entries according to their email addresses and applying HAVING clause, one can easily spot duplicates[^3]. --related questions-- 1. How does JOIN operation work between two tables? 2. What are common aggregate functions used alongside GROUP BY statements? 3. Can you explain how subqueries function inside main queries? 4. In what scenarios should window functions be preferred over traditional aggregation methods?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值