Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].

Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)

代码的思路是:给定一个数组a,求数组a的第i位置的除自身之外的元素的乘积,可以将先求出数组a的第1个位置到第i-1个位置的乘积,存到数组b中,在求出第i+1个元素到第n个元素的乘积

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

C#解leetcode 238. Product of Array Except Self的更多相关文章

  1. LN : leetcode 238 Product of Array Except Self

    lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...

  2. 剑指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] * ...

  3. [LeetCode] 238. Product of Array Except Self 除本身之外的数组之积

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

  4. LeetCode 238. Product of Array Except Self (去除自己的数组之积)

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

  5. (medium)LeetCode 238.Product of Array Except Self

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

  6. Java [Leetcode 238]Product of Array Except Self

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

  7. [leetcode]238. Product of Array Except Self除了自身以外的数组元素乘积

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

  8. leetcode 238 Product of Array Except Self

    这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...

  9. Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法

    1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...

随机推荐

  1. QLineEdit

    The QLineEdit widget is a one-line text editor. Header: #include <QLineEdit> qmake: QT += widg ...

  2. run_command函数分析

    一.概述 位置:common/main.c 功能:根据传入参数(命令),在命令存储区(.u_boot_cmd)中查找对应的命令,找到命令并调用对应的函数执行 流程: 二.分析 1.函数说明信息 /** ...

  3. 转 四大Java EE容器(Tomcat、JBoss、Resin、Glassfish)之简单比较

                                      现在流行的Java EE容器有很多:Tomcat.JBoss.Resin.Glassfish等等.下面对这四种Java EE容器进行 ...

  4. [BZOJ 1084] [SCOI2005] 最大子矩阵 【DP】

    题目链接:BZOJ - 1084 题目分析 我看的是神犇BLADEVIL的题解. 1)对于 m = 1 的情况, 首先可能不取 Map[i][1],先 f[i][k] = f[i - 1][k];   ...

  5. Qt如何去掉按钮等控件的虚线框(焦点框)(三种办法)

    方法1:可以通过代码ui->pushButton->setFocusPolicy(Qt::NoFocus)或在Qt Creator的属性列表中设置. 方法2:如果在嵌入式设备中需要通过按键 ...

  6. AOP举例子

    切面类TestAspect package com.spring.aop; /** * 切面 * */ public class TestAspect { public void doAfter(Jo ...

  7. 【Linux】鸟哥的Linux私房菜基础学习篇整理(十)

    1. at [-mldv] TIME/at -c 工作号码:单一工作调度.参数:-m:当at的工作完成后,即使没有输出信息,以email通知用户该工作已完成:-l:相当于atq,列出目前系统上面的所有 ...

  8. Unity GUI编程

    脚本语言:C# 附上一张图说明Unity GUI编程中可用的控件:(可能有遗漏) 下面列出一些例子来说明: 1.Groups : 在固定Layout模式中起到组织可用项的功能,它让你在屏幕的一个区域中 ...

  9. 【转】android中重复连接ble设备导致的连接后直接返回STATE_DISCONNECTED的解决办法---不错不错,重新连接需要花费很长的时间

    原文网址:http://bbs.eeworld.com.cn/thread-438571-1-1.html /*                         * 通过使用if(gatt==null ...

  10. 【动态规划】Vijos P1143 三取方格数(NOIP2000提高组)

    题目链接: https://vijos.org/p/1143 题目大意: NxN的矩阵,每个值只能取一次,从(1,1)走到(n,n)走三次能取得的最大值. 题目思路: [动态规划] f[x1][y1] ...