我一直在解决leetcode上的问题(数组的乘积,除了Self)。我的代码编译成功,通过了19个测试用例中的18个。我试图找出失败的测试用例,但没有成功。
异常:超过时间限制
输入:https://leetcode.com/submissions/detail/439297112/testcase/ - (70000个整数)
我的代码
public int[] productExceptSelf(int[] nums) {
int arr[] = { 1, 2, 3, 4 };
return mySolution(arr);
}
public static int[] mySolution(int arr[]) {
int size = arr.length;
int j;
int product;
int[] result = new int[size];
for (int i = 0; i < size; i++) {
j = 0;
product = 1;
while (j < size) {
if (j == i) {
j++;
continue;
}
product *= arr[j];
j++;
}
result [i] = product;
}
return result;
}发布于 2021-01-06 17:56:22
我认为这个逻辑应该会减少你的时间。我还没检查过。
product = 1;
for (int i = 0; i < size; i++) {
product *= arr[j];
}
for (int i = 0; i < size; i++) {
result[i] = product/arr[i]
}https://stackoverflow.com/questions/65593026
复制相似问题