C# 写 LeetCode easy #1 Two Sum
1、Two Sum
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]. 代码:
static void Main(string[] args)
{
int[] nums = { , , , };
var res=Twosum(nums, );
Console.WriteLine($"[{res[0]},{res[1]}]");
Console.ReadKey();
} public int[] TwoSum(int[] nums, int target)
{
var dictionary=new Dictionary<int,int>();
for (int i = ; i < nums.Length; i++)
{
var nn = target - nums[i];
if (dictionary.ContainsKey(nn))
{
return new int[]{ dictionary[nn],i};
}
dictionary[nums[i]] = i;
}
throw new ArgumentException();
}
解析: 输入:整型数组num和整型目标值。
输出:一个数组,由得到目标值的两个数的索引构成。
代码思想:
首先,字典的key是num数组中的元素,value是这个元素所对应的索引。
其次,通过遍历整个数组,即假设已知第一个数,求出第二个数并判断是否在字典中,若在,则返回第二个数在字典中的value值与第一个数的索引组成的数组。若不在,将第一个元素和索引添加到字典中,继续判断第二个数。
最后,若找到会返回数组,若没有则抛出异常。
时间复杂度:O(n)
C# 写 LeetCode easy #1 Two Sum的更多相关文章
- C# 写 LeetCode easy #14 Longest Common Prefix
		
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
 - C# 写 LeetCode easy #28 Implement strStr()
		
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
 - C# 写 LeetCode easy #27  Remove Element
		
27. Remove Element Given an array nums and a value val, remove all instances of that value in-place ...
 - C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array
		
26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...
 - C# 写 LeetCode easy #21 Merge Two Sorted Lists
		
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
 - C# 写 LeetCode easy #20 Valid Parentheses
		
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
 - C# 写 LeetCode easy #13 Roman to Integer
		
13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and ...
 - C# 写 LeetCode easy #9 Palindrome Number
		
9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...
 - C# 写 LeetCode easy #7 Reverse Integer
		
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...
 
随机推荐
- window窗口的各种宽高
			
一 常用的宽高属性 在日常开发的时候,我们常常需要用到这几个高度信息.浏览器的视口高度和宽度,浏览器的卷动高度,正文内容的总高度等等信息,我在下图中列出了在工作中最常用的几个宽度和高度信息.并在本篇文 ...
 - ideal 快捷键
			
1.输入sout --> System.out.println(); 2.输入psvm --> main函数; IntelliJ Idea 2017 免费激活方法 1. 到网站 http: ...
 - 【题解】[CF718C Sasha and Array]
			
[题解]CF718C Sasha and Array 对于我这种喜欢写结构体封装起来的选手这道题真是太对胃了\(hhh\) 一句话题解:直接开一颗线段树的矩阵然后暴力维护还要卡卡常数 我们来把\(2 ...
 - 快速学习Symfony4,Symfony4教程
			
快速学习Symfony4,Symfony4教程https://symfony.com/doc/current/index.html 关注公众号回复Symfony4教程,Symfony4教程视频,或访问 ...
 - springboot 默认tomcat配置
			
1. Spring Boot 能支持的最大并发量主要看其对Tomcat的设置,可以在配置文件中对其进行更改.当在配置文件中敲出max后提示值就是它的默认值. 我们可以看到默认设置中,Tomcat的最大 ...
 - BZOJ 4523  [Cqoi2016]路由表  Trie树
			
Trie树的应用题目. 在线建立一棵01 Trie树,然后按照要求用询问在上面跑,用单调栈维护答案即可. #include<iostream> #include<cstdio> ...
 - mysql too many connections 解决方法
			
1.mysql -u root -p 回车输入密码进入mysql 2.show processlist; 查看连接数,可以发现有很多连接处于sleep状态,这些其实是暂时没有用的,所以可以kill ...
 - sqlite:多线程操作数据库“database is locked”解决方法
			
1. 使sqlite支持多线程(不确定是否非加不可,暂且加上,以备后患) 可以在编译时/启动时/运行时选择线程模式,参考:http://www.cnblogs.com/liaj/p/4015219.h ...
 - 2017 google IO大会——5.17
			
大家好! 每年一度的全球互联网及新型技术的盛会 Google IO,今年的大会日期和地址已经公布了:大会将在5月17至19日在谷歌公司总部边上的会场,美国加州 Mountain View的 Shore ...
 - 详解C/C++ 编译 g++ gcc 的区别
			
我们在编译c/c++代码的时候,有人用gcc,有人用g++,于是各种说法都来了,譬如c代码用gcc,而c++代码用g++, 或者说编译用gcc,链 接用g++,一时也不知哪个说法正确,如果再遇上个ex ...