leetcode_448. Find All Numbers Disappeared in an Array 找出不在数组中的数,正负号标记法
2016-12-20 22:24
204 查看
题目:
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:
题意:
给定一个整数数组a,其中数组元素满足1 ≤ a[i] ≤ n(n为数组的大小),a中有些元素出现多次,有些只出现一次。现在请写一个函数,找出数字[1,n]中没有在数组a中出现的
所有数字。
要求不能用多余的空间,另外要求在O(n)的时间复杂度内完成此题。
代码:
public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
// int res[];
// List<Integer> list=new ArrayList<Integer>(new Integer(13));
// List res = new List();
List<Integer> res= new ArrayList<Integer>(); #定义List类型的返回结果
for(int i=0; i<n; i++){
int index = Math.abs(nums[i])-1; //元素num[i]对应num中的下标,元素num[i]可能被更改过,故加上绝对值,确保下标是正数
nums[index] = -Math.abs(nums[index]); //将元素num[i]对应的下标的元素变成负数
}
for(int i=0; i<n; i++){ //统计num中对应的值没有变成负数的下标,这些下标即为没有在num中出现的数
if(nums[i]>0){
res.add(i+1);
}
}
return res;
}
}
笔记:
1、算法思想头一次接触,正负号标记法,参考了http://bookshadow.com/weblog/2016/11/01/leetcode-find-all-numbers-disappeared-in-an-array/
2、java的List集合类型,声明方法在网上找了大半天,参考了http://blog.csdn.net/friendan/article/details/17529683
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]
题意:
给定一个整数数组a,其中数组元素满足1 ≤ a[i] ≤ n(n为数组的大小),a中有些元素出现多次,有些只出现一次。现在请写一个函数,找出数字[1,n]中没有在数组a中出现的
所有数字。
要求不能用多余的空间,另外要求在O(n)的时间复杂度内完成此题。
代码:
public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
// int res[];
// List<Integer> list=new ArrayList<Integer>(new Integer(13));
// List res = new List();
List<Integer> res= new ArrayList<Integer>(); #定义List类型的返回结果
for(int i=0; i<n; i++){
int index = Math.abs(nums[i])-1; //元素num[i]对应num中的下标,元素num[i]可能被更改过,故加上绝对值,确保下标是正数
nums[index] = -Math.abs(nums[index]); //将元素num[i]对应的下标的元素变成负数
}
for(int i=0; i<n; i++){ //统计num中对应的值没有变成负数的下标,这些下标即为没有在num中出现的数
if(nums[i]>0){
res.add(i+1);
}
}
return res;
}
}
笔记:
1、算法思想头一次接触,正负号标记法,参考了http://bookshadow.com/weblog/2016/11/01/leetcode-find-all-numbers-disappeared-in-an-array/
2、java的List集合类型,声明方法在网上找了大半天,参考了http://blog.csdn.net/friendan/article/details/17529683
相关文章推荐
- 给定一个数组,找出不在数组中的最小的那个数字
- 【leetcode】数组中找出只出现一次的数字(Single Number)
- leetcode之数组中找出三个数之和为规定值
- leetcode_442. Find All Duplicates in an Array 找数组中出现两次的数,正负号标记法
- [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字
- 给定一个数组,找出不在数组中的最小的那个数字
- 找出不在预定数组中的自然数
- [LeetCode] Kth Largest Element in an Array (找出数组的第k大的元素)
- ※ Leetcode - Array - 169. Majority Element(快速找出数组中出现过一半以上次数的数,3种写法)
- LeetCode Median of Two Sorted Arrays 在两个已排列的数组中找出中位数。时间复杂度为O(log(min(N,M))
- 【LeetCode】- Single Number(从数组中找出只出现一次的数字)
- LeetCode-Find Minimum in Rotated Sorted Array(找出反转数组中的最小值)
- LeetCode——Single Number II(找出数组中只出现一次的数2)
- leetcode_260. Single Number III 数组中找出两个只出现一次的元素
- leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数
- LeetCode-Median of Two Sorted Arrays,找出两个排好序的数组的中位数
- LeetCode——Single Number(找出数组中只出现一次的数)
- [leetcode] Median of Two Sorted Arrays 两个排序数组找出第k个数,或者中位数
- [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项
- 找出不在预定数组中的自然数