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. oozie错误:javax.servlet.jsp.el.ELException: variable [***] cannot be resolved

    完整错误: javax.servlet.jsp.el.ELException: variable [compute] cannot be resolved at org.apache.oozie.ut ...

  2. Containerpilot 配置文件模板

    { "consul": "{{ .CONSUL }}:8500", "logging": { "level": &quo ...

  3. docker-composer

    1.安装docker-composer   参考官方 安装1.20.1 sudo curl -L https://github.com/docker/compose/releases/download ...

  4. Spring Boot logback

    前言 今天来介绍下spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...

  5. Java对称与非对称加密解密,AES与RSA

    加密技术可以分为对称与非对称两种. 对称加密,解密,即加密与解密用的是同一把秘钥,常用的对称加密技术有DES,AES等 而非对称技术,加密与解密用的是不同的秘钥,常用的非对称加密技术有RSA等 为什么 ...

  6. C/C++ 宏

    0. #define MALLOC(n, type) \ ( (type *) malloc((n)* sizeof(type))) 1. 宏可以像函数一样被定义,例如:    #define min ...

  7. ubuntu安装谷歌拼音输入法

    在这篇教程中,我将告诉你如何在ubuntu系统上安装谷歌拼音输入法.谷歌拼音输入法有基于ibus框架的,也有基于fcitx框架的.我只演示fcitx框架下谷歌拼音输入法的安装,因为ibus框架的谷歌拼 ...

  8. Codeforces Beta Round #12 (Div 2 Only)

    Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 #include<bits/stdc++ ...

  9. SQL Server优化50法(转载)

           虽然查询速度慢的原因很多,但是如果通过一定的优化,也可以使查询问题得到一定程度的解决. 查询速度慢的原因很多,常见如下几种:     1.没有索引或者没有用到索引(这是查询慢最常见的问题 ...

  10. apache日志里出现GET http://wujieliulan.com/mnews.htmHTTP/1.1解决方法

    笔者最近搭建了服务器,Ubuntu 16.04.3 LTS,apache日志里老是出现一些莫名其妙的日志. 好像谁用了我的服务器做代理,日志如下 凭借着小学文化 :) 勉强读懂了汉语拼音wujieli ...