C#解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 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的更多相关文章
- 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 ...
- 剑指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] * ...
- [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 ...
- 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 ...
- (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 ...
- 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] ...
- [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 ...
- leetcode 238 Product of Array Except Self
这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...
- Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法
1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...
随机推荐
- IIC 概述之1
概述: I²C 是Inter-Integrated Circuit的缩写,发音为"eye-squared cee" or "eye-two-cee" , 它是一 ...
- IOS--UISwitch的使用方法
IOS--UISwitch的使用方法详细 (2013-08-24 11:09:38) 转载▼ 标签: uiswitch switch 选择控件 ios it 分类: iOS--UI // UISwit ...
- iOS中MVC等设计模式详解
iOS中MVC等设计模式详解 在iOS编程,利用设计模式可以大大提高你的开发效率,虽然在编写代码之初你需要花费较大时间把各种业务逻辑封装起来.(事实证明这是值得的!) 模型-视图-控制器(MVC)设计 ...
- jQuery.dialog
本篇文章主要是对JQUERY中dialog的用法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助 今天用到了客户端的对话框,把 jQuery UI 中的对话框学习了一下. 准备 jQ ...
- Task schedule
hdu4907:http://acm.hdu.edu.cn/showproblem.php?pid=4907 题意:中文题. 题解:这一道水题,自己调了很久,并且没有注意到序列可能是乱序的,wa了好几 ...
- SOC
http://marsvaadin.iteye.com/blog/1311438 http://blog.csdn.net/cxxsoft/article/details/12610641
- Android中SharedPreferences使用方法介绍
一.Android SharedPreferences的简介 SharedPreferences是一种轻型的Android数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用 ...
- Hibernate 配置详解(5)
9) hibernate.batch_fetch_style: 该配置是hibernate4.2.0新添加的,使用这个设置可以配置hibernate在做batch-fetch的时候,生成SQL的策略. ...
- javascript isNaN
因为以前felx组件默认是-99999999,后来因为方便组件和数据库的操作,就统一修改Number类型的数据为NAN类型了,然后通过isNaN去判断,而然很多表达式是返回字符串的或者null,要注意 ...
- 数学(矩阵乘法):HDU 4565 So Easy!
So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...