public class Solution {
public int[] ProductExceptSelf(int[] nums) {
int[] result = new int[nums.Length];
for (int i = , tmp = ; i < nums.Length; i++)
{
result[i] = tmp;
tmp *= nums[i];
}
for (int i = nums.Length - , tmp = ; i >= ; i--)
{
result[i] *= tmp;
tmp *= nums[i];
}
return result;
}
}

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

在第一个循环中,记录当前的索引左侧的数字乘积,和当前的索引右侧的数字乘积。然后两部分乘积相乘,这样的思路比较简单,但是时间复杂度是O(n*n)。

本题的解法时间复杂度更低,但是代码不容易理解。

补充一个python的实现:

 class Solution:
def productExceptSelf(self, nums: 'List[int]') -> 'List[int]':
n = len(nums)
front = [1] * n
front[0] = nums[0] back = [1] * n
back[n-1] = nums[n-1] for i in range(1,n):
front[i] = front[i-1] * nums[i] for i in range(n-2,0,-1):
back[i] = back[i+1] * nums[i] l = list()
for i in range(n):
if i == 0:
f = 1
b = back[i+1]
l.append(f*b)
elif i == n-1:
f = front[i-1]
b = 1
l.append(f*b)
else:
f = front[i-1]
b = back[i+1]
l.append(f*b)
return l

leetcode238的更多相关文章

  1. [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 ...

  2. [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 ...

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

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

  4. LeetCode 238

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

  5. 剑指offer-java

    面试题67 机器人的运动范围 题意: 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. ...

  6. 2017-3-10 leetcode 229 238 268

    今天登陆leetcode突然发现531被锁了,有种占了便宜的感觉哈哈哈! ================================================ leetcode229 Ma ...

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

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

随机推荐

  1. L330 Black hole picture captured for first time in space ‘breakthrough’

    Black hole picture captured for first time in space ‘breakthrough’ Astronomers have captured the fir ...

  2. list的相关函数

    # ### 列表相关的函数 # (1) append ''' 功能:向列表的末尾添加新的元素 格式:列表.append(值) 返回值:None 注意:新添加的值在列表的末尾,该函数直接操作原有列表 ' ...

  3. Bitmap RGB24 4字节对齐

    Bitmap RGB24 4字节对齐 本文中说的图片都是无压缩的彩色Bitmap图片. 最近在一个项目中有一个场景是需要将RGB32或RGB24的Bitmap转换成为RGB565的Bitmap,在RG ...

  4. FreeSWITCH与FreeSWITCH对接

    (主机A ---> 主机B)192.168.100.A主机:修改/usr/local/freeswitch/conf/dialplan/default.xml 10         <ex ...

  5. 剑指Offer 56. 删除链表中重复的结点 (链表)

    题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...

  6. 剑指Offer 4. 重建二叉树 (二叉树)

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  7. gpu/mxGPUArray.h” Not Found

    https://cn.mathworks.com/matlabcentral/answers/294938-cannot-find-lmwgpu More specifically change th ...

  8. mybatis(一、原理,一对多,多对一查询)

    MyBatis框架及原理分析 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转换 ...

  9. $\mathcal{OI}$生涯中的各种数论算法的证明

    嗯,写这个是因为我太弱了\(ORZ\). #\(\mathcal{\color{silver}{1 \ \ Linear \ \ Sieve \ \ Method \ \ of \ \ Prime}} ...

  10. PythonStudy——Python 注释规范

    注释规范:   什么是注释?  注释:不会被python解释器解释执行,是提供给开发者阅读代码的提示 单行注释: # 开头的语句 多行注释:出现在文件最上方,用''' '''包裹的语句   Pycha ...