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. Webpack探索【16】--- 懒加载构建原理详解(模块如何被组建&如何加载)&源码解读

    本文主要说明Webpack懒加载构建和加载的原理,对构建后的源码进行分析. 一 说明 本文以一个简单的示例,通过对构建好的bundle.js源码进行分析,说明Webpack懒加载构建原理. 本文使用的 ...

  2. 跳转appStore评分

    跳转到AppStore让用户能够给我们的应用进行评分,有两种方法,一种是跳出应用,跳转到AppStore,进行评分.另一种是在应用内,内置AppStore进行评分. PS:appleID在https: ...

  3. bzoj5093: [Lydsy1711月赛]图的价值

    不难想到考虑每个点的贡献,ans=n*sigema(1~n)i C(n-1,i)*(2^C(n-1,2))*i^k 直接套第二类斯特林拆柿子即可.提示:sigema(1~n)i C(n,i)*C(i, ...

  4. Spring 4.3 的新功能和增强

    转载自https://my.oschina.net/waylau/blog/698186 核心容器改进 核心容器额外提供了更丰富的元数据来改进编程. 默认 Java 8 的方法检测为 bean 属性的 ...

  5. 蓝天白云大草原风景PSD背景素材

    蓝天白云大草原风景PSD源文件背景素材,蓝天白云,大草原,风景,背景素材,自然风景,草原景色,绿色清新背景 地址:http://www.huiyi8.com/psd/

  6. python3 - 写一个生成双色球号码的一个程序,生成的号码写到文件里面

    写一个生成双色球号码的一个程序,生成的号码写到文件里面 # 中奖号码由6个红色球号码和1个蓝色球号码组成 # 篮球范围:01-16 # 红球范围:01-33 def swq(num): random. ...

  7. 【boost】ptree 读写中文的问题

    最经项目中使用到了boost property_tree,却在中文问题上遇到大问题. 直接使用ptree读写存储于窄字符(如string)类型的中文字符串时,程序可以运行,但由于XML默认使用UTF- ...

  8. javascript基础知识整理(不定时更新)

    1.js中真与假的定义: 真:true,非零数字,非空字符串,非空对象 假:false,数字零,空字符串,空对象(null),undefined 2.使用for循环对json进行循环操作 for(va ...

  9. sed根据关键字注释crontab的计划任务

    [root@linux06 ~]# crontab -e*/5 * * * * /root/time_test.sh ----------------------------------------- ...

  10. Gym:101630J - Journey from Petersburg to Moscow(最短路)

    题意:求1到N的最短路,最短路的定义为路径上最大的K条边. 思路:对于每种边权,假设为X,它是第K大,那么小于X的变为0,大于K的,边权-X.然后求最短路,用dis[N]+K*X更新答案. 而小于K的 ...