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.

public class Solution {
public int[] TwoSum(int[] nums, int target) {
int[] sumnumbers =new int[]; for (int i = ; i <= nums.Length-; i++)
{
for (int j = ; j <= nums.Length-; j++)
{
if (nums[i] + nums[j] - target == && i != j)
{
if (i < j)
{
sumnumbers[] = i;
sumnumbers[] = j;
return sumnumbers;
}
else
{
sumnumbers[] =j;
sumnumbers[] = i;
return sumnumbers;
}
} }
}
return sumnumbers;
}
}

Top sulution 【O(n)】 C++的top解决方案

vector<int> twoSum(vector<int> &numbers, int target)
{
//Key is the number and value is its index in the vector.
unordered_map<int, int> hash;
vector<int> result;
for (int i = ; i < numbers.size(); i++) {
int numberToFind = target - numbers[i]; //if numberToFind is found in map, return them
if (hash.find(numberToFind) != hash.end()) {
//+1 because indices are NOT zero based
result.push_back(hash[numberToFind] + );
result.push_back(i + );
return result;
} //number was not found. Put it in the map.
hash[numbers[i]] = i;
}
return result;
}

C#的仿写

public class Solution {
public int[] TwoSum(int[] nums, int target) {
int[] RetIndecis = {,};
Hashtable hsNums = new Hashtable();
hsNums.Clear();
//i is the latter index
for(int i=;i<nums.Length;i++)
{
int targetKey = target - nums[i];
//if targetKey exist
if(hsNums.ContainsKey(targetKey))
{
RetIndecis[] = i + ;
RetIndecis[] = (int)hsNums[targetKey];
return RetIndecis;
}
//key is the number and value is the index,filter the number which has existed
if(!hsNums.ContainsKey(nums[i]))
hsNums.Add(nums[i],i + );
}
return(RetIndecis);
}
}

LeedCode-Two Sum的更多相关文章

  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. 安装redis以windows服务形式

    安装redis以windows服务形式 安装redis以windows服务形式 redis windows windows 服务 以前跑redis,老是要开一个命令行窗口,一旦关闭,redis服务就挂 ...

  2. [Erlang 0128] Term sharing in Erlang/OTP 下篇

    继续昨天的话题,昨天提到io:format对数据共享的间接影响,如果是下面两种情况恐怕更容易成为"坑", 呃,恰好我都遇到过; 如果是测试代码是下面这样,得到的结果会是怎样?猜! ...

  3. SQL SERVER 数据库各版本功能对比

    以前写了篇SQL SERVER 2008数据库各版本功能对比,官网提供的那个功能确实很好很强大,后面发现那个链接失效了.今天又遇到要对比SQL Server 2014数据库版本功能需求,搜索找了好久才 ...

  4. Oracle连接odbc数据源

    Oracle连接odbc数据源 说明 oracle连接ODBC数据源有两种方式,hsodbc和dg4odbc,简单说dg4odbc是hsodbc的升级.两种连接方法大致一样,现将连接步骤说明如下: 检 ...

  5. (总结)Oracle 11g常用管理命令(用户、表空间、权限)

    1.启动oracle数据库: 从root切换到oracle用户进入:su - oracle 进入sqlplus环境,nolog参数表示不登录:sqlplus /nolog 以管理员模式登录:sqlpl ...

  6. crontab 启动 、运行 和编辑 查看

    cron服务是Linux的内置服务,但它不会开机自动启动.可以用以下命令启动和停止服务: /sbin/service crond start /sbin/service crond stop /sbi ...

  7. Linux 下dns的搭建

    1.软件安装: redhat默认使用bind作为安装软件 使用yum安装 yum install -y bind bind-chroot bind-utils bind提供主程序 bind-chroo ...

  8. 每天5分钟 玩转OpenStack 目录列表

    最近在学习 OpenStack 的相关知识,一直苦于 OpenStack 的体系庞大以及复杂程度,学习没有进度,停滞不前.偶然机会在 51CTO 上发现了一个热点的专题关于 OpenStack 的,题 ...

  9. win 7下建立FTP

    1.安装FTP服务 鼠标桌面右击个性化-卸载程序-打开或关闭windows功能 2.在IIS控制面板里添加FTP站点 下一步 下一步 鼠标右击 下一步 下一步 3.配置ftp站点 4.测试站点是否正常 ...

  10. node 异步回调解决方法之yield

    先看如何使用 使用的npm包为genny,npm 安装genny,使用 node -harmony 文件(-harmony 为使用es6属性启动参数) 启动项目 var genny= require( ...