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 ...
随机推荐
- Nginx系列1:ubuntu16.04编译出适合自己的nginx服务器
1.下载nginx nginx官网:nginx.org tar.gz文件 解压缩命令: wget https://nginx.org/download/nginx-1.14.2.tar.gz #下载n ...
- flask基本介绍及虚拟环境
Flask Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架. Flask 本身相当于一个内核,其他几乎 ...
- python正则表达式获取代理IP网站上的IP地址
import urllib.request import re def open_url(url): req = urllib.request.Request(url) req.add_header( ...
- OGG-01168
https://blog.csdn.net/zhrzhl/article/details/21698659
- h5中placeholder样式
<!DOCTYPE html> <html> <head> <title>placeholder样式demo</title> <sty ...
- C实战:项目构建Make,Automake,CMake【转】
转自:https://blog.csdn.net/dc_726/article/details/48978849 版权声明:本文为博主原创文章,未经博主允许不得转载.欢迎访问 http://blog. ...
- Linux下自动清理超过指定大小文件
作者:邓聪聪 扫描某个目录下的文件,发现超过指定大小即清空 1)扫描目录下的文件 2)判断文件大小 3)清空大于指定文件的内容 以byte为单位显示文件大小,然后和20M大小做对比. 20M换算成字节 ...
- webp 图形文件操作工具包 win32 (编译 libwebp-20171228-664c21dd 版本)
源码下载地址 https://chromium.googlesource.com/webm/libwebp/ 版本 libwebp-20171228-664 ...
- 缓存系列之二:CDN与其他层面缓存
缓存系列之二:CDN与其他层面缓存 一:内容分发网络(Content Delivery Network),通过将服务内容分发至全网加速节点,利用全球调度系统使用户能够就近获取,有效降低访问延迟,提升服 ...
- Laravel 怎么在 blade 视图中将带 HTML 字符原样输出
### 感觉这是比较细小的,细节处理问题,很容易就一下子想不起怎么处理 但知道处理方式是那么简单时,真的觉得基础不够扎实 ### 富文本编辑内容: 视图原样输出: 视图模板的标签是这样处理就可以的-- ...