首页 > 其他分享 >闯关leetcode——3289. The Two Sneaky Numbers of Digitville

闯关leetcode——3289. The Two Sneaky Numbers of Digitville

时间:2024-11-06 08:49:03浏览次数:4  
标签:appear nums Sneaky two Two num numbers Digitville Numbers

大纲

题目

地址

https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/description/

内容

In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.

As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.

Example 1:

Input: nums = [0,1,1,0]
Output: [0,1]
Explanation:
The numbers 0 and 1 each appear twice in the array.

Example 2:

Input: nums = [0,3,2,1,3,2]
Output: [2,3]
Explanation:
The numbers 2 and 3 each appear twice in the array.

Example 3:

Input: nums = [7,1,5,4,3,4,6,0,9,5,8,2]
Output: [4,5]
Explanation:
The numbers 4 and 5 each appear twice in the array.

Constraints:

  • 2 <= n <= 100
  • nums.length == n + 2
  • 0 <= nums[i] < n
  • The input is generated such that nums contains exactly two repeated elements.

解题

这题就是要在一个数组中找到重复的数。方法就是使用一个set结构来做去重。

#include <set>
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> getSneakyNumbers(vector<int>& nums) {
        set<int> s;
        vector<int> result;
        for (auto num : nums) {
            if (s.find(num) == s.end()) {
                s.insert(num);
            } else {
                result.push_back(num);
            }
        } 
        return result;    
    }
};

在这里插入图片描述

代码地址

https://github.com/f304646673/leetcode/tree/main/3289-The-Two-Sneaky-Numbers-of-Digitville/cplusplus

标签:appear,nums,Sneaky,two,Two,num,numbers,Digitville,Numbers
From: https://blog.csdn.net/breaksoftware/article/details/142354644

相关文章

  • 【语义分割|代码解析】CMTFNet-4: CNN and Multiscale Transformer Fusion Network 用
    【语义分割|代码解析】CMTFNet-4:CNNandMultiscaleTransformerFusionNetwork用于遥感图像分割!【语义分割|代码解析】CMTFNet-4:CNNandMultiscaleTransformerFusionNetwork用于遥感图像分割!文章目录【语义分割|代码解析】CMTFNet-4:CNNandMultiscale......
  • [LeetCode] 3226. Number of Bit Changes to Make Two Integers Equal
    Youaregiventwopositiveintegersnandk.Youcanchooseanybitinthebinaryrepresentationofnthatisequalto1andchangeitto0.Returnthenumberofchangesneededtomakenequaltok.Ifitisimpossible,return-1.Example1:Input:n=13......
  • systemctl restart NetworkManager 重启后,文件/etc/resolv.conf修改失败
    如果你在重启NetworkManager之后发现无法修改/etc/resolv.conf文件,这是因为NetworkManager会自动管理这个文件为了解决这个问题,你可以采取以下两种方法之一:方法一:禁用NetworkManager服务使用以下命令停止NetworkManager服务:sudosystemctlstopNetworkMana......
  • Solution - Atcoder Atcoder ARC137C Distinct Numbers
    如果尝试去刻画这个问题,会发现非常复杂,于是不妨一步一步来。考虑Alice的第一步,此时Alice操作的位置是固定的。考虑把\(a_n\)移到一个位置后,接下来的\(\max\)是\(a_{n-1}\)或\(a_n\),Bob对应也只能这么操作。注意到Bob也有可能操作的是\(a_n\),这看起来就很特殊......
  • Virtual Private Network (VPN) Lab
    Task1:VMSetup使用上一个VPN的Labsetup包所构建的实验环境,所以这个任务就相当于是解决了。Task2:CreatingaVPNTunnelusingTUN/TAPStep1:自己构造tun_server.py,加权限并且在server上运行Step2:在HostU上构建tun_client.py,并运行tun_client.py文件:Step3......
  • 【深度学习】从公式推导来深入理解误差反向传播算法2:《深度学习入门基于Python的理论
    《深度学习入门基于Python的理论与实现》中实现了2层全连接神经网络的代码对MNIST数据集的28x28像素0-9手写数字灰度图像进行分类,本文将重点对代码中的two_layer_net类的gradient函数中的误差反向传播的代码进行公式推导验证。验证小批量数据的交叉熵损失函数对第2层权重......
  • COMP3331/9331 Computer Networks and Applications
    COMP3331/9331ComputerNetworksandApplicationsAssignmentforTerm3,2024Version1.1Due:11:59am(noon)Friday,8November2024(Week9)TableofContentsGOALANDLEARNINGOBJECTIVES....................................................................
  • CSCI 201 Networked Crossword Puzzle
    Assignment#2CSCI201Fall2024Page1of11Assignment#2CSCI201Fall20246%ofcoursegradeTitleNetworkedCrosswordPuzzleTopicsCoveredNetworkingMulti-ThreadingConcurrencyIssuesIntroductionThisassignmentwillrequireyoutocreatetwodiffe......