[置顶] 发现一个数组中重复的数字,448和287的总结 ---重要
2017-02-17 13:39
274 查看
区别一、448题目没有限制不能改变原来的数组,所以采用的是交换机制,不断的将错误位置上的数据不断的交换移动到本该属于其的位置上去,则重新遍历时,nums[i]!=i+1的数字即没有出现的数字,但287题目则限制了不能改变原来的数组,所以不能采用交换机制的方法,方法类似采用查找循环链表的环入口的方法,或者可以采用利用二分查找的思想;
区别二、448题目中1-n之间重复的数字只出现了两次,但是在287中则至少出现两次
一、448题目
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]
采用的方法是:不断的将错误位置上的数据不断的交换移动到本该属于其的位置上去,则重新遍历时,nums[i]!=i+1的数字即没有出现的数字
二、287题目: Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.
二分查找:
寻找链表环入口的方法:
区别二、448题目中1-n之间重复的数字只出现了两次,但是在287中则至少出现两次
一、448题目
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]
采用的方法是:不断的将错误位置上的数据不断的交换移动到本该属于其的位置上去,则重新遍历时,nums[i]!=i+1的数字即没有出现的数字
//注意不断的将错误位置上的数据不断的交换移动到本该属于其的位置上去,则重新遍历时,nums[i]!=i+1的数字即没有出现的数字 vector<int> findDisappearedNumbers(vector<int>& nums) { int len = nums.size(); vector<int> result; for(int i = 0; i < len; i++) { if(nums[i] == i+1) { continue; } else { while(nums[nums[i] - 1] != nums[i] ) { //int idx = nums[i] - 1; int temp = nums[nums[i] - 1]; nums[nums[i] - 1] = nums[i]; nums[i] = temp; } } } for(int i = 0; i < len; i++) { if(nums[i] != i+1) { result.push_back(i+1); } } return result; }
二、287题目: Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.
二分查找:
int findDuplicate(vector<int>& nums) { //二分查找,思想利用下标来实现,诸如:1...10的数中,如果小于5的数目大于5,那么表明重复的数字一定在小于5的数目中,妙哉 int len = nums.size(); if(len == 0 || len == 1) return -1; int low = 0; int high = len - 1; while(low <= high) { int mid = low + (high - low) / 2; int count = 0; for(int i = 0; i < len; i++) if(nums[i] <= mid) count++; if(count > mid) high = mid - 1; else low = mid + 1; } return low; }
寻找链表环入口的方法:
int findDuplicate(vector<int>& nums) { //像寻找单链表的环入口点的思路一样,设置一块一慢的指针,由于数组中存在重复节点,则可通过下标来联想成链表 int len = nums.size(); if(len == 1 || len == 0) return -1; int slow = nums[0]; int fast = nums[slow]; //以下为求两个指针第一次相遇的点 while(slow != fast) { slow = nums[slow]; fast = nums[nums[fast]]; } //以下求重复元素的节点,即重复元素的入口节点 fast = 0; while(fast != slow) { fast = nums[fast]; slow = nums[slow]; } return slow; }
相关文章推荐
- 软件开发者面试百问-----有一个数组,里面是从1到1,000,000的整数,其中有一个数字出现了两次,你怎么找出那个重复的数字?
- 判断一个整数数组中是否有重复数字出现的O(n)时间复杂度算法
- 如何把一个整型数组中重复的数字去掉
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】
- 计数排序——有一个数组,里面是从1到1,000,000的整数,其中有一个数字出现了两次,你怎么找出那个重复的数字?
- Java中从[1,36]随机生成7个不重复的数字,放入一个数组中
- 一个数组arr,长度为N,N小于10^8,里面数字的范围是1~N,是否有重复,并统计出来
- Java 判断数组中唯一一个不重复的数字,其它数字重复两次
- 把一个整形数组中重复的数字去掉 - 微软面试题
- 有个数组中有100w个数,其中有一个数重复了50w次,要求找出这个数字
- 一个长度为N的整形数组,数组中每个元素的取值范围是[0,N-1],写一个算法判断数组中是否存在重复的数字
- java 一个int数组 长度为100 随机生成100个数 即1-100 将其插入进数组 插入的数字不能重复
- 在一个整形数组中去掉重复的数字
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)
- 读入n个数,并把他存入一个数组,并且数字不能重复
- randnumber_mn.c 生成一个数组,包括k个不重复的整数,并且要求这些整数范围为[m,n),生成的结果中不能包含inum中的数字,size表示inum的长度
- 列出一个数组里面的重复数字
- 程序员面试题目总结--数组(三)【旋转数组的最小数字、旋转数组中查找指定数、两个排序数组所有元素中间值、数组中重复次数最多的数、数组中出现次数超过一半的数】
- 程序员面试题目总结--数组(一)【递归求数组所有元素和、用一个for循环打印出一个二维数组、用递归判断数组是否是递增、有序数组中删除重复元素】