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 ...
随机推荐
- git学习------>"Agent admitted failure to sign using the key." 问题解决方法
今天用git clone 命令clone服务器上的代码时候报了如下的错误: ouyangpeng@oyp-ubuntu:~/Android/git_canplay_code$ git clone gi ...
- python cookbook第三版学习笔记四:文本以及字符串令牌解析
文本处理: 假设你存在一个目录,下面存在各种形式的文件,有txt,csv等等.如果你只想找到其中一种或多种格式的文件并打开该如何办呢.首先肯定是要找到满足条件的文件,然后进行路径合并在一一打开. pa ...
- 关于left join遇到where就不管用的问题
今天做了个存储过程,需要的功能是查询所有人的得分,有人没分就给零分,显而易见这里用左外连接是没有问题的, 但是在连接完之后有个根据时间筛选功能,于是我加了where条件判断,这时候没有想到的事情发生了 ...
- MySQL多表查询一网打尽
现有四张表 mysql> select * from student; +------+--------+-------+-------+ | s_id | s_name | s_age | s ...
- CountDownLatch,CyclicBarrier,Semaphore的使用
什么时候使用CountDownLatch CountDownLatch原理和示例 Semaphore信号量的原理和示例 CyclicBarrier的用法 CyclicBarrier 和 CountDo ...
- okhttp 请求list数据实例
public class DataBean { /** * id : 61684 * movieName : <猜火车2>先导预告片 * coverImg : http://img31.m ...
- 分享知识-快乐自己:Oracle中定义及使用同义词
Oracle 同义词概念: Oracle的同义词(synonyms)从字面上理解就是别名的意思,和视图的功能类似,就是一种映射关系. 它可以节省大量的数据库空间,对不同用户的操作同一张表没有多少差别; ...
- 时间序列数据库——索引用ES、聚合分析时加载数据用什么?docvalues的列存储貌似更优优势一些
加载 如何利用索引和主存储,是一种两难的选择. 选择不使用索引,只使用主存储:除非查询的字段就是主存储的排序字段,否则就需要顺序扫描整个主存储. 选择使用索引,然后用找到的row id去主存储加载数据 ...
- win装wamp
前传: 刚换了工作环境,从原来的全mac转到全windows,很不适应,简单的wamp鼓捣了半天 环境: win 7 旗舰版 ,wamp ,下载地址: http://pan.baidu.com/s/1 ...
- [Android Studio] Android Studio快速定位当前打开的文件在哪个目录(package)下
转载自:http://blog.csdn.net/hyr83960944/article/details/38067499 在Eclipse中有一个很好的功能,就是比如我打开一个AActivity,左 ...