Two Sum【LeetCode】
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】的更多相关文章
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
随机推荐
- 2017-2018-2 20165221实验二《Java面向对象程序设计》实验报告
JAVA实验二报告 课程:Java程序设计 姓名:谭笑 学号:20165221 实验时间:2018.4.13--2018.4.15 实验2--1 实验内容 实现百分制成绩转成"优.良.中.及 ...
- 3D中的旋转变换
相比 2D 中的旋转变换,3D 中的旋转变换复杂了很多.关于 2D 空间的旋转,可以看这篇文章.本文主要粗略地探讨一下 3D 空间中的旋转. 旋转的要素 所谓旋转要素就是说,我们只有知道了这些条件,才 ...
- SpringSecurityOAuth使用JWT Token
⒈JWT? JWT(Json Web Token),是Json的一个开放的Token标准. 1,自包含,SpringSecurityOAuth的默认Token是UUID的一个随机的无意义的字符串,并不 ...
- DAC杂谈一
DAC种类: 有权电阻网络DAC 输出电压变化范围为:0~-(2^n-1)/2^n*Vref 当位数很高时,每个电阻都有很高精度是十分困难的. 倒T型电阻网络DAC(比如AD7520 10bit 已停 ...
- pt-table-sync 使用方法【转】
28. pt-table-sync28.1 pt-table-sync 作用 使用对两个库不一致的数据进行同步,他能够自动发现两个实例间不一致的数据,然后进行sync操作,pt-table-sync无 ...
- win7经常出现“关闭xxxx前您必须关闭所有会话框”
这可能是windows的一个BUG,在没有关闭输入法的状态下它不默认你关闭了所有窗口,只要把输入法切换回默认的英文输入法就可以正常关闭了
- 用户 'IIS APPPOOL\xxxx' 登录失败
一.现象发布到iis后,网站出现以下提示异常详细信息: System.Data.SqlClient.SqlException: 用户 'IIS APPPOOL\IdealTest' 登录失败. 二.分 ...
- Linux内核之进程地址空间
Linux内核之进程地址空间 内核中的函数以相当直接了当的方式获得动态内存: __get_free_pages 或 alloc_pages从分区页框分配器中获得页框; kmem_cache_alloc ...
- C++面向对象的特点
C++面向对象的特点 面向对象的特点主要有: 封装, 继承, 多态; 现在自己的简单理解如下, 但要明白具体怎么实现, 背后的原理是什么? 什么是封装, C++怎么实现封装 封装的大致可以分为: 函数 ...
- 码云 Git No network connection
背景 git,好久没用了都.心血来潮还是赶紧熟悉下吧.果不其然,每次要熟悉或开始一个东西,立马就出现问题.本来好好的环境竟然不行了,搞定后感觉应该是换了网络环境的事.这倒次要,主要让我想起了最初学习g ...