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. 第二节,mnist手写字体识别

    1.获取mnist数据集,得到正确的数据格式 mnist = input_data.read_data_sets('MNIST_data',one_hot=True) 2.定义网络大小:图片的大小是2 ...

  2. JDK源码笔记--Object

    public final native Class<?> getClass(); public native int hashCode(); public boolean equals(O ...

  3. CF1096D Easy Problem

    题目地址:CF1096D Easy Problem 比赛时高二dalaoLRZ提醒我是状压,然而,我还是没AC (汗 其实是一道很基础的线性dp \(f_{i,j}\) 表示序列第 \(i\) 个字符 ...

  4. 一言难尽的js变量提升

    基础知识  在这个课题开始之前我们先做一些基础知识的讲解 1.在顶级的区域内声明的变量为 window级别的变量. 也就是说var a=100 等价于 window.a=100; 2.局部的重新声明变 ...

  5. GAN_李弘毅讲解

    GAN_李弘毅讲解: 上式中,xi从data中sample的一部分,现在的目的就是最大化这个似然函数,使得Generator最可能产生data中的这些sample: 上式中之所以如此设计V函数,是为了 ...

  6. libevent学习笔记 一、基础知识【转】

    转自:https://blog.csdn.net/majianfei1023/article/details/46485705 欢迎转载,转载请注明原文地址:http://blog.csdn.net/ ...

  7. python3+selenium框架设计07-unittest单元测试框架

    可以自行百度学习下单元测试框架,或者看Python3学习笔记26-unittest模块 在项目下新建一个entrance.py文件.并使用之前的测试用例进行演示.目前项目结构. ​ 在entrance ...

  8. 使用Boost Regex 的regex_search进行遍历搜索

    在regex_search函数中,会将找到的第一个匹配结果保存到一个smatch类中. 然而如果搜索字符串中有多个匹配结果,则需要自己实现了. 在smatch中,有两个成员,官方文档如下: itera ...

  9. 查询设备的IP地址/掩码/MAC/网关

    import commands, sys import platform from _utils.patrol2 import run_cmd, data_format, report_format ...

  10. windows下django1.7 +python3.4.2搭建记录1

    python+django在linux下搭建比较简单,windows下搭建比较复杂,所以列在下方一.下载安装下载django的包,到刚解压后的Django-1.7目录下执行命令 python setu ...