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. 九度OJ 1035:找出直系亲属 (二叉树、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2380 解决:934 题目描述:     如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外) ...

  2. Netty Bootstrap(图解)|秒懂

    目录 Netty Bootstrap(图解) 源码工程 写在前面 图解几个重要概念 父子 channel EventLoop 线程与线程组 通道与Reactor线程组 Channel 通道的类型 启动 ...

  3. QQ登录集成到自己网站php代码(转载)

    我们现在在各大网站论坛都可以看到点击一个QQ图标就可以利用自己的QQ号在网站进行登录了,下面我来告诉你一段QQ登录集成到自己网站php代码,有需要的朋友可参考. 1.打开open.qq.com 添加创 ...

  4. C# nunit 单元测试

    1. 引包 nunit.framework.dll

  5. mooc_java 集合框架上 学生所选课程

    用一个集合Set存放学生所选课程接口不能实例化,所以用其实现类进行实例化 set接口不能实例化,通过HashSet这个实现类来进行实例化,需要导入包this.courses=new HashSet() ...

  6. Java并发知识概述

    1.Java内存模型的抽象结构 Java中,所有的实例.静态域和数组元素都存储在堆内存中,堆内存是线程共享的.局部变量,形参,异常处理参数不会在线程之间共享,所以不存在内存可见性问题,也就不受内存模型 ...

  7. Hive- Hive安装

    Hive安装 1.1下载Hive安装包 官网:http://hive.apache.org/downloads.html 个人建议到这里下载:http://apache.forsale.plus/ 1 ...

  8. 利用jsp技术实现用户注册

    利用jsp技术实现用户注册,包含register.html和register_check.jsp页面代码​1. [代码]J2EE实验    <!DOCTYPE html PUBLIC " ...

  9. 一步完成MySQL向Redis迁移

    在把一个大表从 MySQL 迁移到 Redis 时,你可能会发现,每次提取.转换.导入一条数据是让人难以忍受的慢!这里有一个技巧,你可以通过使用管道把 MySQL 的输出直接输入到 redis-cli ...

  10. 解决 ssh 链接慢问题

    sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_configsed -i '/#UseDNS ye ...