当前位置:网站首页>Leetcode find duplicates
Leetcode find duplicates
2022-06-13 01:09:00 【dddd_ jj】
The details of the questions are as follows :
Be careful : The title requires that the array cannot be modified , You can't just use O(1) Extra space .
Method 1 ( Used O(n) Space ):
Define a map<int,int>p, Traversal array nums, When you encounter an element in the array, you can see p Whether the current element has appeared , If it does , Then return the current element directly . Otherwise, it will be p The element marked in has already appeared .
Method 2 ( no need O(n) Space , But the array is modified ):
Because the array length is n+1, Then the range of array elements is [1,n]. So you can first subscript from the array to 0 Start traversing the elements of , Record nums[0] Value , Then jump to nums[0] Value as the element corresponding to the subscript , Determine whether the element is -1, If -1, shows nums[0] It has appeared , Go straight back to nums[0]. Otherwise, define a variable v Record nums[nums[0]] Value , Then change the current element to -1, And jump to v Element with subscript , And repeat the process .
Here is an example :


The right way
Suppose the methods and examples in method 2 , Suppose you don't modify the elements of the array , Then the traversal order of the array is :
4->2->3->2->3->2->3........
here , We can find out , The array will go into a ring [2->3]
We assume that , There are two pointers slow and fast,slow Every step the pointer takes ,fast The pointer takes two steps . hypothesis slow The pointer is gone n Step , that fast The pointer is gone 2n Step , here slow Pointers and fast The pointer meets in the ring . Suppose the number of steps from the beginning to the beginning of the ring is m, The circumference inside the ring is c.
So we can get slow The pointer went inside the ring n-m Step ,fast The pointer went inside the ring 2n-m,fast The pointer is in the loop slow The pointer has gone too far n Step . Two pointers can meet in a ring , that fast Pointer ratio slow The pointer went a few more circles , So there is n%c=0
At this point, we define a pointer finder Start from the beginning ,slow The pointer also starts from the current position , Until the two pointers meet , The place where they meet is the place at the beginning of the ring , That is, the number of repetitions we are looking for , Why? ?
When finder Go to No m Step by step , I went to the beginning of the ring . So at this time !!!!slow The pointer went inside the ring n-m+m=n Step . because n%c=0, therefore ,slow The pointer also goes to the beginning of the ring . therefore finder Pointers and slow The pointer will meet at the beginning of the ring .
According to the idea above , We have the following code :
in addition leetcode There are similar topics :
边栏推荐
- spiral matrix visit Search a 2D Matrix
- 在国企做软件测试工程师是一种什么样的体验:每天过的像打仗一样
- MySQL index
- Androi weather
- Rasa对话机器人之HelpDesk (三)
- . The way to prove the effect of throwing exceptions on performance in. Net core
- MySQL异常:com.mysql.jdbc.PacketTooBigException: Packet for query is too large(4223215 > 4194304)
- Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
- [latex] insert picture
- Tree - delete all leaf nodes
猜你喜欢

Alexnet implements image classification of caltech101 dataset (pytorch Implementation)

Alexnet实现Caltech101数据集图像分类(pytorch实现)

Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing

刘徽与《九章算术》《海岛算经》简介

Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing

FLIP动画实现思路

Traditional machine learning classification model predicts the rise and fall of stock prices

Quantitative investment traditional index investment decision vs Monte Carlo simulation method
![[JS component] previous queue prompt](/img/79/9839f68b191b0db490e9bccbeaae1d.jpg)
[JS component] previous queue prompt
![[JS component] dazzle radio box and multi box](/img/2a/00620bee312972db93e1db4313385f.jpg)
[JS component] dazzle radio box and multi box
随机推荐
Most elements leetcode
Rasa对话机器人之HelpDesk (三)
Pytorch's leafnode understanding
Unity calls alertdialog
[JS component] custom paging
Exercise 5.14 input n strings, arrange them in alphabetical order and output them.
Higherhrnet pre training model -- download from network disk
Loss calculation in pytorch
spiral matrix visit Search a 2D Matrix
Install pycharm process
np.concatenate中axis的理解
[JS component] dazzle radio box and multi box
Key point detection data preparation and model design based on u-net Network -- detection model of four key points of industrial components
Go simple read database
軟件測試的幾種分類,一看就明了
[Latex] 插入图片
Pipeline流水线项目构建
[JS component] customize the right-click menu
Androi weather
Four startup modes of kotlin collaboration