题目:

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 = [, , , ], target = ,

Because nums[] + nums[] =  +  = ,
return [, ].

题解:

  首先想到暴力解,TLE(Time Limit Exceeded)问题先不考虑。从数组头开始,依次与其他元素比较,然后向后移动开始位置,需要注意的是返回的位置是从0开始的

Solution 1 (TLE)

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

  那怎样才能降低时间复杂度呢?这里有个经验准则,数组及字符串问题往往与(unordered)map、(unordered)set有关。这里使用unordered_map,用于查找;首先想到的思路是先遍历一遍数组,建立map映射表(元素值和位置的映射),然后再遍历一遍查找与当前元素之和为目标值的元素。建立m(数组元素)与m的位置的映射。

Solution 2

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
int n = nums.size();
vector<int> result;
for (int i = ; i < n; ++i) {
m[nums[i]] = i;
}
for (int i = ; i < n; ++i) {
int t = target - nums[i];
//m.find(t) != m.end() 可以用 m.count(t)代替
       //you may not use the same element twice.
if (m.find(t) != m.end() && m[t] != i) {
result.push_back(i);
result.push_back(m[t]);
break;
}
}
return result;
}
};

  有一种优化方法,思路是从数组头开始,依次将元素对应的加数加入map中,在建立映射的过程中,若发现map中已经存在了一个元素,由于这个元素的存在是之前的操作才加入到map中,及此元素对应的加数在之前已经存在,故直接返回即可。题设 each input would have exactly one solution。建立target-m与m的位置的映射。

Solution 3

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

【LeetCode】001. TwoSum的更多相关文章

  1. 【LeetCode】001. Two Sum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. 更改计算机名称,影响TFS之前映射的工作区 使用。

    今天把自己电脑的计算机名称改了,打开vs2012的时候,就提示以下的错误: ---------------------------Microsoft Visual Studio------------ ...

  2. Linux Shell基础 环境变量

    环境变量 环境变量和用户自定义变量最主要的区别在于,环境变量是全局变量,而用户自定义变量是局部变量.用户自定义变量只在当前的 Shell 中生效,而环境变量会在当前 Shell 和这个 Shell 的 ...

  3. 主攻ASP.NET.4.5.1 MVC5.0之重生:空地搭建一个包含 Ninject框架 项目

    1.创建一个空白解决方案 2.添加一个类库 名称为XXX.Domain 3.添加一个ASP.MVC 名称为XXX.WebUI 4.选着空模版,勾选MVC核心引用 5.添加单元测试项目XXX.UntiT ...

  4. vue引入bootstrap.min.css报错:Cannot find module "./assets/css/bootstrap.min.css"

    问题如下图: 明明文件就在那里,就是报错说找不到模板,然后我就用了网上的方法,重新建立了一个项目,请参考如下: http://blog.csdn.net/ansu2009/article/detail ...

  5. python的计算保留小数

    1.要使得算术运算的结果有小数,则运算的对象至少有一个是float型的. 2.控制小数的位数:字符串格式化 格式:需要进行格式化的字符串%插入对象 需要进行格式化的字符串中带有一个或多个嵌入的转换目标 ...

  6. css transform常用变化解析

    本文旨在对常用变化做最直观的简析 translate 移动 translateX() X轴正方向移动(单位可为px,也可为%,为%时以自身为参照物) translateY() Y轴反方向移动 tran ...

  7. 《Inode与Block重要知识总结核心讲解》【转】

    本文转载自:https://blog.csdn.net/BlackEnn/article/details/50787092 1.查看/dev/sda1下磁盘分区的block大小: 2.查看单个inod ...

  8. freemarker模板解析过程

    例如:一个freemarker表达式<body> ${hello} </body>,会被解析成三个部分,分别是<body>${hello}</body> ...

  9. ssm搭建相关的问题

    在搭建ssm框架时候踩得坑:1.对于拦截器url-parttern的设置:第一次设置的是/** 本以为这个是表示拦截所有,没想到这是错误的写法,正确的写法是/    启动项目不会报错,但是会出现404 ...

  10. Sqoop将MySQL表结构同步到hive(text、orc)

    Sqoop将MySQL表结构同步到hive sqoop create-hive-table --connect jdbc:mysql://localhost:3306/sqooptest --user ...