leetcode 第一题 Two Num java
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的更多相关文章
- LeetCode算法题-Heaters(Java实现)
这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...
- LeetCode算法题-Sqrt(Java实现)
这是悦乐书的第158次更新,第160篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第17题(顺位题号是69). 计算并返回x的平方根,其中x保证为非负整数. 由于返回类型 ...
- leetcode第一题(easy)
第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- 有人相爱,有人夜里开车看海,有人leetcode第一题都做不出来。
第一题 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数 ...
- LeetCode第一题:Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode第一题--two sum
Problem:Given an array of integers, find two numbers such that they add up to a specific target numb ...
- leetcode第一题两数之和击败了 98.11% 的用户的答案(C++)
虽然题目简单,但我这好不容易优化到前2%,感觉也值得分享给大家(方法比较偷机) 题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们 ...
- LeetCode第一题以及时间复杂度的计算
问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: I ...
- LeetCode第一题—— Two Sum(寻找两数,要求和为target)
题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...
随机推荐
- string的操作
除了顺序容器共有的操作之外,string类型还提供了一些额外的操作.这些操作中的大部分要么是提供string类和C风格字符数组之间的相互转换,要么是增加了允许我们用下标代替迭代器的版本. 构造stri ...
- qsettings 中文键值 注释 支持
#ifndef SETTINGS_H #define SETTINGS_H #include <QString> #include <QVariant> class QSett ...
- 这种写法用过没:string.Format("{0,-10}", 8)
1 2 3 4 var s1 = string.Format("{0,-10}", 8); var s2 = string.Format("{0,10}", 8 ...
- iOS 跳转至 指定 StoryBoard 中 指定的 Scene
UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"second" bundle:nil]; UIViewCont ...
- HTML文本格式化
文本格式化标签: 标签 描述 <b> 定义粗体文本. <big> 定义大号字. <em> 定义着重文字. <i> 定义斜体字. <small> ...
- (转)Ilist 和list的区别归纳总结
常见问题: Ilist <> 本身只是一个泛型接口, 既然是接口当然不能实例化,只能用如下方法 IList <Class1> IList11 =new List <Cla ...
- web开发学习之旅---css第一天
一.css全称 Cascade Style Sheet层叠样式表 二.css引入方式 行内样式:<h2 style="color:#0F0">Hello World&l ...
- angularJS--神奇的$scope
我们在使用angularJS时,$scope对于angularJS是非常重要的,它是angularJS的基础,但$scope到底是什么呢?下面进行一些介绍. 1.$scope是一个普通的js对象 2. ...
- Java的云打印Lodop
打印某一个网页上的内容我们都经常遇到过,比如网上申请港澳通行证时需要填写申请表,然后把申请表给打印出来.像这样的打印技术是怎么实现?这种打印可以通过一种叫云打印的插件来做,按我的理解云打印的技 ...
- Source Insight及常用插件
Source Insight及常用插件 1.Source Insight 2.插件 <1>.使用快捷键注释,单行注释,多行注释,#if 0注释 <2>.跳转到当前文件所在的文件 ...