[LC] 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 equal to the product of all the elements of nums except nums[i].
Example:
Input:[1,2,3,4]
Output:[24,12,8,6]
Note: Please solve it without division and in O(n).
Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
class Solution {
public int[] productExceptSelf(int[] nums) {
if (nums == null || nums.length == 0) {
return nums;
}
int[] resArr = new int[nums.length];
resArr[0] = 1;
for (int i = 1; i < nums.length; i++) {
resArr[i] = resArr[i - 1] * nums[i - 1];
}
int right = 1;
for (int i = nums.length - 1; i >= 0; i--) {
resArr[i] *= right;
right *= nums[i];
}
return resArr;
}
}
public class Solution {
/**
* @param nums: an array of integers
* @return: the product of all the elements of nums except nums[i].
*/
public int[] productExceptSelf(int[] nums) {
// write your code here
int len = nums.length;
int[] prefix = new int[len];
int[] suffix = new int[len];
for (int i = 0; i < len; i++) {
if (i == 0) {
prefix[i] = nums[i];
continue;
}
prefix[i] = prefix[i - 1] * nums[i];
}
for(int i = len - 1; i >= 0; i--) {
if (i == len - 1) {
suffix[i] = nums[i];
continue;
}
suffix[i] = suffix[i + 1] * nums[i];
}
int[] res = new int[len];
for (int i = 0; i < len; i++) {
if (i == 0) {
res[i] = suffix[i + 1];
continue;
}
if (i == len - 1) {
res[i] = prefix[i - 1];
continue;
}
res[i] = prefix[i - 1] * suffix[i + 1];
}
return res;
}
}
[LC] 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 ...
- 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 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
Product of Array Except Self Given an array nums of n integers where n > 1, return an array outpu ...
- 238. Product of Array Except Self 由非己元素形成的数组
[抄题]: Given an array of n integers where n > 1, nums, return an array output such that output[i] ...
- [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 ...
随机推荐
- 数据处理pandas
1.缺失值时间戳不为NaN,为NaT, 同样判断都为isna()或notna()方法2.删值\去重 df.dropna() df.drop_duplicates() 3.上下值插值 df.fillna ...
- 02-python-运算符与表达式
目录 1. 比较运算符 2. 算数运算符 3. 赋值运算符 4. 位于运算符 5. 逻辑运算符 6. 成员运算符 7. 身份运算符 8. 运算符优先级 9. 输出输入 10. 数字类型转换及常用数学方 ...
- git submodule update --init 和 --remote的区别
git 的submodule 工具方便第三方库的管理,比如gitlab 上的各种开源工具,spdlog等 在项目目录下创建.gitmodule 里可以添加第三方库,然后在更新第三方库时,有两个选项 g ...
- C++概要简介
从C到C++ 新类型 bool类型 新的输入输出方式 con cout 新的内存存储方式 new delete 引用& 用于传参 函数 内敛函数inline 通过代码区膨胀 减少函数的跳转时间 ...
- ZJNU 1528 - War--高级
类似于1213取水 可以把空投当作第0个城市 最后将0~n的所有城市跑最小生成树 /* Written By StelaYuri */ #include<iostream> #includ ...
- springmvc中那些易被忽略的小知识点
1.springmvc会为没有view的modelandview指定默认view 知道这个的时候我都惊呆了. 我从来都是手动指定view名字,今天看到别人写的代码竟然直接返回了个mav,貌似是在dis ...
- 如何选字体(font-family)
一.默认字体情况 1.Window下: 宋体(SimSun):Win下大部分游览器的默认字体,宋体在小字号下(如12px.14px)的显示效果还可以接受,但是字号一大就非常糟糕了,所以使用的时候要注意 ...
- 2017年3月25日工作日志:Jquery使用小结[绑定事件判断、select标签、军官证正则]
jQuery获取DOM绑定事件 在1.8.0版本之前,我们要想获取某个DOM绑定的事件处理程序可以这样: $.data(domObj,'events');//或者$('selector').data( ...
- 2019深圳Android千人开发者大会【NEW·无界】
报名地址:https://www.hdb.com/dis/mjcsegnslu 安卓巴士技术社区是中国领先的安卓开发者社区,现已聚集超过85万开发者,数年来一直致力于IT从业者的知识分享服务. 安卓巴 ...
- node 配置文件
# cat ~/.npmrc prefix=E:/Private/nodejs #registry=http://r.cnpmjs.org/ registry=http://registry.npm. ...