1. Two Sum [Array] [Easy]
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. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- [array] leetCode-26. Remove Duplicates from Sorted Array - Easy
26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 吴裕雄 实战PYTHON编程(8)
import pandas as pd df = pd.DataFrame( {"林大明":[65,92,78,83,70], "陈聪明":[90,72,76, ...
- Kconfig详解
当执行#make menuconfig时会出现内核的配置界面,所有配置工具都是通过读取"arch/$(ARCH)Kconfig"文件来生成配置界面,这个文件就是所有配置的总入口,它 ...
- linux kernel 的配置及编译
1. 执行make menuconfig 配置内核 2. 执行make zImage 编译内核 3. 执行make modules 编译模块 4. 内核源代码的配置及编译系统 Makefile Kco ...
- KEGG下载某物种最新的版本信息(斑马鱼为例)
步骤一:打开链接并选择物种 http://www.genome.jp/kegg-bin/get_htext?hsa00001+3101 步骤二:对文件进行解析 步骤三:统计信息 一级结构(6大类): ...
- svn搭建相关
转载至:http://blog.163.com/longsu2010@yeah/blog/static/173612348201202114212933/ 安装步骤如下: 1.yum install ...
- phpStudy4——前端页面使用Ajax请求并解析php返回的json数据
项目需求: 在html页面显示所有用户列表信息. 需求分析: 1. html页面使用ajax向后端php请求用户数据 2. php脚本查询数据库,并将查询后的结果以json格式返回前端html页面 3 ...
- ecplice中代码使用快捷键无法格式化,使用其他方法将代码格式化的步骤
选中需要进行格式化的代码--->右键--->source--->format,就可以将代码格式化了.
- mysql rc模式时binlog_format=row的解释【转】
总体来说:在 tx_isolation= READ-COMMITTED .binlog_format =statement 的情况下,mysql 没有gap 锁,这样binlog 记录的数据修改的顺序 ...
- oracle 表分区例子
oracle表分区详解-一步一步教你oracle分区表详解 .创建三个不同的表空间,模拟在不同磁盘上的保存不同范围的数据 create tablespace test01 datafile ...
- cxf soap rest webservice spring
1. 导入 jar 包 2. 编写接口 3. 编写实现 4. 配置spring 配置文件 5. 配置web.xml servlet 6. 访问 package com.diancai.test; im ...