领扣LintCode算法问题答案-1236. 查找数组中没有出现的所有数字
发布日期:2021-06-30 17:10:30 浏览次数:2 分类:技术文章

本文共 1006 字,大约阅读时间需要 3 分钟。

领扣LintCode算法问题答案-1236. 查找数组中没有出现的所有数字

目录

1236. 查找数组中没有出现的所有数字

描述

给定一个整数数组,其中1 ≤ a[i] ≤ n (n =数组的大小),一些元素出现两次,其他元素出现一次。

找到 [1,n] 中所有没有出现在此数组中的元素。

你可以在没有额外空间和O(n)运行时的情况下完成吗? 您可以认为返回的列表不计为额外空间。

样例 1:

输入:[4,3,2,7,8,2,3,1]输出:[5,6]

题解

public class Solution {
/** * @param nums: a list of integers * @return: return a list of integers */ public List
findDisappearedNumbers(int[] nums) {
// write your code here if (nums.length == 0) {
return new ArrayList
(); } for (int i = 0; i < nums.length; i++) {
int number = nums[i]; int real = number; if (number < 0) {
real = -number; } if (nums[real - 1] < 0) {
continue; } nums[real - 1] = -nums[real - 1]; } List
ret = new ArrayList
(); for (int j = 0; j < nums.length; j++) {
if (nums[j] > 0) {
ret.add(j + 1); } } return ret; }}

鸣谢

非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。

欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。

转载地址:https://le-yi.blog.csdn.net/article/details/108907431 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:领扣LintCode算法问题答案-1237. 回旋镖的数量
下一篇:【精】LintCode领扣算法问题答案:1231. 使数组元素相同的最少步数

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月25日 01时16分35秒