描述

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.

示例

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

算法分析

难度:低

分析:要求给定的数组,查找其中2个元素,满足这2个元素的相加等于给定目标target的值。

思路:一般的思路,我们遍历数组元素,假设当前遍历的数组元素x,再次遍历x之后的数组元素,假设当前再次遍历的数组元素y,判断x+y是否满足target,如果满足,则返回x,y下标,否则继续遍历,直至循环结束。考虑这种算法的时间复杂度是O (n²),不是最优的解法。

跟前面几章类似,我们可以考虑用哈希表来存储数据,这里用C#提供的Hashtable来存储下标-对应值(key-value)键值对;

接着遍历数组元素,如果目标值-当前元素值存在当前的Hashtable中,则表明找到了满足条件的2个元素,返回对应的下标;

如果Hashtable没有满足的目标值-当前元素值的元素,将当前元素添加到Hashtable,进入下一轮遍历,直到满足上一条的条件。

代码示例(C#)

public int[] TwoSum(int[] nums, int target)
{
var map = new Hashtable(); ;
for (int i = 0; i < nums.Length; i++)
{
int complement = target - nums[i];
//匹配成功,返回结果
if (map.ContainsKey(complement))
{
return new int[] { (int)map[complement], i };
}
map.Add(nums[i], i);
}
return null;
}

复杂度

  • 时间复杂度O (n).
  • 空间复杂度O (1).

附录

算法题丨Two Sum的更多相关文章

  1. Kotlin实现LeetCode算法题之Two Sum

    LeetCode介绍 LeetCode是算法练习.交流等多功能网站,感兴趣的同学可以关注下(老司机请超车).页面顶部的Problems菜单对应算法题库,附带历史通过滤.难易程度等信息. 未来计划 打算 ...

  2. LeetCode算法题-Minimum Index Sum of Two Lists(Java实现)

    这是悦乐书的第272次更新,第286篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第139题(顺位题号是599).假设Andy和Doris想要选择一家餐馆吃晚餐,他们都有 ...

  3. 算法题丨3Sum

    描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  4. 算法题丨3Sum Closest

    描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  5. 算法题丨4Sum

    描述 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  6. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

  7. 算法题丨Longest Consecutive Sequence

    描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...

  8. 算法题丨Remove Element

    描述 Given an array and a value, remove all instances of that value in-place and return the new length ...

  9. 算法题丨Move Zeroes

    描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...

随机推荐

  1. 命令行更新node和npm

    Windows系统下: 查看版本的命令和Ubuntu下一样. 不同的是Windows下不能使用"n"包管理器来对NodeJS进行管理,在这里我们使用一种叫"gnvm&qu ...

  2. WebService下实现大数据量的传输

    设置RemotingFormat = SerializationFormat.Binary;再序列化,通过WebService传输,客户端接收,再反序列化,确实效果大大的优于直接传送DataSet,不 ...

  3. Problem : 1202 ( The calculation of GPA )

    Losers always whine about their best. Winners go home and fuck the prom queen. 很操蛋却非常有意思的题目,注意变量的类型, ...

  4. 封装Jquery 合并table中任何同列数据

    封装代码: jQuery.fn.rowspan = function (colIdx) { //封装JQuery同列值相同合并小插件 return this.each(function () { va ...

  5. JAVA学习:面向对象编程

    "算法+数据结构=程序"是一句经典名言,这句话很直接的说明了程序的本质:处理数据.产生结果.即便是最简单的HelloWorld程序,我们也可以将字符串"HelloWorl ...

  6. Go a lot of way but I go back to the original point

    I try a lot of blog platform and even construct my blog website. But I have to say I just want to ha ...

  7. 记录一则enq: TX - row lock contention的分析过程

    故障描述:与客户沟通,初步确认故障范围大概是在上午的8:30-10:30之间,反应故障现象是Tomcat的连接数满导致应用无法连接,数据库alert中无明显报错,需要协助排查原因. 1.导入包含故障时 ...

  8. JAVA获取文件数据 ( xxxxx.json )

    //路径fPixFile filePath = new File(fPix);System.out.print("文件路径:" + filePath);try { if (file ...

  9. Matlab绘图基础——axis设置坐标轴取值范围

    peaks; axis tight  %Set the axis limits to equal the range of the data  axis square axis 'auto x'  % ...

  10. Android 动画 属性动画 视图动画 补间动画 帧动画 详解 使用

    Android动画 Property Animation res/animator/filename.xml In Java: R.animator.filename In XML: @[packag ...