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: input: nums = [2, 7, 11, 15] , target=9.

output:  [0, 1]

给定一个整型数组与一个目标数,输出两个数相加为此目标数的下标。(只有一个结果,且每个数只能使用一次)

1. 最简单的方法就是双重循环,取到之后return. 不过太耗时了,肯定不符合要求。

所以我在每次循环里面,查找数组有没有另一个值。(做算法题的话应该不能使用.ToList()这样的方法吧?)

public int[] TwoSum(int[] nums, int target) {
for(int index = ; index < nums.Length; index++){
int element = nums[index];
int otherElement = target - element;//查找是否存在
int otherIndex = nums.ToList().IndexOf(otherElement);//主要是这一步
if(otherIndex != - && otherIndex != index){
return new int[]{index,otherIndex};
}
}
return null;
}

耗时:652ms.  内存:48.7MB

2. 在上面的基础上从把数组转换成字典,从其中查找,查找速度应该比数组快。

 public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> dic = new Dictionary<int, int>();
for(int index = ; index < nums.Length; index++){
dic[index] = nums[index];
}
for(int index = ; index < nums.Length; index++){
int element = nums[index];
int otherElement = target - element;//查找是否存在
bool containsValue = dic.ContainsValue(otherElement);//主要是这一步
if(containsValue){
int foundKey = -;
foreach (int key in dic.Keys){
if (dic[key] == otherElement && key != index){
foundKey = key;
return new int[]{index, foundKey};
}
}
}
}
return null;
}

耗时:788 ms.  内存:29.6 MB  不过找到元素后还得取出下标,又是一个循环。不过内存占用变小了。

3. 只需要一个循环。 每次查找当前元素的匹配项前,把前一个数放入字典中,从字典查找匹配项。如果匹配到了,则当前index为后一个数的下标。

 public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> dic = new Dictionary<int, int>();
for(int index = ; index < nums.Length; index++){
if(dic.ContainsKey(target - nums[index]))
return new int[]{dic[target - nums[index]],index};
dic[nums[index]] = index;
}
  return null;
}

252 ms. 29.4 MB。题目中说结果只有一个,所以就算是数组元素作为key也没事。而且通过key容易找出value下标值。

但是如果会有多个重复值,上述就不适用了,给同一个key赋值,value会被覆盖掉。

例如 input:[5,5,8,3]  target=13。 本应该输入[0, 2], 可是上述结果却输出了[1, 2].

因此需要使用下标作为key才行. 不过此时通过value快捷查找key又成了问题。看看以后会不会有这类题目。

1. Two Sum [Array] [Easy]的更多相关文章

  1. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

  2. [array] leetCode-26. Remove Duplicates from Sorted Array - Easy

    26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...

  3. LeetCode--Sort Array By Parity && N-Repeated Element in Size 2N Array (Easy)

    905. Sort Array By Parity (Easy)# Given an array A of non-negative integers, return an array consist ...

  4. LeetCode Array Easy 167. Two Sum II - Input array is sorted

    Description Given an array of integers that is already sorted in ascending order, find two numbers s ...

  5. LeetCode Array Easy 1. Two Sum

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

  6. Group and sum array of hashes by date

    I have an array of hashes like this: [{:created=>Fri, 22 Jan 2014 13:02:13 UTC +00:00, :amount=&g ...

  7. LeetCode--219、268、283、414、448 Array(Easy)

    219. Contains Duplicate II Given an array of integers and an integer k, find out whether there are t ...

  8. LeetCode--122、167、169、189、217 Array(Easy)

    122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...

  9. LeetCode--1、26、27、35、53 Array(Easy)

      1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to ...

随机推荐

  1. PCA原理(转)

    PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降 ...

  2. memory management

    1. 高端内存: 内存的物理寻址范围比虚拟寻址范围大的多,有一些内存页不能永久的映射到内核地址空间. 2. 高端内存和低端内存是内核对内存物理页的划分. 参考:http://ilinuxkernel. ...

  3. 安装RabbitMq-----windows

    在官网download我们所需要的版本,安装rabbitMq需要erlang支持 rabbitMq :http://www.rabbitmq.com/download.html erlang  :ht ...

  4. 视图的URL配置,找不到我设置的第一个Page

    问题:视图的URL配置,找不到我设置的第一个Page 我的代码如下: 结果访问/test/时说找不到这个page   原因:patterns方法的参数有两个,一个是prefix,一个是参数元祖,详见下 ...

  5. 关于number...的精度问题

    一 当数字的精度被定为number(3,2)时, 这时他能输入的数字整数部分只能是3-2=1位, 小数位如果不够会用0补齐, 超出的四舍五入保留3位小数. SQL> insert into t_ ...

  6. SQLserver和oracle中对应的数据类型

  7. mysql优化概述4

    一.分区 1.分区概念 将某张表数据,分别存储到不同的区域中. 每个分区,都是独立的表,都要存储该分区的数据,索引信息. 2.创建分区 创建表并指定分区的选项 create table 表名 ( 定义 ...

  8. c语言使用指针实现模拟java/c# string.concat字符串串联方法

    #include <stdio.h> void _strcat(char *, const char *); int main(void) { char source[] ="V ...

  9. Underscore模版引擎的使用-template方法

    之前项目里有遇到在DOM中增加大量的html结构的时候,傻乎乎的在js中写一堆模版,然后用replace一个一个做替换.当时就是难看了点,不觉得啥,现在了解了模版引擎之后回头来看真的比较捉急了,以后是 ...

  10. BZOJ1221 [HNOI2001]软件开发 - 费用流

    题解 非常显然的费用流. 但是建图还是需要思考的QuQ 将每天分成两个节点 $x_{i,1}, x_{i,2} $, $ x_{i,1}$用于提供服务, $x_{i ,2}$ 用来从源点获得$nd[i ...