给定一个数组,求两数之和等于某个值
2020-12-13 03:40
标签:color argument bsp system illegal public int for nbsp 给定一个数组,求两数之和等于某个值 标签:color argument bsp system illegal public int for nbsp 原文地址:https://www.cnblogs.com/wangzxblog/p/11082683.html public static void main(String[] args) {
int[] intArr = {1, 3, 5, 8, 9, 12};
int sum = 10;
int right = intArr.length - 1;
for (int i=0; i intArr.length; ) {
if (right == i) {
throw new IllegalArgumentException("未获取到有效取和值");
}
if (sum == intArr[right] + intArr[i]) {
System.out.println("left index:" + i + ",left num:" + intArr[i] + "; right index:" + right + ",right num:" + intArr[right]);
break;
} else if (sum intArr[i]) {
right--;
} else {
i++;
continue;
}
}
}