Product of Array Except Self
来源:互联网 发布:类似快看小说的软件 编辑:程序博客网 时间:2024/06/05 04:35
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 static void main(String[] args) {// TODO Auto-generated method stubint[] nums = {1,0,3,4};int[] res = Solution.productExceptSelf(nums);for(int x : res)System.out.print(x+" ");} public static int[] productExceptSelf(int[] nums) { int numsLen = nums.length; int[] res = new int[numsLen]; res[0] = 1; for(int i = 1 ;i < numsLen ;i++)//先从左向右乘,每一位对前几位数进行累乘保存到res中 { res[i] = res[i-1]*nums[i-1]; } int right = 1; for(int i = numsLen-1;i>=0 ;i--)//从右向左,每一位对后几位进行累乘。 { res[i] *= right; right *= nums[i]; } return res; }}
0 0
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- 【opencv练习07 - 滤波器(锐化)】
- Nginx-----反向代理Google
- DNS修改即时生效
- ViewDragHelper学习
- 机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
- Product of Array Except Self
- JavaScript几种函数声明方式的区别
- android ListView 几个重要属性
- android SystemServer进程的作用
- LA 3263 That Nice Euler Circuit 好看的一笔画 几何问题
- MyBatis配置文件标签
- Android 面试
- 红黑树算法简要介绍
- [AngularJS面面观] 23. 依赖注入 --- Factory vs Service