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 ...
随机推荐
- springboot中.yml没有spring的小叶子标志解决办法
我的idea springboot项目中有两个.yml文件,一个application.yml,一个log4j2.yml,但是只有application.yml显示的是树叶图标,如下所示 做如下配置后 ...
- 极客时间|AI技术内参
学习进度: 014 | 精读AlphaGo Zero论文
- ubuntu14.04升级mysql5.5至mysql5.7
原文链接:https://www.cnblogs.com/os-python/p/6842485.html 1.下载mysql-apt的配置包,并安装 wget https://dev.mysql.c ...
- MySQL绿色版安装整套流程
https://www.cnblogs.com/LiuChunfu/p/6426918.html
- 【Linux-Redhat】新手需要知道的Linux命令
好像接触运维有一年的时间了吧,查的资料什么的,也算是挺多的了.再加上最近看的<Linux就该这么学>,也算是把自己最近学的东西系统化了一下.今天就来说说,常用的Linux命令有什么,如果你 ...
- Linux运行时I/O设备的电源管理框架【转】
转自:https://www.cnblogs.com/coryxie/archive/2013/03/01/2951243.html 本文介绍Linux运行时I/O设备的电源管理框架.属于Linux内 ...
- 国产 WEB UI 框架 (收费)-- Quick UI,Mini UI
国产 WEB UI 框架 (收费)-- Quick UI,Mini UI : http://www.uileader.com/ http://www.miniui.com/
- python3+selenium入门11-窗口切换
在打开新的浏览器窗口时,如果要定位新窗口的元素,需要先切换到这个新打开的窗口中,才能定位到该窗口下的元素. current_window_handle:获取当前句柄.可以把句柄理解成窗口的身份证 wi ...
- Winform调用百度地图接口简单示例
1.首先用一个html文件调用百度地图接口(主要注册一个序列号): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...
- MySQL的数据文件存储
MySQL的数据文件存储 MyISAM引擎分为:静态.动态和压缩MyISAM三种: 静态MyISAM:如果数据表中的各数据列的长度都是预先固定好的,服务器将自动选择这种表类型.因为数据表中每一条记录所 ...