Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

238. Product of Array Except Self

Posted on 2020-07-12 | Edited on 2021-01-22

LeetCode


Math

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int[] productExceptSelf(int[] nums) {
int leng = nums.length;
int[] res = new int[leng];
if (leng == 0) return res;
int runningprefix = 1;
for (int i = 0; i < leng; i++) {
res[i] = runningprefix;
runningprefix *= nums[i];
}
int runningsufix = 1;
for (int i = leng - 1; i >= 0; i--) {
res[i] *= runningsufix;
runningsufix *= nums[i];
}
return res;
}
}

<1…535455…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%