[LeetCode]238.Product of Array Except Self

简介:

题目

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].

Follow up:

Could you solve it with constant space complexity? 
(Note: The output array does not count as extra space 
for the purpose of space complexity analysis.)
AI 代码解读

思路

这里写图片描述

对于i = 5 时 result[5] = (nums[0] * nums[1] * nums[2] * nums[3] * nums[4] ) * (nums[6] nums[7] * nums[8] * nums[9] * nums[10])

从上面开始看出对于第i个,我们只要知道它左边的连续乘积它右边的连续乘积 就OK了。

代码

/*---------------------------------------
*   日期:2015-07-31
*   作者:SJF0115
*   题目: 238.Product of Array Except Self
*   网址:https://leetcode.com/problems/product-of-array-except-self/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int size = nums.size();
        vector<int> result(size,0);
        vector<int> left(size,0);
        vector<int> right(size,0);
        int leftNum = 1,rightNum = 1;
        // left[i] 为 nums[0]...nums[i-1]的连续乘积
        // right[i] 为 nums[i+1]...nums[size-1]的连续乘积
        for(int i = 0;i < size;++i){
            left[i] = leftNum;
            right[size-i-1] = rightNum;
            leftNum *= nums[i];
            rightNum *= nums[size-i-1];
        }//for
        // 计算不包括自己的所有乘积
        for(int i = 0;i < size;++i){
            result[i] = left[i] * right[i];
        }//for
        return result;
    }
};

int main(){
    Solution s;
    vector<int> vec = {1,2};
    vector<int> result = s.productExceptSelf(vec);
    int size = result.size();
    for(int i = 0;i < size;++i){
        cout<<result[i]<<" ";
    }//for
    return 0;
}
AI 代码解读

运行时间

这里写图片描述

目录
打赏
0
0
0
0
63
分享
相关文章
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
78 0
LeetCode 410. Split Array Largest Sum
给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组。设计一个算法使得这 m 个子数组各自和的最大值最小。
164 0
LeetCode 410. Split Array Largest Sum
LeetCode 330. Patching Array
给定一个已排序的正整数数组 nums,和一个正整数 n 。从 [1, n] 区间内选取任意个数字补充到 nums 中,使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数字的和来表示。请输出满足上述要求的最少需要补充的数字个数。
105 0
LeetCode 330. Patching Array
LeetCode 315. Count of Smaller Numbers After Self
给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。
128 0
LeetCode 315. Count of Smaller Numbers After Self
LeetCode 238. Product of Array Except Self
给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。
109 0
LeetCode 238. Product of Array Except Self
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 1013. 将数组分成和相等的三个部分 Partition Array Into Three Parts With Equal Sum
LeetCode 1013. 将数组分成和相等的三个部分 Partition Array Into Three Parts With Equal Sum

热门文章

最新文章

  • 1
    通过array.reduce()实现数据汇总、条件筛选和映射、对象属性的扁平化、转换数据格式、聚合统计、处理树结构数据和性能优化,reduce()的使用详解(附实际应用代码)
    46
  • 2
    JavaScript 中通过Array.sort() 实现多字段排序、排序稳定性、随机排序洗牌算法、优化排序性能,JS中排序算法的使用详解(附实际应用代码)
    12
  • 3
    JavaScript中通过array.map()实现数据转换、创建派生数组、异步数据流处理、复杂API请求、DOM操作、搜索和过滤等,array.map()的使用详解(附实际应用代码)
    11
  • 4
    深入理解 JavaScript 中的 Array.find() 方法:原理、性能优势与实用案例详解
    6
  • 5
    JavaScript中通过array.filter()实现数组的数据筛选、数据清洗和链式调用,JS中数组过滤器的使用详解(附实际应用代码)
    10
  • 6
    别再用双层遍历循环来做新旧数组对比,寻找新增元素了!使用array.includes和Set来提升代码可读性
    3
  • 7
    Java 中数组Array和列表List的转换
    10
  • 8
    通过array.some()实现权限检查、表单验证、库存管理、内容审查和数据处理;js数组元素检查的方法,some()的使用详解,array.some与array.every的区别(附实际应用代码)
    2
  • 9
    多维数组操作,不要再用遍历循环foreach了!来试试数组展平的小妙招!array.flat()用法与array.flatMap() 用法及二者差异详解
    5
  • 10
    通过array.every()实现数据验证、权限检查和一致性检查;js数组元素检查的方法,every()的使用详解,array.some与array.every的区别(附实际应用代码)
    2
  • AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等