算法设计与分析第一次作业

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...ak1ak+1...an a 1 a 2 . . . a k − 1 a k + 1 . . . a n ,我们可以拆成 a1a2...ak1ak+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–,否则那个被换过来的数就会被跳掉

国科大算法设计与分析相关1-5章复习题 第一章样例: 1.讲义习题一: 第1(执行步改为关键操作数)、第2、3、6、7题 习题一 1答:执行步4pmn+3pm+2m+1;关键操作2n*m*p 2方法一答:2n-2次 方法二答:2n-2次 3 1)证明:任给c,n>c,则10n2>cn 。不存在c使10n22c时,logn>c,从而n2logn>=cn2,同上。 6 答:logn,n2/3,20n,4n2,3n,n! 7 答:1)6+n 2) 3)任意n 2.讲义习题二:第5题。 答:c、e是割点。每点的DFN、L值:A1,1、B2,1、C3,1、D4,4、E5,1、F6,5、G7,5。最大连通分支CD、EFG、ABCE。 3.考虑下述选择排序算法: 输入:n个不等的整数的数组A[1..n] 输出:按递增次序排序的A For i:=1 to n-1 For j:=i+1 to n If A[j]<A[i] then A[i] A[j] 问:(1)最坏情况下做多少次比较运算?答1+2+..+n-1=n(n-1)/2 (2)最坏情况下做多少次交换运算?在什么输入时发生? n(n-1)/2,每次比较都交换,交换次数n(n-1)/2。 4.考虑下面的每对函数f(n)和g(n) ,比较他们的阶。 (1) f(n)=(n2-n)/2, g(n)=6n (2)f(n)=n+2 , g(n)=n2 (3)f(n)=n+nlogn, g(n)=n (4)f(n)=log(n!), g(n)= 答:(1)g(n)=O(f(n)) (2)f(n)=O(g(n) (3)f(n)=O(g(n) (4)f(n)=O(g(n) 5.在表中填入true或false . 答案: f(n) g(n) f(n)=O(g(n) f(n)=(g(n)) f(n)=(g(n)) 1 2n3+3n 100n2+2n+100 F T F 2 50n+logn 10n+loglogn T T T 3 50nlogn 10nloglogn F T F 4 logn Log2n T F F 5 n! 5n F T F 6.用迭代法求解下列递推方程: (1) (2) ,n=2k 答:(1)T(n)=T(n-1)+n-1=T(n-2)+n-2+n-1 =…=T(1)+1+2+…+n-1=n(n-1)/2=O(n2) (2)T(n)=2T(n/2)+n-1=2(2T(n/4)+n/2-1)+n-1 =4T(n/4)+n-2+n-1=4(2T(n/23)+n/4-1)+n-2+n-1 =23T(n/23)+n-4+n-2+n-1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值