[LeetCode]Find All Duplicates in an Array
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
Question
Given an array of integers, 1 ≤ a[i] ≤ n
(n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
Example:
本题难度Medium。
【复杂度】
时间 O(N) 空间 O(1)
【思路】
与[LeetCode]Find All Numbers Disappeared in an Array几乎一样,不同之处就是找出twice的数。那么我们只要当nums[i]!=i+1
时,然后又当nums[nums[i]-1]==nums[i]
时,把nums[i]
放入set即可。
【代码】
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |