数组中消失的数--------------------------Find All Numbers Disappeared in an Array448

本文主要是介绍数组中消失的数--------------------------Find All Numbers Disappeared in an Array448,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 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]
// 因为Array中的元素是从1到n,所以减1将是0到n-1,它们是数组的索引。
//以输入[4.3.2.7.8.2.3.1]为例,通过减去1,它变为[3.2.1.6.7.1.2.0],这是一个索引数组。
//第一次迭代
//当i = 0时,它将nums [3]标记为负数,数组变为[4.3.2.-7.8.2.3.1]。
//当i = 1时,它将nums [2]标记为负数,数组变为[4.3.-2.-7.8.2.3.1]。
//当i = 2时,它将nums [1]标记为负数,数组变为[4.-3.-2.-7.8.2.3.1]。
//当i = 3时,它将nums [6]标记为负数,数组变为[4.-3.-2.-7.8.2.-3.1]。
//...
//...
//当i = 6时,它将nums [0]标记为负数,数组变为[-4.-3.-2.-7.8.2.-3.-1]。
//
//对于第二次迭代
//找到nums [4] = 8和nums [5] = 2> 0;
//这意味着4和5不在索引数组[3.2.1.6.7.1.2.0]中。
//通过添加1,5和6不在输入[4.3.2.7.8.2.3.1]
//返回[5.6]public static void main(String[] args) {int []nums= {4,3,2,7,8,2,3,1};List<Integer> findDisappearedNumbers = findDisappearedNumbers(nums);for(Integer num: findDisappearedNumbers) {System.out.print(num+" ");}}public  static List<Integer> findDisappearedNumbers(int[] nums) {List<Integer> list=new ArrayList<>();for(int i=0;i<nums.length;i++) {int index=Math.abs(nums[i])-1;if(nums[index]>0) {nums[index]=-nums[index];}  		}for(int i=0;i<nums.length;i++) {if(nums[i]>0) {list.add(i+1);}}return list;}

 

这篇关于数组中消失的数--------------------------Find All Numbers Disappeared in an Array448的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/491774

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

C语言:柔性数组

数组定义 柔性数组 err int arr[0] = {0}; // ERROR 柔性数组 // 常见struct Test{int len;char arr[1024];} // 柔性数组struct Test{int len;char arr[0];}struct Test *t;t = malloc(sizeof(Test) + 11);strcpy(t->arr,

C 语言基础之数组

文章目录 什么是数组数组变量的声明多维数组 什么是数组 数组,顾名思义,就是一组数。 假如班上有 30 个同学,让你编程统计每个人的分数,求最高分、最低分、平均分等。如果不知道数组,你只能这样写代码: int ZhangSan_score = 95;int LiSi_score = 90;......int LiuDong_score = 100;int Zhou

计算数组的斜率,偏移,R2

模拟Excel中的R2的计算。         public bool fnCheckRear_R2(List<double[]> lRear, int iMinRear, int iMaxRear, ref double dR2)         {             bool bResult = true;             int n = 0;             dou

C# double[] 和Matlab数组MWArray[]转换

C# double[] 转换成MWArray[], 直接赋值就行             MWNumericArray[] ma = new MWNumericArray[4];             double[] dT = new double[] { 0 };             double[] dT1 = new double[] { 0,2 };

PHP7扩展开发之数组处理

前言 这次,我们将演示如何在PHP扩展中如何对数组进行处理。要实现的PHP代码如下: <?phpfunction array_concat ($arr, $prefix) {foreach($arr as $key => $val) {if (isset($prefix[$key]) && is_string($val) && is_string($prefix[$key])) {$arr[

Go 数组赋值问题

package mainimport "fmt"type Student struct {Name stringAge int}func main() {data := make(map[string]*Student)list := []Student{{Name:"a",Age:1},{Name:"b",Age:2},{Name:"c",Age:3},}// 错误 都指向了最后一个v// a

码蹄集部分题目(2024OJ赛9.4-9.8;线段树+树状数组)

1🐋🐋配对最小值(王者;树状数组) 时间限制:1秒 占用内存:64M 🐟题目思路 MT3065 配对最小值_哔哩哔哩_bilibili 🐟代码 #include<bits/stdc++.h> using namespace std;const int N=1e5+7;int a[N],b[N],c[N],n,q;struct QUERY{int l,r,id;}que