道阻且长,行则将至,走慢一点没关系,不停下就好了.|

Ac_c0mpany丶

园龄:3年7个月粉丝:6关注:3

2024-01-14 17:34阅读: 3评论: 0推荐: 0

[刷题技巧] LeetCode238. 除自身以外数组的乘积

题目描述

思路:前缀/后缀乘积数组

构造除自身以外数组的左边前缀乘积
构造除自身以外数组的右边后缀乘积
然后对应位置相乘

方法一:

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        // 前缀乘积数组: leftProduct[i]表示索引i左侧所有元素的乘积
        int[] leftProduct = new int[n];
        leftProduct[0] = 1;
        for (int i = 1; i < n; i ++) {
            leftProduct[i] = leftProduct[i - 1] * nums[i - 1];
        }
        // 后缀乘积数组:rightProduct[i]表示索引i右侧所有元素的乘积
        int[] rightProduct = new int[n];
        rightProduct[n - 1] = 1;
        for (int i = n - 2; i >= 0; i --) {
            rightProduct[i] = rightProduct[i + 1] * nums[i + 1];
        }
        // 对应位置相乘
        int[] res = new int[n];
        for (int i = 0; i < n; i ++) {
            res[i] = leftProduct[i] * rightProduct[i];
        }
        return res;
    }
}

时间复杂度:O(n)
额外空间复杂度:O(n)

方法二:优化方法一

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] res = new int[n];
        res[0] = 1;
        // res[i]表示索引i左侧所有元素的乘积
        for (int i = 1; i < n; i ++) {
            res[i] = res[i - 1] * nums[i - 1];
        }
        // 每个元素的右边所有元素的乘积存储在一个变量中
        int rightProduct = 1;
        for (int i = n - 1; i >= 0; i --) {
            // 对于索引i左边的乘积为res[i],右边的乘积为rightProduct
            res[i] = res[i] * rightProduct;
            // 更新右边乘积
            rightProduct = rightProduct * nums[i];
        }
        return res;
    }
}

在方法一的基础上进行优化:

  • 将res用于记录left数组
  • 然后用一个变量维护right数组

时间复杂度:O(n)
额外空间复杂度:O(1)

本文作者:Ac_c0mpany丶

本文链接:https://www.cnblogs.com/keyongkang/p/17963948

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ac_c0mpany丶  阅读(3)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 You Are My Sunshine REOL
You Are My Sunshine - REOL
00:00 / 00:00
An audio error has occurred.

作曲 : Traditional

You are my sunshine

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away

The other night, dear,

When I lay sleeping

I dreamed I held you in my arms.

When I awoke, dear,

I was mistaken

So I hung my head and cried.

You are my sunshine,

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away.

You are my sunshine,

My only sunshine

You make me happy

When skies are gray.

You'll never know, dear

How much I love you

Please don't take my sunshine away

Please don't take my sunshine away.

Please don't take my sunshine away.