Leetcode no. 238
2016-04-07 18:04
274 查看
238. Product of Array Except Self
Given an array of n integers where n > 1,
return an array
equal to the product of all the elements of
Solve it without division and in O(n).
For example, given
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.)
Given an array of n integers where n > 1,
nums,
return an array
outputsuch that
output[i]is
equal to the product of all the elements of
numsexcept
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.)
Simple Java solution in O(n) without extra space
public class Solution { public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] res = new int ; res[0] = 1; for (int i = 1; i < n; i++) { res[i] = res[i - 1] * nums[i - 1]; } int right = 1; for (int i = n - 1; i >= 0; i--) { res[i] *= right; right *= nums[i]; } return res; } }
相关文章推荐
- 防火墙的局限与入侵检测系统的特征
- Linux网络编程必看书籍推荐
- HashMap
- jsp统计网站的访问次数
- 基于halcon工业图像处理——初学
- 【Java 基础】Java 泛型简单入门
- 企业级开发账号In House ipa发布流程
- scikit-learn——Classification
- 【学习笔记javascript设计模式与开发实践(状态模式)----16】
- 郑州轻工业OJ 1097: 计算平均成绩(函数专题)//又一个gets()
- Matting学习
- Android apk程序调用其它的APK程序
- salesforce 零基础开发入门学习(十一)sObject及Schema深入
- 数据库分库分表
- iOS - 观察者模式
- Android——ArrayList 、LinkList、List 区别 & 迭代器iterator的使用 & HashMap、Hashtable、LinkedHashMap、TreeMap
- 将某目录文件打成tar包
- 关于控件Controls随Dialog改变大小 && CDialogResize的总结
- mysql存储过程循环修改每一条数据
- Understanding Convolutional Neural Networks for NLP