题目描述:

  给一个整数数组,找到两个数使得他们的和等于一个给定的数target。

  你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是1到n,不是以0开头。

注意事项

  你可以假设只有一组答案。

样例

  numbers=[2, 7, 11, 15],  target=

  return [1, 2]

 public class Solution {
/*
* @param numbers : An array of Integer
* @param target : target = numbers[index1] + numbers[index2]
* @return : [index1 + 1, index2 + 1] (index1 < index2)
*/
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
for(int i=0;i<numbers.length;i++){
for(int j=i+1;j<numbers.length;j++){
if(numbers[i]+numbers[j]==target){
res[0] = i+1;
res[1] = j+1;
return res;
}
}
}
return res;
}
}

LintCode-两数之和的更多相关文章

  1. 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

    题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...

  2. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  3. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  4. LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  5. [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  6. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  7. Leetcode(一)两数之和

    1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...

  8. 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数

    问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...

  9. 两数之和,两数相加(leetcode)

    我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...

  10. leetcode刷题--两数之和(简单)

    一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...

随机推荐

  1. jvm的内存管理【转】

    [转]JVM内存管理 这些日子一直在研究jvm内存管理的东西,网上的知识很多,总结一下,能沉淀下来的就是自己的! 首先,刚学java的时候就知道java类文件是以 .java为后缀的文件,经过java ...

  2. CentOS 安装Node.js

    先安装gcc-c++编译环境和openssl yum install gcc-c++ openssl-devel 然后 下载包并解压 cd /usr/local/src wget http://nod ...

  3. 注意:只有xcode5.1创建的项目会自动适配iphone6,iphone6p

    注意:只有xcode5.1创建的项目会自动适配iphone6,iphone6p

  4. VS2013 快捷键乱掉如何修改回来

    比如 CTRL+E+C =注释 F6=重新生成解决方案 CTRL+D+Q=运行时快速监视 工具-->选项-->环境-->键盘-->应用以下其他键盘映射方案,下拉选择 Visua ...

  5. linux install nginx error

    1 2 3 4 5 6 7 8 9 10 11 [mahao01@127.0.0.1 nginx-1.2.9]$ make make -f objs/Makefile make[1]: Enterin ...

  6. 整理部分JS 控件 WEB前端常用的做成Jsp项目,方便今后直接用

    整理部分JS 控件  WEB前端常用的做成Jsp项目,方便今后直接用 最近又没时间了,等用时间了,再加入更多的, 源码下载: http://download.csdn.net/detail/liang ...

  7. Lavarel(-) windows 部署

    使用 Lavarel 开发完全可以抛开wamp,phpstudy等一键环境.因为lavarel 内置的artisan 整合了php5.4 内置的webserver ,甚至可以使用artisan 命令指 ...

  8. Ubuntu14下LAMP环境的安装以及yaf扩展的安装

    前段时间在ubuntu下安装了lamp环境,记录一下安装过程方便以后查阅. 安装lamp环境 ① 安装apache sudo apt-get install apache2 系统会弹出如图所示的提示, ...

  9. The method of using code coverage tool

    Please look at the following blog: http://blog.csdn.net/superqa/article/details/9060521 Use  ReportG ...

  10. m文件转换为C/C++文件的编译、绘图、参数、打包问题总结

    在工程计算相关项目中,常常利用Matlab来完成计算.算法.绘图等功能.使用Matlab来完成这些功能非常简单,Matlab提供的m编程语言功能强大,代码量少.为了在自己的C/C++项目中加入这些功能 ...