238. Product of Array Except Self Medium

@problem@discussion
#Array#Prefix Sum



1/**
2 * [238] Product of Array Except Self
3 *
4 * Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
5 * The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
6 * You must write an algorithm that runs in O(n) time and without using the division operation.
7 *  
8 * Example 1:
9 * Input: nums = [1,2,3,4]
10 * Output: [24,12,8,6]
11 * Example 2:
12 * Input: nums = [-1,1,0,-3,3]
13 * Output: [0,0,9,0,0]
14 *  
15 * Constraints:
16 * 
17 * 	2 <= nums.length <= 10^5
18 * 	-30 <= nums[i] <= 30
19 * 	The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
20 * 
21 *  
22 * Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
23 * 
24 */
25pub struct Solution {}
26
27// problem: https://leetcode.com/problems/product-of-array-except-self/
28// discuss: https://leetcode.com/problems/product-of-array-except-self/discuss/?currentPage=1&orderBy=most_votes&query=
29
30// submission codes start here
31
32impl Solution {
33    pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
34        vec![]
35    }
36}
37
38// submission codes end
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_238() {
46    }
47}
48


Back
© 2025 bowen.ge All Rights Reserved.