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. kvm恢复和删除快照

    使用文件快照的方式实现文件备份,但单说快照(snapshot)的话,他是某一时间点(版本)你能看到的该时间点备份文件状态的全貌,通过文件的快照(全貌)你能恢复到特定时间点(版本)的文件状态. 创建虚拟 ...

  2. vs2015和Oracle在一起时的Shit问题

    VS2015在连接Oracle时,必须要安装到一个不含有空格的目录中去,否则连接不上Oracle,至于为什么,不知道,鬼知道,日的. 如果你不幸以前安装过VS2015,安装到它的默认的什么“progr ...

  3. WEBBASE篇: 第七篇, JavaScript知识1

    JavaScript 1 一.JavaScript 概述什么是JavaScript: JavaScript 简称 JS,是一种专门运行于JS解释器/引擎中的解释型脚本语言JS发展史: 1.1992年N ...

  4. i3wm 入门

    安装 所用Linux版本为kali-rolling,直接安装 apt install i3 设置为xinit的启动对像 修改 ~/.xserverrc #!/bin/sh exec /usr/bin/ ...

  5. js 数字随机滚动(数字递增)

    HTML: <div class="textMon"> <img src="./img/20180830160315.png" alt=&qu ...

  6. 产生10个随机数5-9之间 统计一个int类型的一维数组中有多少个在[min,max]之间的数

    * 产生10个随机数5-9之间 统计一个int类型的一维数组中有多少个在[min,max]之间的数 */ import java.util.*; public class Demo{ public s ...

  7. python: ImportError:DLL load failed 解决方法。

    在学习使用wordcloud 库创建词云过程中,mooc里提到可以使用另一个库函数,来创建不同形状的词云. 就是这句: ... from scipy.misc import imread mk = i ...

  8. PythonStudy——赋值运算符 Assignment operator

    eg: num = 10 num += 1 # 等价于 num = num + 1 => 11 print(num) 特殊操作: 1.链式赋值 a = b = num print(a, b, n ...

  9. 账户和联系人 Accounts and Contacts 译

    原文链接: https://crmbook.powerobjects.com/basics/searching-and-navigation/understanding-accounts-and-co ...

  10. 3、PHP中常用的数据库操作函数解析

    mysql_connect  连接数据库 mysql_select_db 选择需要操作的数据库 mysql_query 执行数据库操作语句 mysql_fetch_array 以数组的形式返回每行查询 ...