Product of Array Exclude Itself
Given an integers array A.
Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation.
For A = [1, 2, 3]
, return [6, 3, 2]
.
分析:
public class Solution {
public int[] productExceptSelf(int[] A) {
int[] left = new int[A.length];
int[] right = new int[A.length];
int[] result = new int[A.length]; for (int i = ; i < A.length; i++) {
left[i] = i == ? : left[i - ] * A[i - ];
right[A.length - - i] = (i == ) ? : right[A.length - i] * A[A.length - i];
} for (int i = ; i < A.length; i++) {
result[i] = left[i] * right[i];
}
return result;
}
}
public class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] res = new int[n];
res[] = ;
for (int i = ; i < n; i++) {
res[i] = res[i - ] * nums[i - ];
}
int right = ;
for (int i = n - ; i >= ; i--) {
res[i] *= right;
right *= nums[i];
}
return res;
}
}
one pass
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];
Arrays.fill(result, );
int left = , right = ;
for (int i = , j = nums.length - ; i < nums.length - ; i++, j--) {
left *= nums[i];
right *= nums[j];
result[i + ] *= left;
result[j - ] *= right;
}
return result;
}
}
Product of Array Exclude Itself的更多相关文章
- Lintcode: Product of Array Exclude Itself
Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B wi ...
- [LintCode] Product of Array Except Self 除本身之外的数组之积
Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...
- 【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 ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- 【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 ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 238. Product of Array Except Self(对O(n)和递归又有了新的理解)
238. Product of Array Except Self Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...
- leetcode:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...
- 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 ...
随机推荐
- iOS开发小技巧--初始化项目中修改APP安装后的名称
- Go to the first line OR the last line of the file
(1) 跳到首行 :1 或 gg (2)跳到最后一行 :$ 或 G 或shift+g(大写.当前若大小写锁定直接按g,未锁定则按shift+g)
- 【bzoj2115】 Xor
www.lydsy.com/JudgeOnline/problem.php?id=2115 (题目链接) 题意 给出一张图,可能有重边和自环,在图中找出一条从1-n的路径,使得经过的路径的权值的异或和 ...
- 总结css之内联图片的优缺点
会不会有这样一种感觉?IT技术开发知识面很广也很深,总会有你不懂得问题出现.一个接着一个新的问题,一个接着一个新的挑战. 今天,解读[内联图片],什么是内联图片,使用内联图片的优缺点是什么?这个问题是 ...
- Dancing Links初学记
记得原来备战OI的时候,WCX大神就研究过Dancing Links算法并写了一篇blog.后来我还写了个搜索策略的小文章( http://www.cnblogs.com/pdev/p/3952279 ...
- 洛谷P1755 斐波那契的拆分
题目背景 无 题目描述 已知任意一个正整数都可以拆分为若干个斐波纳契数,现在,让你求出n的拆分方法 输入输出格式 输入格式: 一个数t,表示有t组数据 接下来t行,每行一个数n(如题) 输出格式: t ...
- CSU 1115 最短的名字
传送门 Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description 在一个奇怪的 ...
- 多线程java代码移植到android&下载文本界面的更新
1)效果演示:
- Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
实现一个控制器ShirdrnController,如下所示: package org.shirdrn.spring.mvc; import java.util.Date; import javax.s ...
- MyEclipse------如何查询MySQL数据库里面表的信息
testExecuteQuary.jsp <%@ page language="java" import="java.util.*" pageEncodi ...