如何同时寻找缺失和重复的元素 :: labuladong的算法小抄 #1041
Replies: 10 comments 1 reply
-
东哥,"是否可以在 O(1) 的空间复杂度之下找到重复和确实的元素呢?","缺失"敲成了"确实" |
Beta Was this translation helpful? Give feedback.
-
@Joycn2018 感谢指出,我改下~ |
Beta Was this translation helpful? Give feedback.
-
645这个有点疑惑当输入为 |
Beta Was this translation helpful? Give feedback.
-
这个方法好妙,想不出来😥 |
Beta Was this translation helpful? Give feedback.
-
要看清楚题目哦,题目中说先找到重复的,再找到缺失的。 |
Beta Was this translation helpful? Give feedback.
-
nums[index]也没说放的就是值是Index的元素呀... |
Beta Was this translation helpful? Give feedback.
-
如果给出的数组不是[1...n], 而是[0,n-1]和索引一一对应,那么当nums[dup]==0时,也就是重复索引下对应的元素为0时,是找不到重复的数字的,同样,当缺失数字索引下对应的元素为0时,也是找不到缺失的数字的 |
Beta Was this translation helpful? Give feedback.
-
如果这行: |
Beta Was this translation helpful? Give feedback.
-
感觉两个for循环 可以合并啊,没必要再单独照一次,nums[index] < 0 说明i就是不存在的元素 |
Beta Was this translation helpful? Give feedback.
-
打卡。非常巧妙的。index 对应的元素作为index所对应的元素*-1,做一轮的过程中,本身已经是负数,则重复了。做完了,还是正数,则缺失了。 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
文章链接点这里:如何同时寻找缺失和重复的元素
评论礼仪 见这里,违者直接拉黑。
Beta Was this translation helpful? Give feedback.
All reactions