Java 如何在一个条件下找到数组中数字的索引位置?
我有一个整数数组。。 我会输入一个数字 我必须找出两个数的索引位置,这样两个数的和 等于输入的数字 我是用下面的代码做的Java 如何在一个条件下找到数组中数字的索引位置?,java,arrays,Java,Arrays,我有一个整数数组。。 我会输入一个数字 我必须找出两个数的索引位置,这样两个数的和 等于输入的数字 我是用下面的代码做的 import java.io.BufferedInputStream; import java.util.Arrays; import java.util.Scanner; public class FindIndex { int a[] = { 1, 7, 6, 5, 12, 2, 3, 11 }; public void findingIndex() {
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;
public class FindIndex {
int a[] = { 1, 7, 6, 5, 12, 2, 3, 11 };
public void findingIndex() {
Arrays.sort(a);
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sum of two selected numbers from the array");
int i = sc.nextInt();
for(int j=0;j<a.length;j++){
for(int k=j+1;k<a.length;k++){
if(a[j]+a[k]==i){
System.out.println(Arrays.toString(a));
System.out.println("The indexes of the elements are"+j+"and"+k);
}
}
}
}
public static void main(String[] args) {
FindIndex fi = new FindIndex();
fi.findingIndex();
System.out.println("end of the program");
}
现在我只想使用一个for循环来实现这一点??如何做?由于数组已经排序,您可以创建两个索引,一个从开头开始,一个从结尾开始。如果当前和小于要查找的数字,则增加第一个索引,否则减少第二个索引
public static void findingIndex() {
Arrays.sort(a);
System.out.println(Arrays.toString(a));
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sum of two selected numbers from the array");
int i = sc.nextInt();
int index1 = 0;
int index2 = a.length-1;
while(index1 != index2){
int sum = a[index1] + a[index2];
if(sum == i){
System.out.println("Found with "+index1+" "+index2);
index1++;
}
else if (sum < i){
index1++;
} else {
index1 = 0;
index2--;
}
}
}
你可以用这种方式在*logn时间内完成
public static int[] findSum(int[] values, int sum) {
Arrays.sort(values); // O(N * log N)
for(int i = 0; i < values.length() - 1; i ++) { // O(n)
int remainder = sum - values[i];
// O(log N) and assuming you can't use the same value twice.
int pos2 = Arrays.binarySearch(value, i + 1, values.length, remainder);
if (pos2 >= 0)
return new int[] { i, pos2 };
}
return null;
}
因此,总的顺序是在logn+ON*Olog N上,或者仅仅在logn上任何场景都有一个最佳级别,我们不能再简化到该级别之外。你可以试试下面的
Integer a[] = { 1, 2, 3, 5, 6, 7, 11, 12} ;
List<Integer> list=new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sum of two selected numbers from the array");
int in = sc.nextInt();
for(int i=0;i<a.length;i++){
if(list.contains(in-a[i]) &&(list.indexOf(in-a[i])!=i)){
System.out.println("The indexes of the elements are"
+i+"and"+list.indexOf(in-a[i]));
}
}
但包含它自己为循环使用的方法 高棉。。。你想用一个循环来解决一个开^2的问题,我想你是说开循环?唯一的方法是将复杂度转移到空间域中,即^2内存结构上的复杂度。如果对其进行排序,将无法获得原始数组的正确索引,而将获得已排序数组的索引。while循环的复杂度将启用,因为在最坏的情况下,您将检查每个元素一次。总体复杂度为*longn,因为这是排序的复杂度。但我不确定你是否遗漏了一些解决方案。我感觉这个算法会漏掉一些。也许我错了。@peter.petrov是的,我说这是在while循环开/2=开。我很确定我不会错过一些案例。问题是当你减少index2时,你应该将index1设置为0,因为你不知道你需要回溯多远。@PeterLawrey是的,我认为你是对的。所以整体的复杂性是对的。。。我失败了@user2336315您可以使用正在排序数组中搜索sum-a[index1]的事实,请参阅我的答案;您可以存储indexOf的值,而不是callcontains。顺便说一句,indexOf是启用的,因为它使用循环,两个匹配的索引可能是最后两个。
public static int[] findSum(int[] values, int sum) {
Arrays.sort(values); // O(N * log N)
for(int i = 0; i < values.length() - 1; i ++) { // O(n)
int remainder = sum - values[i];
// O(log N) and assuming you can't use the same value twice.
int pos2 = Arrays.binarySearch(value, i + 1, values.length, remainder);
if (pos2 >= 0)
return new int[] { i, pos2 };
}
return null;
}
Integer a[] = { 1, 2, 3, 5, 6, 7, 11, 12} ;
List<Integer> list=new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sum of two selected numbers from the array");
int in = sc.nextInt();
for(int i=0;i<a.length;i++){
if(list.contains(in-a[i]) &&(list.indexOf(in-a[i])!=i)){
System.out.println("The indexes of the elements are"
+i+"and"+list.indexOf(in-a[i]));
}
}