LeetCode238. Product of Array Except Self
来源:互联网 发布:北京海隆软件 编辑:程序博客网 时间:2024/06/06 12:35
Solution1 Two-pass O(n) time O(n) space
Considering that we are trying to get something like cumulative sum except self, it is possible that we can utilize some method similar to cumulative sum to get this product. So for this product of array except self, we actually want the product of the left side of the array multiplies the product of the right side of the array. Yes! we can use the 2 side cumulative product, i.e. one multiplies from left to right one by one and the other multiplies from right to left one by one. And thus for a specific product except self, we simply get the product of left side and right side.
Time complexity: O(2n) = O(n)
because of the 2 passes.
Space complexity: O(2n) = O(n)
because of the 2 arrays.
class Solution { /** * O(n) time, O(n) space 2 pass solution * Calculate the left cumulative product and right cumulative product respectively. * And then get the product expect self in O(1). */ public int[] productExceptSelf(int[] nums) { int product = 1; int[] leftProduct = new int[nums.length]; int[] rightProduct = new int[nums.length]; // left cumulative product for (int i = 1; i <= nums.length - 1; i++) { leftProduct[i] = product * nums[i - 1]; product = leftProduct[i]; } leftProduct[0] = 1; product = 1; // right cumulative product for (int i = nums.length - 2; i >= 0; i--) { rightProduct[i] = product * nums[i + 1]; product = rightProduct[i]; } rightProduct[nums.length - 1] = 1; int[] ret = new int[nums.length]; for (int i = 0; i < nums.length; i++) { ret[i] = leftProduct[i] * rightProduct[i]; } return ret; }}
Solution2 Two-pass O(n) time O(1) space without considering the return array
Although we stored the left and right cumulative product in the previous solution, actually since we are only using one right product each time, we can calculate it on the fly, rather than store it. And for the left product, we could override it step by step from right to left to make it stores the return results.
Time complexity: O(2n) = O(n)
.
Space complexity: O(1)
without considering the return array.
class Solution { public int[] productExceptSelf(int[] nums) { int len = nums.length; int[] ret = new int[len]; ret[0] = 1; for (int i = 1; i < len; i++) { ret[i] = nums[i - 1] * ret[i - 1]; } int right = 1; for (int i = len - 1; i >= 0; i--) { ret[i] = ret[i] * right; right = right * nums[i]; } return ret; }}
Solution3 One-pass O(n) time O(1) space
Scan the array from left to right in the same for loop.
class Solution { public int[] productExceptSelf(int[] nums) { int left = 1; int right = 1; int len = nums.length; int[] ret = new int[len]; Arrays.fill(ret, 1); for (int i = 0, j = len - 1; i < len - 1; i++, j--) { left *= nums[i]; right *= nums[j]; ret[i + 1] *= left; ret[j - 1] *= right; } return ret; }}
- LeetCode238:Product of Array Except Self
- [leetcode238]Product of Array Except Self
- leetcode238 Product of Array Except Self
- [LeetCode238]Product of Array Except Self
- [leetcode238]Product of Array Except Self
- leetcode238. Product of Array Except Self
- Leetcode238. Product of Array Except Self
- LeetCode238. Product of Array Except Self
- LeetCode238. Product of Array Except Self解题
- LeetCode238——Product of Array Except Self
- LeetCode238 Product of Array Except Self(java and python solution)
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- 删除xcode证书
- 购物车逻辑Angularjs
- 如何用Tomcat部署前端静态文件
- CCF编程练习:俄罗斯方块(Java)
- git回滚到上一版本(撤销上一步commit)
- LeetCode238. Product of Array Except Self
- oracle数据库通过回滚点方式做重大版本部署回退方案
- ISIS报文格式—LSP
- 栈之前,中,后缀表达式求值
- mysql关于时间戳
- ImageLoader之加载不同来源的图片方式
- iOS浮点数去掉小数点之后的0
- Java 中 byte、byte 数组和 int、long 之间的转换
- php中file_get_contents