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

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

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

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

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

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. 1010. Radix (25)(未完成)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  2. django例子,question_text为中文时候报错

    问题描述 UnicodeEncodeError at /admin/polls/question/3/ 'ascii' codec can't encode characters in positio ...

  3. 月薪3万的程序员告诉你:这样工作才能拿高薪(转 IT之家)

    习惯即刻回报 他不懂得只有春天播种,秋天才会有收获.刚刚付出一点点,甚至还没有付出,就想要得到回报.技术刚刚掌握,能一边百度一边干活了就觉得该拿到多少多少钱了.找工作先想着多少多少钱,入职了没干几个月 ...

  4. java面向对象设计原则

    原则1:DRY(Don't repeat yourself) 即不要写重复的代码,而是用"abstraction"类来抽象公有的东西.如果你需要多次用到一个硬编码值,那么可以设为公 ...

  5. 插件dTree的使用

    解压缩dtree.zip 包.  dtree目录下包括这些文件:example01.html . dtree.js . api.html . dtree.css 和img目录       注意:除了a ...

  6. 慕课网__css_padding && z_index

    一个正方形 对于“内联元素”来说 z-index 静态布局没有z-index作用

  7. Goppa code

    上面的公式定义了长度为n的Goppa码[1].n=2^m, 其维度 k≥n- t·m. 最小距离d≥ 2t+1. 存在运行时间与 n·t 成正比的快速译码算法. 从形式上看,右边是分式,相当于线性分组 ...

  8. weex 小结--内建模块

    使用以下模块时,需要导入依赖:var *** = require('@weex-module/****'); 1. navigator --像浏览器一样切换页面 2. webview(module) ...

  9. git pull 然后 ahead of origin/master * commit 消失

    本来显示 your branch is ahead origin/master * commit后来也许在master merge 这个分支后, 然后git pull, 就显示Your branch ...

  10. SSH框架总结

    首先,SSH是由多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框架的系统从职责 ...