LeetCode——Product of Array Except Self
2024-08-28 17:42:50
Description:
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[] res = new int[nums.length];
res[nums.length - 1] = 1;
for(int i=res.length-2; i>=0; i--) {
res[i] = nums[i+1] * res[i+1];
} int l = 1;
for(int i=0; i<nums.length; i++) {
res[i] *= l;
l *= nums[i];
} return res;
}
}
最新文章
- Linux 驱动学习笔记05--字符驱动实例,实现一个共享内存设备的驱动
- SpringMvc xml 配置
- [CF442C] Artem and Array (贪心+单调栈优化)
- url中文参数解决方案
- 1190: [HNOI2007]梦幻岛宝珠 - BZOJ
- Android PagerAdapter
- MVC5学习笔记
- ZOJ 3872 Beauty of Array DP 15年浙江省赛D题
- ubuntu下mysql二进制包安装
- IIS7出现“HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。”错误-Windows-
- hibernate动态切换数据源
- Linux 重启命令
- Android Studio设置字体和主题
- ReactiveSwift源码解析(三) Signal代码的基本实现
- ASP.NET Web API决跨域问题
- ERROR:scala:Error:Object scala.runtime in compiler mirror not found
- flask+mako+peewee(上)
- Why there is two completely different version of Reverse for List and IEnumerable?
- 百度搜索引擎取真实地址-python代码
- js中对String去空格