国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[LeetCode] Find All Numbers Disappeared in an Array

2019-11-08 18:27:02
字體:
來源:轉載
供稿:網友

題目鏈接在此

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]

要求不用額外的空間,以及O(n)的時間。

額,這要求有點高啊。

不過有一個很重要的限制條件:1 ≤ a[i] ≤ n

于是大體思想就是:把數組中的每一個數(設為x),當做數組下標,把下標x處的數a[x]用其相反數-a[x]來代替。(當然,x要做下-1的處理,不然數組會越界)。

class Solution {public:	vector<int> findDisappearedNumbers(vector<int>& nums) {		vector<int> ans;		for (int n : nums) {			int val = abs(n) - 1;			if(nums[val] > 0)				nums[val] = -nums[val];		}		for (int i = 0; i < nums.size(); i++)			if (nums[i] > 0)				ans.push_back(i + 1);		return ans;	}};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长垣县| 招远市| 红原县| 临朐县| 贵德县| 乐业县| 休宁县| 巧家县| 阜新市| 衡南县| 巴里| 永春县| 贡觉县| 蒲城县| 江永县| 上栗县| 苍南县| 江都市| 前郭尔| 垦利县| 泸水县| 富源县| 广宁县| 诏安县| 上林县| 湟源县| 长宁区| 开阳县| 大名县| 山东| 浑源县| 正定县| 吉木乃县| 色达县| 许昌县| 锡林郭勒盟| 涿州市| 永城市| 丁青县| 亚东县| 红安县|