Product of Array Except Self
2015-09-12 13:46
543 查看
Given an array of n integers where n > 1,
Solve it without division and in O(n).
For example, given
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].
public class Solution { public int[] productExceptSelf(int[] nums) { int size = nums.length; int[] front = new int[size]; int[] re = new int[size]; front[0] = 1; for(int i=1;i<size;i++) { front[i] = front[i-1]*nums[i-1]; } int mutiply = 1; for(int i=size-1;i>=0;i--) { re[i] = front[i]*mutiply; mutiply = mutiply*nums[i]; } return re; } }
相关文章推荐
- 高性能JavaScript--加载和执行(简要学习笔记一)
- java小程序:输入五个整数顺序输出,再输入一个顺序输出
- CENTOS 6.x/7.x yum安装配置Nginx(最新版)
- java__线程通信02
- 判断两圆的位置关系
- Spark第一个研究笔记1一片 - Spark一个简短的引论
- asp.net缓存使用介绍
- Remove Duplicates from Sorted Array
- Spring 向页面传值以及接受页面传过来的参数的方式
- php strcasecmp()与strncasecmp()函数
- GB2312和UTF-8
- LeetCode_merge-two-sorted-lists
- 数据库锁
- 常用的PHPStorm快捷键
- 匿名对象、string拼接、stringBuffer
- 数据库锁
- IMSDroid遇到注册问题(蘼1S 计3等一下 Android4.4)
- 自定义控件使用DrawableLeft/Right图片和文字同时居中
- 12个免费学习编程的网站
- 后台判断用户输入的值是否为数值的几种方法: