Let's say I am given an array
假设我有一个数组
int[] arr = {1, 2, 3, 4, 5, 6}
and I have to input a "key" value which is based on user input.
我必须输入一个基于用户输入的“键”值。
If the array have any two elements that sums up to my key value it returns a true value or otherwise a false value. Why do I still have issue with my algorithm? Is there some misconception that I am putting in the nested loop? Any help with this please?
如果数组中有任何两个元素总和为我的键值,它将返回一个true值或一个false值。为什么我的算法仍然有问题?是否存在将我放入嵌套循环的误解?有什么帮助吗?
public static boolean checkPair(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == key) {
return true;
} else {
return false;
}
}
}
return false;
}
3 个解决方案
#1
6
Your else
-clause is wrong. It always returns false
if the first pair hasn't the same value.
你的else子句是错误的。如果第一对的值不相同,则返回false。
Change your code like this:
像这样更改代码:
public static boolean checkPair(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == key) {
return true;
}
//else {
// return false; <- this returns always false if the
// first pair isnt a i,j-match
//}
}
}
return false; // stub
}
Explanation
解释
In your example int[] arr = {1, 2, 3, 4, 5, 6}
在您的示例int[] arr = {1,2,3,4,5,6}
arr[i] = 1
and a[j] = 2
If you set key to any int except 3, the method will return false and the other numbers will not be considered
arr[i] = 1, a[j] = 2如果你将键设置为除3之外的任意整数,该方法将返回false,其他数字将不被考虑
#2
1
I think, this task can be solved faster than your algorithm. Are your array sorted?? If yes, you can do that:
我认为,这个任务可以比你的算法更快地解决。你的数组排序?如果是,你可以这样做:
public static boolean checkPair(int[] arr, int key) {
int i = 0; //first index
int j = arr.length - 1; //last index
while (i < j) {
if (arr[i] + arr[j] > key){
j--;
continue;
}
if (arr[i] + arr[j] < key){
i++;
continue;
}
else {
return true;
}
}
return false;
}
If your array are not sorted, you can sort it with java standart sort and use my algorithm. My algorithm faster more than 2 times.
如果数组没有排序,可以使用java standart排序并使用我的算法。我的算法快了2倍多。
#3
-2
you just break loop like this:
你只需要像这样打破循环:
public static boolean checkPair(int[] arr, int key) {
boolean isMacthed = false;
for (int i=0; i<arr.length; i++) {
for (int j=i+1; j<arr.length; j++) {
if (arr[i] + arr[j] == key) {
isMacthed=true;
break;
}
}
if(isMacthed) break;
}
return isMacthed ;
}
#1
6
Your else
-clause is wrong. It always returns false
if the first pair hasn't the same value.
你的else子句是错误的。如果第一对的值不相同,则返回false。
Change your code like this:
像这样更改代码:
public static boolean checkPair(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == key) {
return true;
}
//else {
// return false; <- this returns always false if the
// first pair isnt a i,j-match
//}
}
}
return false; // stub
}
Explanation
解释
In your example int[] arr = {1, 2, 3, 4, 5, 6}
在您的示例int[] arr = {1,2,3,4,5,6}
arr[i] = 1
and a[j] = 2
If you set key to any int except 3, the method will return false and the other numbers will not be considered
arr[i] = 1, a[j] = 2如果你将键设置为除3之外的任意整数,该方法将返回false,其他数字将不被考虑
#2
1
I think, this task can be solved faster than your algorithm. Are your array sorted?? If yes, you can do that:
我认为,这个任务可以比你的算法更快地解决。你的数组排序?如果是,你可以这样做:
public static boolean checkPair(int[] arr, int key) {
int i = 0; //first index
int j = arr.length - 1; //last index
while (i < j) {
if (arr[i] + arr[j] > key){
j--;
continue;
}
if (arr[i] + arr[j] < key){
i++;
continue;
}
else {
return true;
}
}
return false;
}
If your array are not sorted, you can sort it with java standart sort and use my algorithm. My algorithm faster more than 2 times.
如果数组没有排序,可以使用java standart排序并使用我的算法。我的算法快了2倍多。
#3
-2
you just break loop like this:
你只需要像这样打破循环:
public static boolean checkPair(int[] arr, int key) {
boolean isMacthed = false;
for (int i=0; i<arr.length; i++) {
for (int j=i+1; j<arr.length; j++) {
if (arr[i] + arr[j] == key) {
isMacthed=true;
break;
}
}
if(isMacthed) break;
}
return isMacthed ;
}