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
思路:1、将数组放入hash表,遍历数组,在hash表中查找另一个数。hash查找速度较快
      2、利用快排先将数组排序,双向遍历,找到两数之和为target的两个数m,n ,再遍历原数组,找到m,n的下标
----------转载---------
<pre name="code" class="java">public class Solution {
public int[] twoSum(int[] numbers, int target) {
int []result = new int[2];
java.util.HashMap<Integer, Integer> table = new java.util.HashMap<Integer, Integer>(200000);
for (int i = 0; i < numbers.length; i++) {
table.put(numbers[i], i+1);
}
for (int i = 0; i < numbers.length; i++) {
if(table.get(target-numbers[i])!=null&&table.get(target-numbers[i])!=i+1){
result[0]=i+1;
result[1]=table.get(target-numbers[i]);
break;
}
}
return result;
}
}
</pre><pre name="code" class="java">---------my coding    time limit exceeded ---------

public class Solution1 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
int[] num={2,3,4,5,6,8,9};
Solution1 s=new Solution1();
int[] indexs= s.twoSum(num, 10); System.out.println(indexs[0]+" "+indexs[1]);
} public int[] twoSum(int[] numbers, int target) {
int length=numbers.length;
int[] sortNum=numbers;
sortNum=quickSort(sortNum,0,length-1); int[] index=new int[2];
int split1=0,split2=0,index1=0,index2=0;
for(int i=0;i<length-1;i++){
if(2*sortNum[i]<=target && 2*sortNum[i+1]>target){
split1=i;
}
if(sortNum[i]>target){
split2=i;
break;
}
}
if(split2==0){
split2=length;
} index1=0;index2=split1;
while(index1<split1 && index2<split2 ){ if(sortNum[index1]+sortNum[index2] <target){
index1++;
if(index1==split1){
index1=0;
index2++;
} }else if(sortNum[index1]+sortNum[index2] >target){
index1=0;
index2++;
}else{
index[0]=sortNum[index1];
index[1]=sortNum[index2];
break;
}
} if(index[0]!=0 && index[1]!=0){
for(int i=0;i<numbers.length;i++){
if(numbers[i]==index[0])
index[0]=i+1;
if(numbers[i]==index[1])
index[1]=i+1;
}
if(index[0]>index[1]){
int temp=index[0];
index[0]=index[1];
index[1]=temp;
}
}
return index; } int[] quickSort(int[] num,int left,int right){ if(left<right){
int i=left,j=right,x=num[left];
while(i<j){
while(i<j && num[j]>=x)
j--;
if(i<j)
num[i++]=num[j]; while(i<j && num[i]<x)
i++;
if(i<j)
num[j--]=num[i];
}
num[i]=x; quickSort(num,left,i-1);
quickSort(num,i+1,right);
} return num;
} }

</pre><pre name="code" class="java">
												

leetcode 第一题 Two Num java的更多相关文章

  1. LeetCode算法题-Heaters(Java实现)

    这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...

  2. LeetCode算法题-Sqrt(Java实现)

    这是悦乐书的第158次更新,第160篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第17题(顺位题号是69). 计算并返回x的平方根,其中x保证为非负整数. 由于返回类型 ...

  3. leetcode第一题(easy)

    第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  4. 有人相爱,有人夜里开车看海,有人leetcode第一题都做不出来。

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

  5. LeetCode第一题:Two Sum

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

  6. leetcode第一题--two sum

    Problem:Given an array of integers, find two numbers such that they add up to a specific target numb ...

  7. leetcode第一题两数之和击败了 98.11% 的用户的答案(C++)

    虽然题目简单,但我这好不容易优化到前2%,感觉也值得分享给大家(方法比较偷机) 题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们 ...

  8. LeetCode第一题以及时间复杂度的计算

    问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: I ...

  9. LeetCode第一题—— Two Sum(寻找两数,要求和为target)

    题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...

随机推荐

  1. Qt全局热键(windows篇)

      Qt对于系统底层,一直没有很好的支持,例如串口并口通信,还有我们经常都会用到的全局热键,等等.既然Qt可能出于某种原因,不对这些进行支持,我们就只能自己写代码,调用系统相关的API了. 注意,这个 ...

  2. cocos2dx下最大纹理大小取决于平台

    原文:http://www.cocos2d-x.org/wiki/Max_size_of_textures_in_cocos2d-x_depends_on_each_platform 在理论上,coc ...

  3. 动作之CCActionInstant(立即动作)家族

    立即动作就是不需要时间,马上就完成的动作.立即动作的共同基类是CCActionInstant.CCActionInstant的常用子类有: CCCallFunc:回调函数包装器 CCFlipX:X轴翻 ...

  4. Big Clock

    Problem Description Our vicar raised money to have the church clock repaired for several weeks. The ...

  5. JVM笔记3:Java垃圾收集算法与垃圾收集器

    当前商业虚拟机的垃圾收集都采用"分代收集"算法,即根据对象生命周期的不同,将内存划分几块,一般为新生代和老年代,不同的代根据其特点使用最合适的垃圾收集算法 一,标记-清除算法: 该 ...

  6. VS2010常用插件介绍之Javascript插件(一)

    引自:http://blog.csdn.net/cyxlzzs/article/details/6583577 今天在写JS时,写到500多行时,感觉代码已经很难看了.想到C#代码都有折叠功能,是不是 ...

  7. MVC小系列(十七)【自定义验证规则给下拉框】

    因为下拉框不支持验证,所以写一个attribute特性,让它继承ValidationAttributemvc的特性验证,很直接,无论是数据安全特性上还是页面表现上都不错,它的运行机制: 前台表单验证规 ...

  8. [转]CSS 模块

    CSS 模块 如果你想知道 CSS 最近发展的转折点,你应该选择去观看 Christopher Chedeau 在2014年11月的 NationJS 大会上做的名称为 CSS in JS 的分享.不 ...

  9. 线段树(单点更新)HDU1166、HDU1742

    在上一篇博文里面,我提到了我不会线段树,现在就努力地学习啊! 今天AC一题感觉都很累,可能是状态不佳,在做HDU1166这题目时候,RE了无数次. 原因是:我的宏定义写错了,我已经不是第一犯这种错误了 ...

  10. 无刷新删除 Ajax,JQuery

    1.数据库用上面的,增加一个 DeleteById 的SQL方法 delete from T_Posts where Id = @Original_Id 2.设置处理页面 delete.ashx pu ...