leetcode之Contains Duplicate

本文介绍使用C++ STL中的两种方法来检测数组是否存在重复元素:一是通过排序后比较相邻元素;二是利用Set集合的唯一性特点进行检测。这两种方法相较于C和Java更简洁高效。

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

做题的时候有种感觉,就是同样一道题目,用C++的STL要比用C、java更容易一些,所以,很多用C++能轻松搞定的问题,换成了C和java还真得费一番功夫。

这里给出C++的两种做法,

一种是先排序,再相邻两两判断是否相等。含有重复数字的数组,经过排序后,必定由相邻两个数字是一样的。

第二种是用STL的集合Set,因为Set的元素是不能重复的,所以我们就先读数组里面的数字,一个一个放到Set里,当一个新读到的数字被Set找到的时候,就说明已经出现了重复。

(1)C++代码解法一:

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

(2)C++代码解法二:

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


C和java的实现算法暂时采用的是最笨的两个for循环,但是这样就出现了一个问题,就是出现了time limit exceeded 超时的现象,所以java和C语言的实现暂时还在考虑中。

### 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、付费专栏及课程。

余额充值