238. 数组除自身以外的乘积 | LeetCode | Top Interview 150 | 编程题

发布: (2025年12月18日 GMT+8 05:36)
1 min read
原文: Dev.to

Source: Dev.to

问题链接

https://leetcode.com/problems/product-of-array-except-self/

leetcode 238

解法

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] left = new int[n];
        int[] right = new int[n];

        left[0] = 1;
        for (int i = 1; i = 0; i--) {
            right[i] = right[i + 1] * nums[i + 1];
        }

        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            ans[i] = left[i] * right[i];
        }

        return ans;
    }
}
Back to Blog

相关文章

阅读更多 »