日前,使用暴力法破解此题,认为这是很简单的算法,但是所有人都能想出来的算法,凭什么优秀?所以在看到了大神“Grandyang”的博客上精妙的解法,实在是认为自己需要修炼,在此写在这里是为了做笔记,加深理解附上网址:http://www.cnblogs.com/grandyang/p/4130379.html

题目描述

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
//以下为大神理解

一般来说,我们为了提高时间的复杂度,需要用空间来换,这算是一个trade off吧,我们只想用线性的时间复杂度来解决问题,那么就是说只能遍历一个数字,那么另一个数字呢,我们可以事先将其存储起来,使用一个HashMap,来建立数字和其坐标位置之间的映射,我们都知道HashMap是常数级的查找效率,这样,我们在遍历数组的时候,用target减去遍历到的数字,就是另一个需要的数字了,直接在HashMap中查找其是否存在即可,注意要判断查找到的数字不是第一个数字,比如target是4,遍历到了一个2,那么另外一个2不能是之前那个2,整个实现步骤为:先遍历一遍数组,建立HashMap映射,然后再遍历一遍,开始查找,找到则记录index。

其参考答案:

参考答案一:

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
    //哈希定义方法现在是这样
hash_map<int, int> m;
vector<int> res;
for (int i = ; i < nums.size(); ++i) {
m[nums[i]] = i; //先遍历一遍数组,建立HashMap映射
}
//然后再遍历一遍,开始查找,找到则记录index
for (int i = ; i < nums.size(); ++i) {
//总值-元素 = 目标值
int t = target - nums[i];
//查找目标值是否存在,在哈希中查找 t 与 i 存在关系,如果m[t]=i 说明是当前数字自己加了两次等于目标值
//hash查找的是key值,也就是nums
if (m.count(t) && m[t] != i) {//if里面的条件用于判断查找到的数字不是第一个数字
res.push_back(i);
res.push_back(m[t]);
break;
}
}
return res;
}
};

参考答案二:

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

解法一样,第二种较为简洁。

letCode-1的更多相关文章

  1. letcode刷题之两数相加

    letcode里面刷题,坑还是链表不熟,(1)头结点还是有必要设置,否则返回的时候找不到位置:(2)先设置next到新节点再next到下一个节点:都是基础知识 /* * * You are given ...

  2. [py]letcode第一题求和

    letcode第一题, tm的不好弄. 想了很久想到了一个粗蠢的解决办法. Given an array of integers, return indices of the two numbers ...

  3. (letcode)String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  4. [Letcode] 1. Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  5. 迭代法与开根号求值(letcode 69)

    p { margin-bottom: 0.25cm; line-height: 120% } 一.理论证明 p { margin-bottom: 0.25cm; line-height: 120% } ...

  6. 【letcode】5-LongestPalindromicSubstring

    回文串 回文串(palindromic string)是指这个字符串无论从左读还是从右读,所读的顺序是一样的:简而言之,回文串是左右对称的.一般求解一个字符串的最长回文子串问题. problem:Lo ...

  7. letcode code]Maximum Subarray

    1 题目: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  8. letCode(771 Jewels and Stones )

    问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...

  9. letcode刷题记录-day03-罗马转整数

    题目 罗马转整数 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 ...

  10. letcode刷题记录-day02-回文数

    回文数 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答 ...

随机推荐

  1. 数据结构|-用C#实现一个简单的链表

    我们知道C#中是没有链表的,我们可以自己实现一个 整个单链表能实现的功能有: 功能 方法 返回值 备注 获取链表长度 GetLength() int 返回值是链表长度 清空链表 Clear() voi ...

  2. centos用YUM装mysql笔记

    安装的方法,参考:https://blog.csdn.net/jeffleo/article/details/53559712 注意事项: 1.上面教程中,关于设置密码的地方,SQL语句有误,单引号要 ...

  3. zabbix报错gd、freetype、png、jpeg

    安装包位置:http://www.p-pp.cn/app/zabbix/ 1.安装freetype [root@localhost softs]# tar xf freetype-2.5.0.tar. ...

  4. 可编程并行接口8255A详解

  5. jquery 点击显示更多

    点击显示更多 html <div class="servicepicture banxin"> <div class="imgcontent" ...

  6. ashx导出dataTable为Excel

    一,datatable导出Excel,用户可以选择路径,方法如下: /// <summary> /// DataTable导出到Excel /// </summary> /// ...

  7. nameode启动过程

    namenode在内存和磁盘中都保存了fsimage和edits文件 内存中保证hdfs文件系统的访问效率,磁盘中保证hdfs文件系统的安全性 namenode的文件组成: fsimage文件:保存文 ...

  8. sqlserver isnull函数

    isnull(参数1,参数2),判断参数1是否为NULL,如果是,返回参数2,否则返回参数1. select ISNULL(null,'helloword') 返回helloword字符串select ...

  9. git pull更新错误解决办法

    Your local changes to the following files would be overwritten by mergeerror: Your local changes to ...

  10. js的一些常用方法

    1.判断是否为一个空对象 let a={}; console.log(Object.keys(arr).length==0);//true 2.从数组中取出重复的数据 var arr = [" ...