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之外的全 ...
随机推荐
- Objective-c之NSCopying
Objective-c之NSCopying copy的原理: 执行<NSCopying>协议,类中必须实现copyWithZone:方法响应的copy消息. copy消息将发送co ...
- 如何完美打造Win8 Metro版IE10浏览器页面(转)
Windows8 内置两种 Internet Explorer 10 (以下简称 IE10),一个是在桌面环境下使用的 IE10:视窗操作.可以支持各种插件(ActiveX):而另外一个则是在新的开始 ...
- 完美世界-2015校园招聘-java服务器工程师-成都站
给定一个整数,将该整数分解成多个2的幂次方相加的形式,每次都取最大的可以分解出来的2的幂次方 比如 65 64 1 1 1 2 2 package wanmanshijie; import java. ...
- +=与join的性能测试
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- gridview checkbox从服务器端和客户端两个方面实现全选和反选
GridView中的checkbox的全选和反选在很多的地方都是要求实现的,所以下面就从服务器端和客户端两个方面实现了checkbox的选择,感兴趣的朋友可以了解下,希望本文对你有所帮助 GridVi ...
- 【转】nodejs
Node.JS + MongoDB技术讲座 云计算 + 大数据 = 未来. 在中国的云计算上基本上是一个概念,个人感觉与当初的SOA没有太大的区别,空泛的理论. 中小型开发的未来 ...
- JavaScript Client-Side
JavaScript Client-Side GET, POST Events, Elements Template, Event, Data(MVC) XMLHttpRequest Logic (f ...
- Sphnix创建文档
http://www.ibm.com/developerworks/cn/opensource/os-sphinx-documentation/ Oracle实例名和服务名
- network: Android 网络判断(wifi、3G与其他)
package mark.zeng; import Java.util.List; import Android.content.Context; import android.location.Lo ...
- curl post传递json数据
有时想在命令行使用post http提交一个表单,比较常用的是POST模式和GET模式 GET模式什么option都不用,只需要把变量写在url里面就可以了 比如:curl http://www.wa ...