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的更多相关文章

  1. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  2. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

  3. 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  ...

  4. 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 ...

  5. 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 ...

  6. C# 写 LeetCode easy #20 Valid Parentheses

    20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  7. 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  ...

  8. C# 写 LeetCode easy #9 Palindrome Number

    9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...

  9. C# 写 LeetCode easy #7 Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...

随机推荐

  1. power law 幂定律

    y=cx∧a 卖品销量排名与销量

  2. 响应式布局【3】 --- bootstrap

    本片文章主要讲解Bootstrap中如何实现响应式布局的. 参考资料&内容来源: https://code.ziqiangxuetang.com/bootstrap/bootstrap-gri ...

  3. 电话聊天狂人 【STL】

    7-2 电话聊天狂人(25 分) 给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人. 输入格式: 输入首先给出正整数N(≤10​5​​),为通话记录条数.随后N行,每行给出一条通话记录.简单起 ...

  4. python源码安装的包的卸载

    python setup.py install安装的包如何卸载 在使用源码安装的过程中,记录安装文件细节,如: python setup.py install --record log 这时所有的安装 ...

  5. Exception of type 'System.OutOfMemoryException' was thrown

    最近刚换了服务器,开始测试的时候未发现什么问题,可是一旦同一时间段操作的人比较多的时候,就会抛出如下错误: Server Error in '/' Application. Exception of ...

  6. 在ubuntu环境安装youcompleteme

    sudo apt-get update #更新软件源 sudo apt-get clang #安装clang sudo apt-get cmake #安装cmake sudo apt-get inst ...

  7. LoadRunner监控图表与配置(三)对系统与网络资源进行监控

    1.Windows可监控的性能计数器 2.UNIX可监控的性能计数器 3.性能计数器信息说明 Windows 性能对象 计数器 数值说明 System %Total Processor Time 系统 ...

  8. 「SDOI 2009」Elaxia的路线

    发现自己这几天智商完全不在线-- 这道题的数据十分的水,怎样都可以艹过去-- 开始想了一个完全错误的算法,枚举一对点,判断这一对点是否同时在两条最短路上,是就用两点之间的路径更新答案.显然这样是错的: ...

  9. workerman介绍

    WorkerMan的特性 1.纯PHP开发 使用WorkerMan开发的应用程序不依赖php-fpm.apache.nginx这些容器就可以独立运行. 这使得PHP开发者开发.部署.调试应用程序非常方 ...

  10. 「P3385」【模板】负环(spfa

    题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 ...