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:

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

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

my soluction:

public class Solution {
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> dic = new Dictionary<int, int>();
for (int i = ; i < nums.Length; i++)
{
dic.Add(i,nums[i]);
//dic.Add(nums[i],i);
} for (int i = ; i < nums.Length; i++)
{
int complement = target - nums[i];
if (dic.ContainsValue(complement))
{
var keys=dic.Where(m=>m.Value==complement).Select(m=>m.Key);
foreach(var item in keys)
{
if(item != i)
{
return new int[]{i,item};
}
} }
}
throw new Exception("none");
}
}
												

Two Sum【LeetCode】的更多相关文章

  1. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  2. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  3. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  5. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  6. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  9. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

随机推荐

  1. struts文件下载机制

    Struts2 中使用 type="stream" 的 result 进行下载即可.只用提供一个输入流inputStream,剩下的输出工作struts帮我们做. 例子一: 1.可 ...

  2. android PopupWindow 显示问题

    设置可点击,其余控件也可点击,等操作 setOutsideTouchable(true);   setBackgroundDrawable(new BitmapDrawable()); setTouc ...

  3. 数据库无法启动ORA-01034: ORACLE not available

    错误场景: 1.数据库未启动,查询v$instance报错 SQL> select status from v$instance; select status from v$instance * ...

  4. Java HashMap工作原理及实现[转]

    原文:http://yikun.github.io/2015/04/01/Java-HashMap%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE ...

  5. adapter.notifydatasetchanged()没有效果

    项目中有个列表的处理,通过一个参数判断是下拉刷新数据还是加载更多数据,结果下拉刷新就是显示不出来界面,数据是有,就开始searching~,搜出很多相关问题,大意如下: 1 当数据源发生变化的时候,我 ...

  6. 【Linux-Redhat】新手需要知道的Linux命令

    好像接触运维有一年的时间了吧,查的资料什么的,也算是挺多的了.再加上最近看的<Linux就该这么学>,也算是把自己最近学的东西系统化了一下.今天就来说说,常用的Linux命令有什么,如果你 ...

  7. Go语言程序开发初涉

    由于工作原因,现在开始学习Go语言.这也是本人第一篇关于Go的博客.本文将通过一些基本概念的说明和实际案例,使得大家能快速对Go程序的开发有个了解. 一. Go的安装 :     在 https:// ...

  8. sudo su 和 sudo -s【转】

    sudo su 和 sudo -s 都是切换到root用户,不同的是 sudo su 环境用的是目标用户 (root)的环境 sudo -s 环境用的是当前用户本身的环境 转自 sudo su 和 s ...

  9. HttpResonse 要记得关闭

    写了一个小程序,要识别网站上的图片,用到HttpRequest发请求,HttpResponse接受返回数据.刚开始循环可以跑两个,但是总是提示超时.后来查资料发现,在Http协议中,规定了同个Http ...

  10. 一个优秀windows C++ 程序员该有哪些知识