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 ...
随机推荐
- Android主题切换方案总结
所谓的主题切换,就是能够根据不同的设定,呈现不同风格的界面给用户,也就是所谓的换肤. 1.将主题包(图片与配置)存到SD卡上(可通过下载或手动放入指定目录),在代码里强制从本地文件创建图片与配置文字大 ...
- Android 开发第三天
今天学习的内容
- 集合练习——List部分
利用ArrayList 1.存储多个员工信息,包括工号,姓名,年龄,入职时间,逐条打印所有员工姓名,并输出员工个数. package CollectionPart; import java.util. ...
- C# 反射的简单用法
新建两个项目:类库(Model)和控制台应用程序(ReflectTest). 在[Model]中添加一个类[User]: namespace Model { public class User { p ...
- Zepto源码解读
/*******************************************************************************Zepto核心和dom操作******* ...
- c#面向对象编程基础
1. 为什么要有面向对象? (1) 增加代码重用. (2)降低维护负担,将具备独特性质的代码封装起来,修改程序时,相互不会影响. 2.数据封装用来解决全局变量不易维护的问题. 3.多态: ...
- Log4Net 在多层项目中的使用小记
原文地址:http://www.cnblogs.com/zdh8675/p/3645556.html 这几天刚好在调整一个项目,把一些自己不是很清楚的东西先试验一下,这篇文章主要是对我在项目中需要使用 ...
- ubuntu 安装 fcitx
安装fcitx (1)添加ppa源 sudo add-apt-repository ppa:fcitx-team/nightly 或 sudo add-apt-repository ppa:fcitx ...
- 常用JS验证和函数
下面是我常用一些JS验证和函数,有一些验证我直接写到了对象的属性里面了,可以直接通过对象.方法来调用 //浮点数除法运算 function fdiv(a, b, n) { if (n == undef ...
- 伪Base16的构思和实现
最近看见了一个迅雷地址,发现将其转换为普通链接的工具后,发现所谓专用地址地址就是原地址前加一个表示迅雷的前缀,后进行Base64编码.查阅Base64编码过程后,突发奇想:能否做一个Base16算法? ...