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. ajax传递数组,后台更新

    js: var rows = $("#stu_reg_table").datagrid("getSelections"); if(rows != "& ...

  2. 20165237 2017-2018-2 《Java程序设计》第7周学习总结

    20165237 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 1.MySQL数据库管理系统,简称MySQL,是世界上最流行的开源数据库管理系统. 2.启动 ...

  3. 在 sql server 中,查询 数据库的大小 和 数据库中各表的大小

    其实本来只想找一个方法能查询一下 数据库 的大小,没想到这个方法还能查询数据库中 各个数据表 的大小,嗯,挺好玩的,记录一下. MSDN资料:https://msdn.microsoft.com/zh ...

  4. [转]python3之模块psutil系统性能信息

    转自:https://www.cnblogs.com/zhangxinqi/p/9106265.html 阅读目录 1.psutil模块安装 2.获取CPU信息 3.内存信息 4.磁盘信息 5.网络信 ...

  5. Python-列表的常用操作

    #先定义一个列表 names = ["ZhangXueyou","LiMing","GuoFucheng","LiuDehua&q ...

  6. python 大全

    python 大全:https://awesome-python.com/ 生产 GUI 应用的库 :PyQt ,PySide , 不错   (https://kivy.org)kivy - A li ...

  7. Liunx之始

    作者:邓聪聪 本章是为了工作之需要,所做的一个学习心得,也为自己留下相关印记以便日后留用. 1:安装服务yum install ppp -yyum install pptpd -y由于yum源没有pp ...

  8. HDU 3333 Turing Tree 离线 线段树/树状数组 区间求和单点修改

    题意: 给一个数列,一些询问,问你$[l,r]$之间不同的数字之和 题解: 11年多校的题,现在属于"人尽皆知傻逼题" 核心思想在于: 对于一个询问$[x,R]$ 无论$x$是什么 ...

  9. Unity3D RTS游戏中帧同步实现

    帧同步技术是早期RTS游戏常用的一种同步技术,本篇文章要给大家介绍的是RTX游戏中帧同步实现,帧同步是一种前后端数据同步的方式,一般应用于对实时性要求很高的网络游戏,想要了解更多帧同步的知识,继续往下 ...

  10. docker拉取镜像报错:net/http: TLS handshake timeout.

    docker拉取镜像报错:net/http: TLS handshake timeout. 启动一个后台的busybox容器 [yunva@node1 network-scripts]$ docker ...