给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。

示例:

输入: [1,2,3,4] 输出: [24,12,8,6]

说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。

进阶:

你可以在常数空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)

temp用来记录从0到i - 1个元素的乘积

output[i]表示从i到最后一个元素的乘积

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums)
{
int len = nums.size();
vector<int> output(len, 0);
vector<int> save(len, 0);
int temp = 0;
for(int i = len - 1; i >= 0; i--)
{
if(i == len - 1)
{
temp = nums[len - 1];
save[i] = temp;
}
else
{
temp *= nums[i];
save[i] = temp;
}
}
temp = 1;
for(int i = 0; i < len - 1; i++)
{
output[i] = temp * save[i + 1];
temp *= nums[i];
}
output[len - 1] = temp;
return output;
}
};

Leetcode238. Product of Array Except Self除自身以外数组的乘积的更多相关文章

  1. 238. Product of Array Except Self除自身以外数组的乘积

    网址:https://leetcode.com/problems/product-of-array-except-self/ 参考:https://leetcode.com/problems/prod ...

  2. 238 Product of Array Except Self 除自身以外数组的乘积

    一个长度为 n 的整形数组nums,其中 n > 1,返回一个数组 output ,其中 output[i] 等于nums中除nums[i]以外所有元素的乘积.不用除法 且在O(n)内解决这个问 ...

  3. [LeetCode238]Product of Array Except Self

    题目: Given an array of n integers where n > 1, nums, return an array output such that output[i] is ...

  4. LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)

    238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...

  5. [LintCode] Product of Array Except Self 除本身之外的数组之积

    Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...

  6. 【LeetCode】Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  7. 【08_238】Product of Array Except Self

    Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...

  8. 【LeetCode】238. Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  9. LeetCode OJ 238. Product of Array Except Self 解题报告

        题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...

随机推荐

  1. webjars和springboot热启动

    webjars WebJars将Web前端Javascript和CSS等资源打包成Java的Jar包, 以便能使Maven的依赖管理支持静态JavaScript库/CSS库,比如jQuery.layu ...

  2. amazeUI tab禁止左右滑动(触控操作)

    参考:http://amazeui.clouddeep.cn/javascript/tabs/ 效果: html: <!DOCTYPE html> <html> <hea ...

  3. tbody设置超出固定的高度出现滚动条,没超出不显示。

    没有超出时显示样式,不显示滚动条: 超出时显示滚动条: 1.html <table class="table"> <thead> <tr> &l ...

  4. postgresql数据库学习-win平台下SQLshell基础操作及语法

    由于在学习https://www.bilibili.com/video/av24590479小马视频时, up主采用的linux虚拟机进行教学, 而本人采用window7进行操作,故在基础操作和语法上 ...

  5. Ubuntu clion下载及激活

    1.下载 方法:去官网下载clion  https://www.jetbrains.com/clion/download/#section=linux 或者使用我上传的百度网盘链接: https:// ...

  6. NFS+mou

    前言 有两台电脑,Linux操作系统,服务器和客户端,IP不同,但是可以相互访问. 客户端想访问服务器的文件系统 准备工作 假设 服务器的ip为 192.168.0.100,要分享为公共文件夹的目录为 ...

  7. Android基础控件ListView和自定义BaseAdapter适配器

    1.简介 ListView用于列表显示,相当于OC中的TableView,和适配器一块使用,相关属性: footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条, ...

  8. 图解SQL的Join

    原文地址:http://coolshell.cn/articles/3463.html 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的 ...

  9. javaSpring学习总结day_01

    本文章用于总结自己学习知识,有不足或错误之处清谅解. bean.xml 文件的读取方式: ClassPathXmlApplicationContext: 它是只能加载类路径下的配置文件 推荐 1.加载 ...

  10. https证书加密

    对称加密 浏览器向服务端发送请求时,服务端首先给浏览器发送一个秘钥,浏览器用秘钥对传输的数据进行加密后发送给浏览器,浏览器拿到加密后的数据使用秘钥进行解密 非对称加密 服务端通过rsa算法生成一个公钥 ...