LeetCode——Product of Array Except Self
Description:
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.)
public class Solution {
public int[] productExceptSelf(int[] nums) {
int[] res = new int[nums.length];
res[nums.length - 1] = 1;
for(int i=res.length-2; i>=0; i--) {
res[i] = nums[i+1] * res[i+1];
}
int l = 1;
for(int i=0; i<nums.length; i++) {
res[i] *= l;
l *= nums[i];
}
return res;
}
}
LeetCode——Product of Array Except Self的更多相关文章
- [LeetCode] 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 ...
- 238. [LeetCode] 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 -- Product of Array Except Self My Submissions Question
Question: Given an array of n integers where n > 1, nums, return an array output such that output ...
- LeetCode: Product of Array Except Self
Dynamic Programming public class Solution { public int[] productExceptSelf(int[] nums) { int[] ans = ...
- LeetCode Product of Array Except Self (除自身外序列之积)
题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- 【LeetCode】Product of Array Except Self
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...
- 【LeetCode】238. Product of Array Except Self
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...
随机推荐
- 设计模式-观察者模式(上)<转>
本文参考Head First设计模式一书,感觉书中的例子实在很好,很贴切.对模式的知识点进行总结,并对书的源码做了一定注释. 观察者模式要点有二:主题和观察者. 最贴切的案例是:杂志订阅,杂志是主 ...
- uboot中bootargs实现
setup.h通过宏定义实现了bootargs传递参数到内核,值得以后编程学习. include/asm-arm/setup.h 14 * NOTE: 15 * This file contai ...
- Entity Framework应用:使用LINQ操作
一.什么是LINQ TO EntitiesLINQ,全称是Language-INtegrated Query(集成语言查询),是.NET语言中查询数据的一种技术.LINQ to Entities是一种 ...
- Hibernate- HQL查询方式
HQL查询方式 01.基本查询 02.动态实例查询 03.分页查询 04.条件查询 05.连接查询 06.子查询
- Beego 框架学习(一)
1.特性 beego是一个http框架 高性能,是目前最快的的go框架 开发快捷,组件多,高度解耦 RESTful方式,可以自定义action 文档完整 智能路由.智能监控 2.安装 go get g ...
- EasyUI项目中的自定义JS
自定义方法: (function($) { $.extend($, { //获取下标,删除时使用 getArrayIndex : function (array,value) { var index ...
- 在使用R做数据挖掘时,最常用的数据结构莫过于dataframe了,下面列出几种常见的dataframe的操作方法
原网址 http://blog.sina.com.cn/s/blog_6bb07f83010152z0.html 在使用R做数据挖掘时,最常用的数据结构莫过于dataframe了,下面列出几种常见的d ...
- 免费 web api 接口大全
下面的接口来自互联网,部分功能需要付费 查询手机 http://www.yodao.com/s-martresult-xml/search.s?type=mobile&q= 手机号码 查询 I ...
- 关于Cocos2d-x中父子节点的互动
1.子节点可以通过this->getParent()来获得相应的父节点,并且进行强制类型转换. ((Scene*)this->getParent())->getPhysicsWorl ...
- 【转】【MySql】Update批量更新与批量更新多条记录的不同值实现方法
批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...