LeetCode-448. Find All Numbers Disappeared in an Array C#
2024-10-10 07:47:57
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;
public class Solution {
public IList<int> FindDisappearedNumbers(int[] nums) {
int n = nums.Length;
IList<int> result = new List<int>();
for(int i=; i<n;i++)
{
int index = Math.Abs(nums[i]);
if(nums[index-]>)
{
nums[index-]=-nums[index-];
}
}
for(int i=; i<n;i++)
{
if(nums[i]>)
{
result.Add(i+);
}
}
return result;
}
}
最新文章
- iOS - 捕获应用程序崩溃日志
- 做的一个HTML表白页面
- x01.Weiqi.9: 点目功能
- Android 开发之旅:view的几种布局方式及实践
- NK3C开发要点
- Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例
- U盘安装CentOS无法进入Centos系统解决办法
- python异常以及面向对象编程
- HDU-1846 Brave Game
- ReportViewer导出功能筛选
- Dede推荐文章与热点文章不显示?
- Linux负载均衡软件LVS之二(安装篇)
- 我总结的常用sql语句
- vagrant扩容
- python如何转换word格式、读取word内容、转成html
- 关于oracle设置主键自增的问题
- 6L - 大菲波数
- VsCode编写和调试.NET Core
- java线程池实例
- java iterator