238题:除自身以外数组的乘积
写作背景:由于最近在练习leetcode,这道题刚开始思路不太清晰,所以将自己的解题思路记录下来,以便后续复习。
题目描述:
解题思路:
- 1.暴力解法:遍历数组,每次遍历时,将当前元素置为1,然后遍历数组,将其他元素相乘,得到结果,将结果放入结果数组中,时间复杂度为O(n^2)
- 2.优化解法:遍历一遍数组,得到数组的乘积,然后遍历数组,将数组的乘积除以当前元素,得到结果,将结果放入结果数组中,时间复杂度为O(n)
- 难点:不能使用除法,且时间复杂度为O(n)
- 解决思路:使用模拟手动运算方法,把除法转化为乘法计算。
public class P238 {
public int[] productExceptSelf(int[] nums) {
if (nums.length == 0){
return nums;
}
int sumMultiply = 1;
int[] res = new int[nums.length];
int zeroCount = 0;
int zeroPos = -1;
for (int i = 0;i< nums.length;i++) {
if (0 != nums[i])
sumMultiply *= nums[i];
else {
zeroCount++;
zeroPos = i;
}
}
if (zeroCount > 1)
return res;
else if (zeroCount == 1) {
res[zeroPos] = sumMultiply;
return res;
}else {
for (int i = 0;i<nums.length;i++) {
res[i] = (int) (sumMultiply * calculateReciprocal(nums[i]));
}
}
return res;
}
private static double calculateReciprocal(int val) {
if (val == 0) {
return 0;
}
boolean negative = false;
if (val < 0) {
negative = true;
val = -val;
}
int iniVal = 1;
double iniInt = 1.0;
double iniDouble = 0.1;
double finalVal = 0.0;
int maxAcc = 16;
int iniAcc = 0;
do {
int count = 0;
while (iniVal < val) {
iniVal *= 10;
iniInt *= iniDouble;
}
while (iniVal >= val) {
iniVal -= val;
count++;
}
finalVal += iniInt * count;
iniAcc++;
} while (iniVal != 0 && iniAcc < maxAcc);
return negative ? -finalVal : finalVal;
}
}
- 3.还可以通过数学方法计算一个数倒数并且不涉及除法,解决方法:使用对数来计算倒数,即:1/x = e^(-ln(x)),这样就可以使用乘积倒数来计算结果。
但是此方法由于java中的double类型精度问题,导致结果不准确,所以不推荐使用,但是可以作为一种思路。
public class P238 {
public int[] productExceptSelf(int[] nums) {
if (nums.length == 0){
return nums;
}
int sumMultiply = 1;
int[] res = new int[nums.length];
int zeroCount = 0;
int zeroPos = -1;
for (int i = 0;i< nums.length;i++) {
if (0 != nums[i])
sumMultiply *= nums[i];
else {
zeroCount++;
zeroPos = i;
}
}
if (zeroCount > 1)
return res;
else if (zeroCount == 1) {
res[zeroPos] = sumMultiply;
return res;
}else {
for (int i = 0;i<nums.length;i++) {
res[i] = (int) (sumMultiply * calculateReciprocal(nums[i]));
}
}
return res;
}
private static double calculateReciprocal(int val) {
if (val == 0) {
return 0;
}
boolean negative = false;
if (val < 0) {
negative = true;
val = -val;
}
return negative ? -Math.exp(-Math.log(val)) : Math.exp(-Math.log(val));
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理