public static void main(String[] args) throws UnsupportedEncodingException {
int[] ints = { 1, 5, 10, 11 , 13, 18, 22 ,55 };
int find = 23;
int start = 0;
int end = ints.length - 1;
while (ints[start] >= find) {
System.out.println("都比他大哦!");
return;
}
while (end > start) {
if (ints[start] + ints[end] == find) {
System.out.println(ints[start] + " + " + ints[end] + " = "
+ find);
end--;
start++;
} else if (ints[start] + ints[end] < find) {
start++;
} else if (ints[start] + ints[end] > find) {
end--;
}
System.out.println(start +" "+end);
}
}
一个排好序的数组,找出两数之和为m的所有组合
最新推荐文章于 2022-04-08 17:51:13 发布
本文介绍了一种在已排序数组中寻找两个数使它们的和等于特定目标值的算法。该算法通过定义两个指针分别从数组两端开始向中间移动的方式进行查找,实现了高效的解决方案。

5万+

被折叠的 条评论
为什么被折叠?



