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. Android(java)学习笔记142:使用Sqlite基本流程

  2. file is missing from source location:c\temp\batang.ttc

    打开注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink 在右侧找到 Micr ...

  3. Adapter 适配器模式

    将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 目标接口(Target):客户所期待的接口.目标可以是具体的或抽象的类,也可 ...

  4. Wireshark - 过滤规则

    使用 Wireshark 的默认设置抓包时,会得到大量的冗余信息,以至于很难找到自己所需的封包.使用过滤器可以帮助我们在庞杂的结果中快速地找到我们所需的封包.过滤器分为两种:捕捉过滤器和显示过滤器. ...

  5. web开发第一周

    第一天:HTML基础内容. 超文本标记语言,Hyper Text Makeup Language. 列表(清单),表格,框架,和表单,四个方法还不是很熟练. 列表,list,分OL和UL,表格的每个单 ...

  6. 解构控制反转(IoC)和依赖注入(DI)

    1.控制反转 控制反转(Inversion of Control,IoC),简言之就是代码的控制器交由系统控制,而不是在代码内部,通过IoC,消除组件或者模块间的直接依赖,使得软件系统的开发更具柔性和 ...

  7. 那天有个小孩跟我说LINQ(六)转载

    2  LINQ TO SQL完结(代码下载)      我们还是接着上次那个简单的销售的业务数据库为例子,打开上次那个例子linq_Ch5 2.1 当数据库中的表建立了主外键 ①根据主键获取子表信息 ...

  8. win向linux传文件

    使用pscp.exe即可. 下载pscp.exe(http://pan.baidu.com/s/1jG6zmx4) 复制到windows/system32目录下即可. 然后可在cmd命令行下使用psc ...

  9. 使用反射让Spinner选择同一选项时触发onItemSelected事件

    翻看源码,Spinner判断是否触发onItemSelected,是在它的基类AdapterView里面做的: void checkSelectionChanged() { if ((mSelecte ...

  10. TCP调试助手

    网络开发经常要用到一些TCP&UDP的调试工具,搜集一些备用. 目前总结工具有(不分先后): chrome等自带调试器调试HTTP Fiddler(.NET)和Charles debugger ...