这篇文章主要介绍“如何编写两数之和的算法代码”,在日常操作中,相信很多人在如何编写两数之和的算法代码问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何编写两数之和的算法代码”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。
示例:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
package com.lau.javabase;
import org.junit.Test;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* 时间复杂度排序:
* O(1)——常数时间
* O(logN)——对数时间
* O(N)——线性时间
* O(N*N)——2次指数时间
* O(N*N*N)——3次指数时间
*
*给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。
*
* 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
*
* 你可以按任意顺序返回答案。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/two-sum
*
* 测试用例:
* 输入:nums = [2,7,11,15], target = 9
* 输出:[0,1]
* 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/two-sum
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
*/
public class TwoSumTest {
@Test
public void test(){
int[] array = {1,5,10,9,12};
Instant start = Instant.now();
System.out.println("---------1--------");
int[]resArray = findTwoIntBySum(array, 17);
Instant end = Instant.now();
long timeElapsed = Duration.between(start, end).toNanos(); // 单位为毫秒
System.out.println("程序1耗时:" + timeElapsed);
Arrays.stream(resArray).forEach(System.out :: println);
start = Instant.now();
System.out.println("---------2--------");
int[]resArray2 = findTwoIntBySum2(array, 17);
end = Instant.now();
timeElapsed = Duration.between(start, end).toNanos(); // 单位为毫秒
System.out.println("程序2耗时:" + timeElapsed);
Arrays.stream(resArray2).forEach(System.out :: println);
start = Instant.now();
System.out.println("---------3--------");
int[]resArray3 = findTwoIntBySum3(array, 17);
end = Instant.now();
timeElapsed = Duration.between(start, end).toNanos(); // 单位为毫秒
System.out.println("程序3耗时:" + timeElapsed);
Arrays.stream(resArray3).forEach(System.out :: println);
start = Instant.now();
System.out.println("---------4--------");
int[]resArray4 = findTwoIntBySum4(array, 17);
end = Instant.now();
timeElapsed = Duration.between(start, end).toNanos(); // 单位为毫秒
System.out.println("程序4耗时:" + timeElapsed);
Arrays.stream(resArray4).forEach(System.out :: println);
}
/**
* @Description:解法一,两层遍历
* @param array
* @param target
* @return
*/
private int[] findTwoIntBySum(int[] array, int target){
int[] resArray = null;
for(int i = 0; i < array.length - 1; i++){
for(int j = i + 1; j < array.length; j++){
if(target == array[i] + array[j]){
resArray = new int[]{i, j};
return resArray;
}
}
}
return resArray;
}
/**
* @Description:解法二,依托HashMap,将数组值作为K,索引作为V存入Map
* @param array
* @param target
* @return
*/
private int[] findTwoIntBySum2(int[] array, int target){
int[] resArray = null;
//建立HashMap,存储<k,v> K存值,V存索引
Map<Integer, Integer> midMap = new HashMap<>();
// Arrays.stream(array).forEach(s -> );
for(int i = 0; i < array.length; i++){
midMap.put(array[i], i);
}
for(int i = 0; i < array.length; i++){
if(Objects.nonNull(midMap.get(target - array[i]))){
resArray = new int[]{i, midMap.get(target - array[i])};
return resArray;
}
}
return resArray;
}
/**
* @Description:解法三,两层遍历(解法一的变种)
* @param array
* @param target
* @return
*/
private int[] findTwoIntBySum3(int[] array, int target){
int[] resArray = null;
for(int i = 0; i < array.length - 1; i++){
for(int j = i + 1; j < array.length; j++){
if(target - array[i] == array[j]){
resArray = new int[]{i, j};
return resArray;
}
}
}
return resArray;
}
/**
* @Description:解法四,依托HashMap,一次遍历即可完成
* @param array
* @param target
* @return
*/
private int[] findTwoIntBySum4(int[] array, int target){
int[] resArray = null;
//建立HashMap,存储<k,v> K存值,V存索引
Map<Integer, Integer> midMap = new HashMap<>();
// Arrays.stream(array).forEach(s -> );
for(int i = 0; i < array.length; i++){
if(midMap.containsKey(target - array[i])){
resArray = new int[]{midMap.get(target - array[i]), i};
return resArray;
}
midMap.put(array[i], i);
}
return resArray;
}
}
到此,关于“如何编写两数之和的算法代码”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/Howard2016/blog/5008291