Java [Leetcode 238]Product of Array Except Self
题目描述:
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 n = nums.length;
int[] res = new int[n];
for(int i = 0, temp = 1; i < n; i++){
res[i] = temp;
temp *= nums[i];
}
for(int i = n - 1, temp = 1; i >= 0; i--){
res[i] *= temp;
temp *= nums[i];
}
return res;
}
}
Java [Leetcode 238]Product of Array Except Self的更多相关文章
- 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 ...
- 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)
剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...
- [LeetCode] 238. 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 238. 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 ...
- (medium)LeetCode 238.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 ...
- C#解leetcode 238. 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 ...
- [leetcode]238. 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 238 Product of Array Except Self
这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...
- Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法
1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...
随机推荐
- cookie中转注入实战
随着网络安全技术的发展,SQL注入作为一种很流行的攻击方式被越来越多的人所知晓.很多网站也都对SQL注入做了防护,许多网站管理员的做法就是添加一个防注入程序.这时我们用常规的手段去探测网站的SQL注入 ...
- Apache代理和反向代理
服务器上安装了多个服务,包括apache的80端口,以及tomcat的8080和8090,为了访问使用方便,尝试了代理和反向代理.下面是部分配置以备参考: NameVirtualHost *:80 & ...
- 同一机器 部署 两个 jboss
当jboss和oracle在同一机器上时,通常oracle占用8080端口,这时只需要去修改\deploy\jbossweb-tomcat50.sar\server.xml中.当在同一台机器上运行两个 ...
- Java学习第二篇:类,对象,成员属性,成员方法,构造方法,类变量,类方法
一.类的定义 一个全面的类定义是比较复杂的, 定义如下:
- jstl删除session,choose,动态获取request当前工程路径
1.jstl标签c:remove删除session request.getSession().setAttribute("ssmsg", "修改成功"); &l ...
- POJ 1702
#include <iostream> #include <cmath> #define MAXN 100 using namespace std; void fun(int ...
- windows下eclipse远程连接hadoop错误“Exception in thread"main"java.io.IOException: Call to Master.Hadoop/172.20.145.22:9000 failed ”
在VMware虚拟机下搭建了hadoop集群,ubuntu-12.04,一台master,三台slave.hadoop-0.20.2版本.在 master机器上利用eclipse-3.3连接hadoo ...
- hdu1102
http://acm.hdu.edu.cn/showproblem.php?pid=1102 最小生成树(模板题) 3 0 990 692 990 0 179 692 179 0 1 1 2 一共3个 ...
- 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【转】Dr.com 5.20破解教程
Dr.com 5.20破解教程 方法一 1.首先下载相关工具 Process Explorer(大家可以自行百度 一般绿色汉化版就可以)右键选择以管理员权限运行process的主程序 然后运行drc ...