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 ...
随机推荐
- 用:hover伪类代替js的hover事件
制作二级菜单要实现鼠标移动上去显示子菜单,鼠标移出子菜单隐藏,或者其他类似需求的地方,首先我会想到用jquery的hover事件来实现,如: $(".nav").hover(fun ...
- lucene 搜索demo
package com.ljq.utils; import java.io.File; import java.util.ArrayList; import java.util.List; impor ...
- java的4种代码块
一.普通代码块 直接在一个方法中出现的{}就称为普通代码块,例子程序如下: public class CodeDemo01{ public static void main(String[] args ...
- Javascript 数组常用操作方法
一.数组 Array 1.创建数组 /* 构造函数 */ var arr1 = new Array(); //创建一个空数组 var arr1 = new Array(5); //创建指定长度数组(数 ...
- Nodejs 之Ajax的一个实例(sql单条件查询&并显示在Browser端界面上)
1.Broswer端的Ajax <!DOCTYPE html> <html> <head lang="en"> <meta charset ...
- php访问远程服务器上的文件
test.php <?php $fp=fopen('http://www.baidu.com', 'r'); while (!feof($fp)) { $chunk=fgets($fp); ec ...
- sqlite字段属性删除方法
Sqlite 不支持直接修改字段的名称. 我们可以使用别的方法来实现修改字段名. 1.修改原表的名称 ALTER TABLE table RENAME TO tableOld; 2.新建修改字段后的表 ...
- LR录制Flex+Web,登录功能之登录密码出错的处理
在LR中录制好更改密码脚本,Controller中使用少量用户进行:单用户多迭代.多用户单迭代.多用户多迭代,运行正常,于是使用490Vuser+2iteration修改980个用户的密码,部分 Vu ...
- mysql 5.7配置文件参数详解
read_buffer_size 默认大小:128KB 最大:2GB 最小:8KB 增量:必须为4KB的整数倍,如果配置的不是整数倍,会向下取整 用途: 1.MyISAM表顺序扫描提供的缓存 2.所有 ...
- ie浏览器兼容问题汇总
对兼容ie浏览器所遇到的问题及总结 互联快谈 2016-10-28 05:51 1,若直接给一个元素设置absolute定位.在浏览器缩放的时候.位置会错位.解决的方法是给外层的元素设置为relati ...