238. 除自身以外数组的乘积

238. Product of Array Except Self

题目描述

LeetCode

LeetCode238. Product of Array Except Self中等

Java 实现

class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] before = new int[n], after = new int[n], res = new int[n];
before[0] = 1;
after[n - 1] = 1;
for (int i = 1; i < n; i++) {
before[i] = before[i - 1] * nums[i - 1];
}
for (int i = n - 2; i >= 0; i--) {
after[i] = after[i + 1] * nums[i + 1];
}
for (int i = 0; i < n; i++) {
res[i] = after[i] * before[i];
}
return res;
}
}

相似题目

参考资料

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

  1. Java实现 LeetCode 238 除自身以外数组的乘积

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

  2. [leetcode]238. 除自身以外数组的乘积

    题目描述 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输 ...

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

    题目描述 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输 ...

  4. [LeetCode] 238. 除自身以外数组的乘积 ☆☆☆(左积*右积)

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

  5. leetcode 238. 除自身以外数组的乘积 (python)

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

  6. [Swift]LeetCode238. 除自身以外数组的乘积 | Product of Array Except Self

    Given an array nums of n integers where n > 1,  return an array outputsuch that output[i] is equa ...

  7. Leetcode题目238.除自身以外数组的乘积(中等)

    题目描述: 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: ...

  8. leecode 238除自身以外数组的乘积

    class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { //用除法必须要 ...

  9. 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)

    剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...

随机推荐

  1. A Class of Blind Source Extraction Method Using Second-Order Statistics

    基于二阶统计量的盲源提取方法[1]. 文中提出了一系列基于二阶统计量的算法,包括离线BSE和在线BSE算法,可以提取平稳信号和非平稳信号.这些算法中,通过挖掘信号特征,提出了新的打分函数,以及一个无参 ...

  2. Kali Linux 2019.4中文乱码解决

    1.先换源deb http://mirrors.aliyun.com/kali kali-rolling main non-free contribdeb-src http://mirrors.ali ...

  3. ssh集成

    导入pom依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  4. 在windbg调试.net时遇到的问题

    调试.net应用程序时,有时会在windbg中收到错误消息.以下是我最常遇到的几个问题. Failed to start stack walk---启动堆栈遍历失败 如果你运行sos命令!clrsta ...

  5. 【luoguP5091】【模板】欧拉定理

    题目链接 欧拉定理: 当\(a\),\(m\)互质时,\(a^{\phi(m)}\equiv 1 (mod ~ m)\) 扩展欧拉定理: 当\(B>\phi(m)\)时,\(a^B\equiv ...

  6. 原创:协同过滤之spark FP-Growth树应用示例

    上一篇博客中,详细介绍了UserCF和ItemCF,ItemCF,就是通过用户的历史兴趣,把两个物品关联起来,这两个物品,可以有很高的相似度,也可以没有联系,比如经典的沃尔玛的啤酒尿布案例.通过Ite ...

  7. 【POJ2965】The Pilots Brothers' refrigerator

    题目传送门 本题知识点:深度优先搜索 + 暴力枚举 + 回溯 + 栈 如果对以上知识点还不熟悉的同学建议先做做 POJ1753 (本题是1753的提高版)(附 POJ1753博客) 以下默认您已ACP ...

  8. 龙贝格积分(c++)

    用龙贝格算法计算积分 #include <iostream> #include<cmath> #include <iomanip> using namespace  ...

  9. SDN第七次上机作业

    1.补充并运行basic代码 任务是实现基础的交换机转发数据包功能 补充后代码如下: /* -*- P4_16 -*- */ #include <core.p4> #include < ...

  10. process.env.NODE_ENV理解

    1.理解NODE_ENV 在node中,有全局变量process表示的是当前的node进程.process.env包含着关于系统环境的信息.但是process.env中并不存在NODE_ENV这个东西 ...