日前,使用暴力法破解此题,认为这是很简单的算法,但是所有人都能想出来的算法,凭什么优秀?所以在看到了大神“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. selenium-xpath练习

  2. R语言查看栅格值

    有这么一个需求,知道栅格上的坐标,想看看这个坐标上的栅格值是多少.坐标长这个样子 那么这样的坐标下的栅格值该怎么看 cellFromXY(the.stack$t1,c( -1505000,683500 ...

  3. Docker镜像加速器配置

    一.为什么要配置Docker镜像加速器 因为我们默认pull的docker镜像是从Docker Hub来下载,由于其服务器在国外,速度会比较慢.因此我们可以配置成国内的镜像仓库,这样可以加速镜像的上传 ...

  4. git pull和本地文件冲突

    在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息: error: Your local changes to 'c/environ.c' would be overwritten b ...

  5. kindle 安卓 app下载的电子书放在什么文件夹?

    They are buried in: /sdcard/Android/data/com.amazon.kindle/files/

  6. 雷林鹏分享:jQuery EasyUI 数据网格 - 添加复选框

    jQuery EasyUI 数据网格 - 添加复选框 本实例演示如何放置一个复选框列到数据网格(DataGrid).通过复选框,用户将可以选择 选中/取消选中 网格行数据. 为了添加一个复选框列,我们 ...

  7. RecyclerView联动滑动失败

    RecyclerView联动滑动失败 我们在做Recyclerview联动滑动的时候,就是左边一个RecyclerView右边一个RecyclerView 我们希望左边的RecyclerView可以和 ...

  8. liunx-centos的安装学习篇

    1.centos下载地址:网易镜像:http://mirrors.163.com/centos/6/isos/搜狐镜像:http://mirrors.sohu.com/centos/6/isos/ 2 ...

  9. Jieba库使用和好玩的词云

    jieba库的使用: (1)  jieba库是一款优秀的 Python 第三方中文分词库,jieba 支持三种分词模式:精确模式.全模式和搜索引擎模式,下面是三种模式的特点. 精确模式:试图将语句最精 ...

  10. Vue非父子级通信

    <div id="app"> <xz-todo></xz-todo> </div> <!--1. 为每个组件定义HTML 模板 ...