首页 > 编程语言 > 详细

数组元素内两元素相加等于目标值,返回数组下标

时间:2020-03-29 17:09:58      阅读:76      评论:0      收藏:0      [点我收藏+]
来自 作者reed分享
import java.util.Arrays;
import java.util.HashMap;

/**
* @auto dh
* @create 2020-03-29-12:40
*/
public class Test019 {
public static int[] testArith() {
int[] arr = {12, 14, 18, 20};
HashMap<Integer, Integer> hashMap = new HashMap<>();
int target = 32;

for (int i = 0; i < arr.length; i++) {
int value = target - arr[i];
if (hashMap.containsKey(value)) {
return new int[]{hashMap.get(value), i};
}
hashMap.put(arr[i], i);
}

return null;

}

public static void main(String[] args) {
System.out.println(Arrays.toString(testArith()));
}
}

数组元素内两元素相加等于目标值,返回数组下标

原文:https://www.cnblogs.com/kukai/p/12592778.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!