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. Altera FPGA SoC搭建步骤

    Altera SoC 官方搭建指南: https://rocketboards.org/foswiki/Documentation/EmbeddedLinuxBeginnerSGuide 官方文档中除 ...

  2. CSS 文字概念小记

    1.水平居中: 更多的是指宽度的居中,margin: 0 auto; 2.垂直居中: 是指高度的居中 PS:这个两个慨念我老是搞混,今天记录一下,防止下次又忘了

  3. MySQL设置空密码

    因为刚安装的时候,MySQL强制设置密码,但是我需要设置MySQL为空密码 语句: ';

  4. 新建一个self hosted Owin+ SignalR Project(2)

    ASPNET SignalR是为ASP.NET开发人员提供的一个库,可以简化开发人员将实时Web功能添加到应用程序的过程.实时Web功能是指这样一种功能:当所连接的客户端变得可用时服务器代码可以立即向 ...

  5. Homebrew简介及安装,Mac 包管理

    Homebrew简介及安装 Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 Linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两 ...

  6. Python学习笔记第二十七周(Bootstrap)

    目录: 全局样式 一.栅格系统 二.表单 三.按钮  四.导航 五.按钮组 六.面板 七.表格 八.分页 九.排版 十.图片 十一.辅助类 十二.响应式工具 组件 内容: 前言: 首先通过https: ...

  7. IDEA控制台乱码解决

    打开Intellij的安装的bin目录(D:\Program Files\JetBrains\IntelliJ IDEA 14.0\bin ),找到上图的两个文件(根据你的系统是32位或64位选择其中 ...

  8. SpringMVC + MyBatis分库分表方案

    mybatis作为流行的ORM框架,项目实际使用过程中可能会遇到分库分表的场景.mybatis在分表,甚至是同主机下的分库都可以说是完美支持的,只需要将表名或者库名作为动态参数组装sql就能够完成.但 ...

  9. 戴尔R710服务器安装系统——配置raid

    一,内存二,硬盘(分区,数据量大小)三,电源线,网络线四,raid(raid0,raid1,raid5) 从这里开始 1.进入系统时不用管,默认进入即可 2.在读完内存消息之后,开始读取磁盘消息,在出 ...

  10. MySQL Error--The Table is full

    问题描述 在MySQL 错误日志中发下以下错误信息:[ERROR] /export/servers/mysql/bin/mysqld: The table '#sql-xxxx-xxx' is ful ...