算法分析与设计课程作业第一周#1

算法分析与设计课程作业第一周#1

开始这次作业之前,已经很久没做过这样的代码题了,自觉技巧生疏了不少,以至于第一道easy题就错漏百出。

先上题目:

Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

第一次错误

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for(int i = 0; i < nums.size() - 1; i++){
            for(int j = i + 1; j < nums.size(); j++){
                if(nums[i] + nums[j] == target)
                    return vector<int>(i, j);
            }
        }
    }
};

错误原因:vector这样用是指放进i个值为j的数,写这样的代码时应该是脑子进水了。

第二,三次错误

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for(int i = 0; i < nums.size() - 1; i++){
            for(int j = i + 1; j < nums.size(); j++){
                if(nums[i] + nums[j] == target)
                    return vector<int>.push_back(i).push_back(j);
            }
        }
    }
};
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> result;
        for(int i = 0; i < nums.size() - 1; i++){
            for(int j = i + 1; j < nums.size(); j++){
                if(nums[i] + nums[j] == target){
                    return result.push_back(i).push_back(j);
                }
            }
        }
    }
};

错误原因:vector的push_back返回的是空值,这个我并没有注意到。

自己写的正确答案

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> result;
        for(int i = 0; i < nums.size() - 1; i++){
            for(int j = i + 1; j < nums.size(); j++){
                if(nums[i] + nums[j] == target){
                    result.push_back(i);
                    result.push_back(j);                 
                    return result;
                }
            }
        }
    }
};

心得:一开始写成那个样子其实是因为我隐约记得返回局部变量会有某些问题出现,但其实只要不是返回局部的指针就可以了(如果想返回指针就用new),而vector作为函数返回值也是可以的。

网站提供的某方面更好的答案(似乎是java)

public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        map.put(nums[i], i);
    }
    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (map.containsKey(complement) && map.get(complement) != i) {
            return new int[] { i, map.get(complement) };
        }
    }
    throw new IllegalArgumentException("No two sum solution");
}

这个时间复杂度为O(n),用了哈希表,而哈希表查找效率为O(1),一个循环下来时间复杂度就为O(n),而额外用多了个哈希表,所以空间复杂度为O(n)。对比我原来做出来的答案,我的答案时间复杂度为O(n^2),空间复杂度为O(1)。
上面的标答再改进下就只需一个循环(原来要两个,但不是嵌套循环,所以时间复杂度都是O(n)):

public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (map.containsKey(complement)) {
            return new int[] { map.get(complement), i };
        }
        map.put(nums[i], i);
    }
    throw new IllegalArgumentException("No two sum solution");
}

这个的思路就是将nums的数一个个检查,检查能不能与原有的加起来符合要求,不能就放进哈希表继续过程。顺带一提检查与放进去顺序不能互换,不然就不符合题目”you may not use the same element twice“的要求了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值