leetcode442. Find All Duplicates in an Array
题目描述如下:Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear ?
once.Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
题目要求找出一个数组中出现过两次的数字,不能使用额外的空间并且时间复杂度为O(n),这道题的难点其实就在于不能使用额外的空间,如果可以使用的话其实只要进行一次类似于桶排序的
方式,统计每个元素的个数就可以了,这样一次需要扫描数组本身,一次需要对用来计数的数组进行扫描,复杂度为O(n),这种方法的代码如下:
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> count(nums.size(),0);
vector<int> ans;
for(int i = 0; i < nums.size(); i++){
count[nums[i] - 1]++;
}
for(int i = 0; i < nums.size(); i++){
if(count[i] == 2){
ans.push_back(i + 1);
}
}
return ans;
}
};
要对其进行优化其基本思路肯定是一样的,但是问题在于如何节省空间,不能使用额外的空间就必须要充分利用数组本身,而注意到一个数只能出现一次或者两次,所以一种比较好的办法为利用正负号对一个数是否出现过进行标记,而因为所给的数都为正数,就算改变了符号也不会影响其内容,具体代码如下:
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> ans;
for(int i = 0; i < nums.size(); i++){
if(nums[abs(nums[i]) - 1] < 0)
ans.push_back(abs(nums[i]));
nums[abs(nums[i]) - 1] = -nums[abs(nums[i]) - 1];
}
return ans;
}
};
leetcode238. Product of Array Except Self
题目描述如下:Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:Input: [1,2,3,4]
Output: [24,12,8,6]
Note: Please solve it without division and in O(n).Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
首先这道题的难点在于不允许用除法,如果可以用除法的话基本没什么难度,只要算出所有元素的积,然后对每个元素再除一下就好了,首先我们先不考虑最后一个对空间复杂度的要求,那么对于 a1a2...ak−1ak+1...an a 1 a 2 . . . a k − 1 a k + 1 . . . a n ,我们可以拆成 a1a2...ak−1和ak+1...an a 1 a 2 . . . a k − 1 和 a k + 1 . . . a n 两个部分,所以我们只需要用两个数组来记录每个k对应的左右两部分的值就好了,代码如下:
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> mularr1,mularr2;
int mul1 = 1, mul2 = 1;
for(int i = 0; i < nums.size(); i++){
mul1 *= nums[i];
mul2 *= nums[nums.size() -1 - i];
mularr1.push_back(mul1);
mularr2.push_back(mul2);
}
vector<int> ans;
for(int i = 0; i < nums.size(); i++){
int right = i == nums.size() -1 ? 1 : mularr2[nums.size() - 2 - i];
int left = i == 0 ? 1 : mularr1[i - 1];
ans.push_back(right*left);
}
return ans;
}
};
现在我们再来考虑第二个要求,即要空间复杂度为O(1),其实思路和上面那题是比较类似的,都是要利用好原数组,而最终结果是不算复杂度的,所以其实可以把上面解法的mul1先存到ans当中去,然后再从后往前处理ans数组,再次乘上右边的部分,而右边的部分其实只用一个变量就可以存储了,代码如下:
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> ans;
int left = 1;
for(int i = 0; i < nums.size(); i++){
ans.push_back(left);
left *= nums[i];
}
int right = 1;
for(int i = nums.size() - 1; i >= 0; i--){
ans[i] *= right;
right *= nums[i];
}
return ans;
}
};
leetcode41. First Missing Positive
问题描述:Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:Input: [3,4,-1,1]
Output: 2
Example 3:Input: [7,8,9,11,12]
Output: 1
Note:Your algorithm should run in O(n) time and uses constant extra space.’
这题就是找一个数组中不存在的最小正整数,同样难点就在于对空间复杂度的要求,如果不考虑空间复杂度的话,可以用一个长度为n的数组来记录是否出现了1~n的数字,因为待处理数组的长度就为n,所以不可能出现1~n都在数组中但是还有其他元素的情况,但是这里只允许O(1)的空间复杂度,所以还是要利用待处理数组本身的空间,比较容易想到的方法就是交换,把我们关心的元素摆在它应该在的位置(注意这并不是排序,非正数和超过n的数都被忽略了,所以复杂度仅为O(n)而不是O(nlogn)),使一个正数a所在的位置为nums[a-1],然后检查每个位置,如果这个位置的值并不是预想的数,就说明这个数根本没有在原数组里面,然后我们从左到右扫描发现的第一个满足条件的位置本应对应的数就是我们要找的最小正数,代码如下:
class Solution {
public:
void swap(int &a,int &b){
int tmp = a;
a = b;
b = tmp;
}
int firstMissingPositive(vector<int>& nums) {
for(int i = 0; i < nums.size(); i++){
if(nums[i]!=i+1 && nums[i]>=1 && nums[i] <= nums.size() && nums[i] != nums[nums[i] - 1]){
swap(nums[i],nums[nums[i] - 1]);
i--;
}
}
for(int i = 0; i < nums.size(); i++){
if(nums[i] != i+1)
return i+1;
}
return nums.size() + 1;
}
};
其中要注意如果发生交换,就要进行一次i–,否则那个被换过来的数就会被跳掉