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]
.
public class Solution { //左边的数乘积乘以右边的数乘积。
public int[] productExceptSelf(int[] nums) {
int[] res=new int[nums.length];
res[0]=1;
int acc=1;
for(int i=1;i<nums.length;i++){
res[i]=res[i-1]*nums[i-1];
}
for(int i=nums.length-1;i>=0;i--){
res[i]*=acc;
acc*=nums[i];
}
return res;
}
}
(Skill)238. Product of Array Except Self的更多相关文章
-
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 ...
-
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 ...
-
leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
-
【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:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...
-
[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 ...
-
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 ...
随机推荐
-
Android开发学习之路-Android中使用RxJava
RxJava的核心内容很简单,就是进行异步操作.类似于Handler和AsyncTask的功能,但是在代码结构上不同. RxJava使用了观察者模式和建造者模式中的链式调用(类似于C#的LINQ). ...
-
Div 定时移动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
-
C++ const用法 尽可能使用const [转载]
C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不变的.如果在编程中确实有某个值保持不变,就应该明确使用const,这样可以获得编译器的帮助. 1.c ...
-
HDU1978 记忆化搜索
How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
-
Wide character in print at a2.pl line 6.
jrhapt01:/home/tomcat/test> cat a2.pl my $str="$ARGV[0]"; use Encode; use URI::Escape; ...
-
C++ Primer 5th 第12章 动态内存
练习12.1:在此代码的结尾,b1 和 b2 各包含多少个元素? StrBlob b1; { StrBlob b2 = {"a", "an", "th ...
-
ifconfig命令详解
linux下网上命名规律:eth0,eth1.第一块以太网卡,第二块.lo为环回接口,它的IP地址固定为127.0.0.1,掩码8位.它代表你的机器本身. 1.ifconfig是查看网卡的信息 eth ...
-
Linux系统下C语言如何调用scalapack中的函数
在并行计算中经常需要调用scalapck(并行化的lapack)函数库里面的函数进行编程,这里简单介绍在C语言如何调用scalapck中的矩阵向量乘的函数. 注意:scalapack中的函数是用for ...
-
HDU5339——Untitled
Problem Description There is an integer a and n integers b1,…,bn. After selecting some numbers from ...
-
【Unity]】AR小工具-Vuforia
很有意思的增强现实玩具,六分钟应用. https://www.youtube.com/watch?v=khavGQ7Dy3c