C#LeetCode刷题之#1-两数之和(Two Sum)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3762 访问。
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
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.
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3762 访问。
public class Program {
public static void Main(string[] args) {
int[] nums = { 2, 7, 11, 15 };
var res = TwoSum(nums, 9);
var res2 = TwoSum2(nums, 9);
Console.WriteLine($"[{res[0]},{res[1]}]");
Console.WriteLine($"[{res2[0]},{res2[1]}]");
Console.ReadKey();
}
public static int[] TwoSum(int[] nums, int target) {
//类似于冒泡,双循环比较2个数的和与目标值是否相等
for (int i = 0; i < nums.Length; i++) {
for (int j = i + 1; j < nums.Length; j++) {
if (nums[i] + nums[j] == target) {
return new int[] { i, j };
}
}
}
//找不到时抛出异常
throw new ArgumentException();
}
public static int[] TwoSum2(int[] nums, int target) {
//用数组中的值做key,索引做value存下所有值
var dictionary = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++) {
//记录差值
int complement = target - nums[i];
//若字典中已经存在这个值,说明匹配成功
if (dictionary.ContainsKey(complement)) {
return new int[] { dictionary[complement], i };
}
//记录索引
dictionary[nums[i]] = i;
}
//找不到时抛出异常
throw new ArgumentException();
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3762 访问。
[0,1]
[0,1]
分析:
显而易见,TwoSum 的时间复杂度为: ,TwoSum2 的时间复杂度为:
。
C#LeetCode刷题之#1-两数之和(Two Sum)的更多相关文章
- leetcode刷题笔记-1. 两数之和(java实现)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- (1)leetcode刷题Python笔记——两数之和
题目如下: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...
- C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...
- #leetcode刷题之路15-三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- #leetcode刷题之路1-两数之和
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...
- leetcode刷题第二天<两数相加>
题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...
- leetcode刷题第一日<两数和问题>
开始就用到了c++的哈希表是真的恶心,首先学习一波基础知识 https://blog.csdn.net/u010025211/article/details/46653519 下面放下大佬的代码 cl ...
- #leetcode刷题之路18-四数之和
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
- LeetCode(1): 两数之和
本内容为LeetCode第一道题目:两数之和 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 19:57:18 201 ...
随机推荐
- Ethical Hacking - Web Penetration Testing(10)
SQL INJECTION SQLMAP Tool designed to exploit SQL injections. Works with many DB types, MySQL, MSSQL ...
- Windows File Recovery - 微软官方文件恢复工具
假如你不小心误删除了文件或因各种意外情况丢失数据后,你可以通过 微软这款工具 这个工具来尝试恢复它们.WinFR 工具支持读取本机硬盘.移动硬盘.U 盘,或者连接相机.手机.使用读卡器来恢复 SD.T ...
- 搭建高可用kubernetes集群(keepalived+haproxy)
序 由于单master节点的kubernetes集群,存在master节点异常之后无法继续使用的缺陷.本文参考网管流程搭建一套多master节点负载均衡的kubernetes集群.官网给出了两种拓扑结 ...
- 解决nginx在Linux中已经正常启动,Windows端的浏览器却无法访问的问题
一:查看Linux中nginx已经正常启动 二:查看80端口,未被占用 三:检查防火墙的问题 关闭防火墙:chkconfig iptables off //失败 暂时关闭防火墙:service ipt ...
- 检查string是否有重复尝试用map
链接: 题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本的另外一个单词.在判断是否满足条件时,字母不分大小写,但在输出时应保留输入的大小写,按字典序排列. 题解:先 ...
- 【Python】Async异步等待简单例子理解
import time def run(coroutine): try: print("") coroutine.send(None) except StopIteration a ...
- scp的使用以及cp的对比
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...
- MacOS下Nginx安装
1. 先安装homebrew 2. 安装Nginx,终端下执行: $ brew install nginx 安装过程中会自己安装依赖: 3. 启动nginx服务 $ nginx 成功后,使用浏览器打开 ...
- 社交网站的数据挖掘与分析pdf版本|网盘下载地址附提取码|
点击此处进入网盘下载地址 提取码:btqx 作者介绍: 马修·罗塞尔(MatthewA.Russell),DigitalReasoningSystems公司的技术副总裁和Zaffra公司的负责人,是热 ...
- PHP date_add() 函数
------------恢复内容开始------------ 实例 添加 40 天到 2013 年 3 月 15 日: <?php$date=date_create("2013-03- ...