238 Product of Array Except Self 除自身以外数组的乘积
一个长度为 n 的整形数组nums,其中 n > 1,返回一个数组 output ,其中 output[i] 等于nums中除nums[i]以外所有元素的乘积。
不用除法 且在O(n)内解决这个问题。
例如,输入 [1,2,3,4],返回 [24,12,8,6]。
进阶:
你可以在常数空间复杂度内解决这个问题吗?(注意:出于空间复杂度分析的目的,输出数组不被视为额外空间。)
详见:https://leetcode.com/problems/product-of-array-except-self/description/
Java实现:
class Solution {
public int[] productExceptSelf(int[] nums) {
int n=nums.length;
int[] f=new int[n];
int[] b=new int[n];
int[] r=new int[n];
Arrays.fill(f,1);
Arrays.fill(b,1);
for(int i=0;i<n-1;++i){
f[i+1]=f[i]*nums[i];
}
for(int i=n-1;i>0;--i){
b[i-1]=b[i]*nums[i];
}
for(int i=0;i<n;++i){
r[i]=f[i]*b[i];
}
return r;
}
}
C++实现:
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
vector<int> f(n,1),b(n,1),res(n);
for(int i=0;i<n-1;++i)
{
f[i+1]=f[i]*nums[i];
}
for(int i=n-1;i>0;--i)
{
b[i-1]=b[i]*nums[i];
}
for(int i=0;i<n;++i)
{
res[i]=f[i]*b[i];
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/4650187.html
238 Product of Array Except Self 除自身以外数组的乘积的更多相关文章
- 238. Product of Array Except Self除自身以外数组的乘积
网址:https://leetcode.com/problems/product-of-array-except-self/ 参考:https://leetcode.com/problems/prod ...
- Leetcode238. Product of Array Except Self除自身以外数组的乘积
给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1 ...
- 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
Product of Array Except Self Given an array nums of n integers where n > 1, return an array outpu ...
随机推荐
- Codeforces Round #457 (Div. 2) B
B. Jamie and Binary Sequence (changed after round) time limit per test 2 seconds memory limit per te ...
- React Native学习(十)—— 生命周期
本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...
- 深入理解hadoop(一)
hadoop 前世今生 hadoop最早起源于开源收缩引擎nutch,由dong cutting 贡献,但由于nutch最初的设计不能解决数10亿级别的文件存储和索引而遇到了严重的可扩展性问题,直到 ...
- Maximum Product Subarray(最大连续乘积子序列)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 创建简单的spring-mvc项目
1.第一步:创建项目 new—>Dynamic Web Project 项目创建成功后,展示如图: 2.第二步:导入springmvc的jar包和common-logging.jar 3.第三步 ...
- Eclipse-Java代码规范和质量检查插件-SonarLint
SonarQube(Sonar)之前的提供的本地工具是需要依赖SonarQube服务器的,这样导致其运行速度缓慢. 新出的SonarLint的扫描引擎直接安装在本地,速度超快,实时探测代码技术债务,给 ...
- DHCP Snooping的实现
DHCP Snooping的实现 DHCP Snooping的实现 主要作用:1.防止在动态获得IP地址的网络环境中用户手动配置PC的IP地址;2.防止A用户的PC静态配置的IP地址顶掉B用户PC动态 ...
- Windows 7 SID 修改
在安裝Windows系統時會產生一個獨一無二的SID (Security ID),它用來識別每一部主機,若在同一個區域網路內有兩部相同SID的主機,會出現警告訊息.一般而言,每次安裝時的SID不可能會 ...
- Chromium硬件加速渲染的UI合成过程分析
在Chromium中.Render端和WebGL端绘制出来的UI终于是通过Browser端显示在屏幕上的.换句话说.就是Browser端负责合成Render端和WebGL端的UI.这涉及到不同Open ...
- Jenkins安装与使用
一.Jenkins简介 Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作 二.下载与安装 下载地址 ...