Two Sum (c#)
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
给定一个整数数组,要求找到数组中的2个值,使他们的和等于给定值,并返回索引位置组成的数组(以1开始的索引)
我的解法:两层循环,从数组的第一个数开始和第j个数做比较,和目标值相同则返回,时间复杂度O(n2)
public int[] TwoSum(int[] nums, int target)
{
for (int i = ; i < nums.Length; i++)
{
for (int j = i+; j < nums.Length; j++)
{
if (nums[i]+nums[j] == target)
{
return new int[]{i+,j+};
}
}
}
return null;
}
优秀解法:一层循环,运用哈希表去重,数组值为key,索引为value。并使用哈希表的containsKey方法和目标值做比较,少去一次循环。
还有一个小细节,就是先去判断哈希表中有无符合条件的值,再去往里面插入,那么可以少去一次插入的步骤。
public int[] TwoSum2(int[] nums, int target)
{
Hashtable hsNums = new Hashtable();
for (int i = ; i < nums.Length; i++)
{
if (hsNums.ContainsKey(target - nums[i]))
{
return new int[] { (int)hsNums[target - nums[i]],i + };
}
if (!hsNums.ContainsKey(nums[i]))
hsNums.Add(nums[i], i + );
}
return null;
}
Two Sum (c#)的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- JS中判断 !="" 或者 !=null 失效
var id; //... if (!id&& typeof (id) != "undefined" && id!= 0) { //id为null ...
- spring cloud资料
https://segmentfault.com/a/1190000006216281 http://git.oschina.net/zhou666/spring-cloud-7simple zuul ...
- Keil软件常见的警告和错误含义。——Arvin
1. warning: #767-D: conversion from pointer to smaller integer 解释:将指针转换为较小的整数 影响:可能造成的影响:容易引起数据截断,造 ...
- C++输入cout与输出cin
输入和输出并不是C++语言中的正式组成成分.C和C++本身都没有为输入和输出提供专门的语句结构.输入输出不是由C++本身定义的,而是在编译系统提供的I/O库中定义的.C++的输出和输入是用" ...
- ActiveReports 11 新特性速递
又到了一年一度,翘首期盼的ActiveReports11 即将发布,ActiveReports 10 表控件横空出世,成为中国式复杂报表的救星后,ActiveReports11 又会有哪些令人惊奇的新 ...
- css学习笔记 10
一个竖向导航,假设ul宽度为100px,li不浮动,在各浏览器下都会正常显示,当li左浮动时,在标准浏览器下,li会横向排列,如果第一行的剩余空间的宽度不够下一个li,下一个li自动换到第二行,第二行 ...
- eclipse中将项目发布到tomcat的root目录
在eclipse中,将项目直接部署在tomcat的root目录中,这样便可以直接ip:port访问项目: 项目右键->属性->web project settings
- 收集C#常用类:自己写的一个DBHelper类
随着学的东西越来越多,一点点的完善吧! using System; using System.Collections.Generic; using System.Linq; using System. ...
- 推荐两个谷歌的json-view插件(附带下载分享地址)
1.JSONView 网盘下载地址:http://pan.baidu.com/s/1hrGlaVa 效果图: 2.JSON-handle 网盘下载地址:http://pan.baidu.com/s/1 ...
- Zotero 使用指南
DownLoad Page: https://onedrive.live.com/redir?resid=5084666E7B16AA85!109&authkey=!ABHQp7yfMnLpE ...