Given an array of integers, return indices of the two numbers such that they add up to a specific target.

给定一个integer类型的数组,让你返回数组中两个元素相加起来等于给定的值得元素的索引数组

You may assume that each input would have exactly one solution.

你可以假设每个给定值只有一个解决方案,那这里我们就不必考虑多个解决方案了,找到一个解决方案后直接返回结果。

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.

返回数组的索引已置为0

以下是我的解决方案:

int[] array = new int[];
for (int i = ; i < nums.Length - ; i++)
{
int num1 = nums[i];
int j = i + ; while(j < nums.Length) {
int num2 = nums[j];
int result = num1 + num2; if (result.CompareTo(target) == )
{
array[] = i;
array[] = j;
i = j = nums.Length + ;
} j++;
}
} return array;
 

[Letcode] 1. 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 ...

随机推荐

  1. 百度富文本编辑器UEditor安装配置全过程

    网站开发时富文本编辑器是必不可少的,他可以让用户自行编辑内容的样式然后上传到后台!下面我们来介绍如何安装使用百度富文本编辑器 一.下载并且设置百度富文本编辑器的样式     你可以去百度UEditor ...

  2. JS判断手机浏览器

    <script type="text/javascript"> /* * 智能机浏览器版本信息: * */ varbrowser={ versions:function ...

  3. 关于Rational Functional Tester (RFT)的简单介绍

    前段时间给客户做了个RFT的简单培训,以下.因为涉及到公司的框架,所以中间省去了很多框架里的细节,只留了一个框架的总体结构的概览. RFT IBM Rational Functional Tester ...

  4. MSP430F149学习之路——时钟2

    代码一: /************************** 功能:LED每隔1秒闪烁一次 ****************************/ #include <msp430x14 ...

  5. SQL Access Advisor

    1.概述: provides advice on improving the performance of a database through partitioning, materialized ...

  6. 华为OJ平台——整数的二进制中1的个数

    题目描述: 输入一个整数,求该整数的二进制表达中有多少个1.例如输入10,由于其二进制表示为1010,有两个1,因此输出2. 思路: 这是一道很基本的考查位运算的面试题.包括微软在内的很多公司都曾采用 ...

  7. Windows应用替代方案接龙

    使开源软件的优势: 开源安全产品的开发.测试和发布过程完全是透明的,同时提供产品的源代码及部分的文档.通过阅读源代码,大家可以清楚地了解开源安全技术的工作原理和实现方法,在选择开源安全技术时更有把握, ...

  8. VBS创建数据库

    '创建数据库'参数:strDBPath 字符串型 数据库文件的完整路径Sub CreateDataBase(strDBPath)Dim catObjSet catObj = CreateObject( ...

  9. Handler 取不到session 解决办法

      Handler需要继承 Handler : IHttpHandler, IReadOnlySessionState, IRequiresSessionState

  10. Oracle 通过触发器 来创建 同步临时表 及处理 通过 自治事务 来解决 查询 基表的问题

    // 触发器 create or replace trigger tr_sync_BD_MARBASCLASS after INSERT or UPDATE on BD_MARBASCLASS for ...