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 ...
随机推荐
- Nunit常用的方法说明
下来还是分为2个部分,一是NUnit的布局,另外一部分就是它的核心概念. 首先熟悉一下NUnit GUI的布局. 让我们更进一步看一下测试运行器窗口的布局.在右边面板的中间,可以看到测试进度条.进度条 ...
- Chi-Square Statistic/Distribution
. 1.What is a Chi Square Test? 卡方检验有两种类型.两者使用卡方统计量和分布的目的不同. 第一种:卡方拟合优度检验确定样本数据是否与总体匹配.(这里不介绍) 第二种:独立 ...
- 大型运输行业实战_day10_1_自定义事务管理类
1.创建事务管理类 TransactionManager.java package com.day02.sation.transaction; import com.day02.sation.uti ...
- 大型运输行业实战_day03_2_使用ajax将请求页面与请求数据分离
1.引入jquery 1.添加jquery包 2.在要使用jquery的页面中引入jquery 引入jquery后必须检查是否引入正确,这里值得注意的是 springMVC默认情况先会拦截 js文件, ...
- eclipse zg项目学习
一.基本知识 1.新增测试系统: xx/jsp:用于摆放jsp xx/src:放置java source 2.在项目上,右键,New-Folder,新建xx文件夹. 同样的方法,在xx文件夹上,右键N ...
- TensorFlow—张量运算仿真神经网络的运行
import tensorflow as tf import numpy as np ts_norm=tf.random_normal([]) with tf.Session() as sess: n ...
- 安卓机在按HOME键时,UNITY触发的APPLICATION_PAUSE事件
安卓机在按HOME键时,UNITY触发的APPLICATION_PAUSE事件 此时安卓程序会返回,在这一瞬间,程序可以通过SOCKET发送数据包给服务器告知, 经测试在这短暂的时间内,这个数据包能发 ...
- GridView控件中的一些常见问题
1. 无法获取模板列中的值,使用FindControl()方法无效: 给模板列中添加隐藏域,并给隐藏域绑定要获取的值,代码如下: <asp:HiddenField ID="hfIsFr ...
- Django的cookie学习
为什么要有cookie,因为http是无状态的,每次请求都是独立的,但是我们还需要保持状态,所以就有了cookie cookie就是保存在客户端浏览器上的键值对,别人可以利用他来做登陆 rep = r ...
- 利用Google Chrome开发插件,在网页中植入js代码
Google Chrome是一个很强大的浏览器,提供了各种各样的插件,大大提升了使用了的效率,比如vimium.honx等. Google在提供这些插件的同时还允许用户开发自己的插件. 最近在写js的 ...