当前位置:网站首页>Find all missing numbers in the array
Find all missing numbers in the array
2022-07-01 18:34:00 【Free dreamer】

We can mark the array directly , If there has been , We can turn it into a negative number , If it's negative , Then it means that this number often appears , Greater than 0 It means that there has been no .
step :
1. Traverse each number in the entire array , If not, multiply by -1, Here, the array subscript represents each number in the array
Be careful : because vector The subscript in is from 0 To n-1, The data range is from 1 To n So counting to the array requires -1 go in , Then there may be points appearing twice , Therefore, the number of current traversal should be changed into a positive number in advance ;
2. Traversal array , If there is an array, the following table is positive , It means there is no such thing as , We will i+1, Save to array .
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
for(auto c:nums){
c=abs(c);
if(nums[c-1]>0)nums[c-1]*=-1;
}
vector<int>res;
for(int i=0;i<nums.size();i++){
if(nums[i]>0)res.push_back(i+1);
}
return res;
}
};边栏推荐
- Convert the robot's URDF file to mujoco model
- Bernoulli distribution (a discrete distribution)
- Is the fund of futures account safe? How to open an account?
- Review Net 20th anniversary development and 51aspx growth
- JS how to convert a string with a delimiter into an n-dimensional array
- Happy new year | 202112 monthly summary
- Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
- SCP -i private key usage
- 540. Single element in ordered array
- [CF559E]Gerald and Path
猜你喜欢

Flex layout

Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)

The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup

Yuancosmos game farmersworld farmers world - core content of the second conference in China!

. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
![Htt [ripro network disk link detection plug-in] currently supports four common network disks](/img/e4/c06b94243cdbe391b8b2124ff30789.jpg)
Htt [ripro network disk link detection plug-in] currently supports four common network disks

Set the style of QT property sheet control

Cloud computing - make learning easier
![[PHP foundation] realize the connection between PHP and SQL database](/img/eb/c8953eddfe3b19b0adb5529957d275.jpg)
[PHP foundation] realize the connection between PHP and SQL database

Cassette helicopter and alternating electric field magnetic manometer DPC
随机推荐
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
[acnoi2022] color ball
MySQL + JSON = King fried
Key points on February 15, 2022
Is Alipay wallet convenient to use?
Explain in detail the process of realizing Chinese text classification by CNN
Is Huishang futures a regular futures platform? Is it safe to open an account in Huishang futures?
Mysql database design
Fix the black screen caused by iPhone system failure
Penetration practice vulnhub range Nemesis
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
Common design parameters of solid rocket motor
12. Design of power divider for ads usage record
JS how to convert a string with a delimiter into an n-dimensional array
MES production equipment manufacturing execution system software
Sanfeng cloud 0215 I often use
Vue uses keep alive to cache page optimization projects
Step size of ode45 and reltol abstol
[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out
Penetration practice vulnhub range Tornado