leetcode 238. Product of Array Except Self 乘法 + DP动态规划
来源:互联网 发布:yap-yum的体位图 编辑:程序博客网 时间:2024/06/18 15:00
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.)
这道题采用最直接的方法挺不错的。
代码如下:
import java.util.Arrays;public class Solution { /* * 分析: * 1、如果数组中有两个及以上的0,那么结果数组的每一项都为0. * 2、如果数组中只有一个0,那么结果数组中,0对应的位置,为数组中其他元素的成绩,其余位置都为0。 * 3、如果数组中没有0,那么结果数组每一项均为数组所有元素的乘积/当前数组元素。 * * 需要扫描数组两次,算法时间复杂度为O(n): * 第一次:计算0的个数,以及非0元素的乘积。 * 第二次,计算结果数组每一位的元素。 * 需要两个临时变量: product保存非0元素的乘积,zeroCount计算0元素的个数,空间复杂度为O(1)。 * * */ public int[] productExceptSelfByDevide(int[] nums) { if(nums==null || nums.length<=1) return nums; int []res=new int[nums.length]; Arrays.fill(res, 0); int count=0 , pro=1; for(int i=0;i<nums.length;i++) { if(nums[i]==0) count++; else pro*=nums[i]; } if(count>=2) return res; else if(count==1) { for(int i=0;i<nums.length;i++) { if(nums[i]==0) res[i]=pro; else res[i]=0; } }else { for(int i=0;i<nums.length;i++) res[i]=(int)(pro/nums[i]); } return res; } /* * 好吧,题目中要求不能用除法。上述算法还是用了除法。另一种不用除法的解法如下: * 1)分别计算两个数组,left、right,left[i]为nums[i]的左边元素的成绩, * right[i]为nums[i]右边元素的乘积。 * * 2)result[i] = left[i] * right[i] * */ public int[] productExceptSelfByDP(int[] nums) { if(nums==null || nums.length<=1) return nums; int []res=new int[nums.length]; Arrays.fill(res, 0); int []left=new int[nums.length]; int []right=new int[nums.length]; Arrays.fill(left, 1); Arrays.fill(right, 1); for(int i=1;i<nums.length;i++) left[i]=left[i-1]*nums[i-1]; for(int i=nums.length-2;i>=0;i--) right[i]=right[i+1]*nums[i+1]; for(int i=0;i<nums.length;i++) res[i]=left[i]*right[i]; return res; } /* * 这样算法的时间复杂度为O(n),但算法空间复杂度也为O(n), * 不满足空间O(1)的需求. * * 题目中提到 返回的结果不计空间,所以直接在result中保存left值。 * right 值用变量代替,空间优化后的代码如下: * * */ public int[] productExceptSelf(int[] nums) { if(nums==null || nums.length<=1) return nums; int []res=new int[nums.length]; Arrays.fill(res, 1); for(int i=1;i<nums.length;i++) res[i]=res[i-1]*nums[i-1]; int right=1; for(int i=nums.length-2;i>=0;i--) { right=right*nums[i+1]; res[i]=res[i]*right; } return res; }}
下面是C++的做法,就是一个简单的DP动态规划的做法,嗯嗯很棒,其实直接做乘法然后计算是最简单的,最容易想到的
代码如下:
#include <iostream>#include <alogrithm>#include <set>#include <map>#include <vector>#include <stack>#include <queue>using namespace std;class Solution {public: vector<int> productExceptSelf(vector<int>& nums) { vector<int> res(nums.size(), 1); vector<int> left(nums.size(), 1); vector<int> right(nums.size(), 1); for (int i = 1; i < nums.size(); i++) left[i] = left[i - 1] * nums[i - 1]; for (int i = nums.size() - 2; i >= 0; i--) right[i] = right[i + 1] * nums[i + 1]; for (int i = 0; i < nums.size(); i++) res[i] = left[i] * right[i]; return res; }};
阅读全文
0 0
- leetcode 238. Product of Array Except Self 乘法 + DP动态规划
- [leetcode] 238.Product of Array Except Self
- [LeetCode]238.Product of Array Except Self
- [leetcode] 238. Product of Array Except Self
- Leetcode 238. Product of Array Except Self
- [LeetCode]238. Product of Array Except Self
- 238. Product of Array Except Self LeetCode
- LeetCode 238. Product of Array Except Self
- [LeetCode]238. Product of Array Except Self
- LeetCode *** 238. Product of Array Except Self
- LeetCode 238. Product of Array Except Self
- 【leetcode】238. Product of Array Except Self
- leetcode-238. Product of Array Except Self
- Leetcode 238. Product of Array Except Self
- 【leetcode】238. Product of Array Except Self
- LeetCode- 238. Product of Array Except Self
- leetcode 238. Product of Array Except Self
- 【leetcode】238. Product of Array Except Self
- MABAITS 实现批量插入
- 19翻转二叉树python
- linux安装ruby
- C++选择排序
- Cordova 入门之环境配置 (Android)(一)
- leetcode 238. Product of Array Except Self 乘法 + DP动态规划
- DA14580 官方SDK详解
- 归并、快速和堆排序总结
- xutils3.0网络请求的关键代码 (这么挨着写是为了区分和2.0之间的区别)
- HDOJ2005
- Photon_PhotonServer安装_009
- 最简单的js判断密码强度
- Ingress使用详解
- 9.22