LeetCode题解之Find All Numbers Disappeared in an Array

Posted wangxiaoyong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode题解之Find All Numbers Disappeared in an Array相关的知识,希望对你有一定的参考价值。

1、题目描述

技术分享图片

2.问题分析

使的 A[i] = i+1 ,最后检查不满足这个条件的i+1 .即为缺失的值。

3.代码

 1 vector<int> findDisappearedNumbers(vector<int>& nums) {
 2         for( int i = 0; i < nums.size() ; ++i ){
 3             if( nums[i]  != nums[ nums[i] - 1 ] ){
 4                 swap( nums[i] , nums[nums[i] - 1] );
 5                 i--;
 6             } 
 7         }
 8         vector<int> result ;
 9         for( int i = 0; i < nums.size() ; i++){
10             if( nums[i] != i+1 )
11                 result.push_back( i+1 );
12         }
13         return result ;
14     }

 

以上是关于LeetCode题解之Find All Numbers Disappeared in an Array的主要内容,如果未能解决你的问题,请参考以下文章

leetcode题解之Find the Duplicate Number

leetcode之Find All Numbers Disappeared in an Array

leetcode之Find All Numbers Disappeared in an Array

#Leetcode# 448. Find All Numbers Disappeared in an Array

leetcode No448. Find All Numbers Disappeared in an Array

LeetCode - Find All Anagrams in a String