LeetCode[1]

第一次写博客,记录自己水水的过程。
最近开始准备刷leetCode,从C++入手,然后意识到自己到底有多菜。
废话不多说,开始吧。努力的和这个洪荒巨兽战斗,看看是我把它按在地上蹂躏,还是它把我扔在地上摩擦。
(1)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].

代码(1):

#include<iostream>
#include<vector>
using namespace std;

/**********************************************************
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) {
        vector<int> indices;
        int nums_length = nums.size();
        for (int i = 0; i < nums_length; i++)
            for (int j = i + 1; j < nums_length; j++) {
                if (nums[i] + nums[j] == target) {
                    indices.push_back(i);
                    indices.push_back(j);
                    return indices;
                }
            }
        return indices;
    }
};

vector<int> get_input(vector<int>& nums, int target) {
    Solution s;
    return s.twoSum(nums, target);
}

leetCode会自动给函数提供输入输出,所以我们不需要自己去提供。我说的话很不严谨,请读者思考着浏览。我只写了一种最简单的方法,就是遍历数组中的所有的数,这种方法的运行速度会较慢,所以我想找出一种更加快速的方法,后来,我发现很多代码大牛已经提供了一些放在并分享了出来。
大牛原话:“一般来说,我们为了提高时间的复杂度,需要用空间来换,这算是一个trade off吧,我们只想用线性的时间复杂度来解决问题,那么就是说只能遍历一个数字,那么另一个数字呢,我们可以事先将其存储起来,使用一个HashMap,来建立数字和其坐标位置之间的映射,我们都知道HashMap是常数级的查找效率,这样,我们在遍历数组的时候,用target减去遍历到的数字,就是另一个需要的数字了,直接在HashMap中查找其是否存在即可,注意要判断查找到的数字不是第一个数字,比如target是4,遍历到了一个2,那么另外一个2不能是之前那个2,整个实现步骤为:先遍历一遍数组,建立HashMap映射,然后再遍历一遍,开始查找,找到则记录index。”
下面给出相关代码:

代码(2):

    vector<int> HashMap(vector<int>& nums, int target) {
        unordered_map<int, int> m;
        vector<int> res;
        for (int i = 0; i < nums.size(); i++) {
            m[nums[i]] = i;
        }
        for (int i = 0; i < nums.size(); i++) {
            int r = target - nums[i];
            if (m.count(r) && m[r] != i){
                res.push_back(i);
                res.push_back(m[r]);
                break;
            }
        }
        return res;     
    }

或者我们可以再写的简单一些,把两个for循环合并。

代码(3):

vector<int> HashMap_2(vector<int>& nums, int target) {
        unordered_map<int, int> m;
        for (int i = 0; i < nums.size(); ++i) {
            if (m.count(target - nums[i])) {
                return {i, m[target - nums[i]]};
            }
            m[nums[i]] = i;
        }
        return {};
    }

这里再提供一个方法,也是利用哈希表:

代码(4):

vector<int> HashMap_3(vector<int>& nums, int target) {
        vector<int> v(2, 0);
		unordered_map<int, int> hash;
		for (int i = nums.size(); i--; hash[nums[i]] = i) {
			if (hash.find(target - nums[i]) == hash.end())
				continue;
			v[0] = i;           
			v[1] = hash[target - nums[i]];
			return v;
		}
		return v;                  
    }

经过检测:
在这里插入图片描述
按照由下网上的顺序分别为:代码1到代码4。
可以看出,代码4的效果最好。
最后贴出整体的代码:

#include<iostream>
#include<vector>
using namespace std;

/**********************************************************
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:
    /****************164ms, 9.4MB*******************/ 
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> indices;
        int nums_length = nums.size();
        for (int i = 0; i < nums_length; i++)
            for (int j = i + 1; j < nums_length; j++) {
                if (nums[i] + nums[j] == target) {
                    indices.push_back(i);
                    indices.push_back(j);
                    return indices;
                }
            }
        return indices;                 
    }
};

    /****************12ms, 10.5MB*******************/    
    vector<int> HashMap_1(vector<int>& nums, int target) {
        unordered_map<int, int> m;
        vector<int> res;
        for (int i = 0; i < nums.size(); i++) {
            m[nums[i]] = i;
        }
        for (int i = 0; i < nums.size(); i++) {
            int r = target - nums[i];
            if (m.count(r) && m[r] != i){
                res.push_back(i);
                res.push_back(m[r]);
                break;
            }
        }
        return res;     
    }

    /****************8ms, 10.3MB*******************/    
    vector<int> HashMap_2(vector<int>& nums, int target) {
        unordered_map<int, int> m;
        for (int i = 0; i < nums.size(); ++i) {
            if (m.count(target - nums[i])) {
                return {i, m[target - nums[i]]};
            }
            m[nums[i]] = i;
        }
        return {};
    }

    /****************8ms, 10MB*********************/  
    vector<int> HashMap_3(vector<int>& nums, int target) {
        vector<int> v(2, 0);
		unordered_map<int, int> hash;
		for (int i = nums.size(); i--; hash[nums[i]] = i) {
			if (hash.find(target - nums[i]) == hash.end())
				continue;
			v[0] = i;           
			v[1] = hash[target - nums[i]];
			return v;
		}
		return v;                  
    }

vector<int> get_input(vector<int>& nums, int target) {
    Solution s;
    return s.twoSum(nums, target);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值