LeetCode-448. Find All Numbers Disappeared in an Array C#

news/2024/7/3 8:44:26

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]

Solution:

Mark the value of already apeared index to negative, then loop it through to find the positive ones , then the index of positive ones is the result;

 1 public class Solution {
 2     public IList<int> FindDisappearedNumbers(int[] nums) {
 3         int n = nums.Length;
 4         IList<int>  result = new List<int>();
 5         for(int i=0; i<n;i++)
 6         {
 7             int index = Math.Abs(nums[i]);
 8             if(nums[index-1]>0)
 9             {
10                 nums[index-1]=-nums[index-1];
11             }
12         }
13         for(int i=0; i<n;i++)
14         {
15             if(nums[i]>0)
16             {
17                 result.Add(i+1);
18             }
19         }
20         return result;
21     }
22 }

 

转载于:https://www.cnblogs.com/MiaBlog/p/6206211.html


http://www.niftyadmin.cn/n/2436248.html

相关文章

MyBatisPlus 入门学习笔记(版本:3.4.3)

文章目录学习辅助资料MyBatisPlus概述1. MyBatisPlus是什么2. 特性快速开始1. 创建数据库 mybatis_plus2. 导入相关依赖3. 数据源配置3. 快速开始3.1 User实体类编写3.2 mapper编写3.3 启动类设置3.4 测试配置日志Mapper层自带的的CRUD方法1. Insert插入操作1.1 产生奇妙ID的原…

利用反射机制获取属性的值遇到的坑

类&#xff1a; public Class Test { public string name; public string value; } Test tnew Test(); t.name"abc"; t.value"123"; string str(string)t.GetType().GetProperty("name").GetValue(t,null); 找了3个小时&#xff0c;都找不出问题…

IDEA常用快捷键和Live Templates for Mac

1. IDEA常用快捷键 CmdShiftEnter&#xff1a;将输入的if&#xff0c;for&#xff0c;函数等等补上{}或者&#xff1b;使代码语句完整ShiftEnter&#xff1a;在当前行的下方开始新行OptEnter: 正则表达式验证CmdOptEnter&#xff1a;在当前行的上方插入新行OptEnter: 代码快速…

Qt 查找功能

版权声明该文章原创于Qter开源社区&#xff08;www.qter.org&#xff09;&#xff0c;作者yafeilinux&#xff0c;转载请注明出处&#xff01;导语这一篇我们来加上查找菜单的功能。因为要涉及Qt Creator的很多实用功能&#xff0c;所以单独用一篇文章来介绍。以前都用设计器设…

Git入门笔记

文章目录0. Git原理简述1. 设置用户签名 git config2. 初始化本地库 git init3. 查看本地库状态 git status4. 本地文件添加到暂存区 git add5. 暂存区的文件提交到本地库 git commit6. 查看历史版本 git reflog7. 修改文件后提交到本地库8. 版本穿梭9. Git分支9.1 查看分支9.2…

创建帧动画

Photoshop制作会跳动的文字动画效果和流动效果 --之心 新建一个大小适当文档&#xff0c;选择椭圆工具&#xff0c;按住Shift拉出一个正圆&#xff0c;然后锁定图层。选择渐变工具&#xff0c;将前景色与背景色分别设置为白色和任意深色。如下图中直线方向从上至下拖曳。得到小…

macOS IDEA等jetbrain全家桶,Clear Read-Only Status解决方法

问题 在编写代码的时候&#xff0c;代码因为只读而不让修改&#xff0c;并且跳出clear read-only status窗口。 主要原因在于&#xff0c;现在的macOS的权限设置导致编辑器没有权限修改代码。 解决办法 放开文件的写权限即可。 终端cd到项目的根目录后执行以下代码&#x…

imx6 关闭调试串口

需要关闭imx6调试串口&#xff0c;用作普通的串口使用。 参考链接 http://blog.csdn.net/neiloid/article/details/7585876 http://www.cnblogs.com/helloworldtoyou/p/5437867.html 更改kernel中配置&#xff0c; make menuconfig Symbol: SERIAL_IMX_CONSOLE [y] …