Product of Array Except Self
来源:互联网 发布:libgcc s.so.1 ubuntu 编辑:程序博客网 时间:2024/06/14 07:57
题目地址:https://leetcode.com/problems/product-of-array-except-self/#/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 ProductofArrayExceptSelf { public static int[] productExceptSelf(int[] nums) { if (nums == null || nums.length <= 1) return nums; int temp = 1; for (int i = 0; i < nums.length; i++) { temp *= nums[i]; } for (int i = 0; i < nums.length; i++) { if (nums[i] != 0) nums[i] = temp / nums[i]; else nums[i] = 0; } return nums; }}
如果不用除法,时间复杂度先不要保证
先将结果集初始化:
其中
res
的长度与nums
的长度相等,然后把nums
中的元素依次与res
中的元素做乘法,遇到下标相同的则略过,于是这样写代码的时间复杂度为public class ProductofArrayExceptSelf { public static int[] productExceptSelfII(int[] nums) { if (nums == null || nums.length <= 1) return nums; int[] result = new int[nums.length]; for (int i = 0; i < nums.length; i++) { result[i] = 1; } for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums.length; j++) { if (i == j) continue; else result[j] *= nums[i]; } } return result; }}
现在题目的要求是既要没有除法,又要
这个数组还是比较好构建的,而且构建这个数组的时间复杂度为
对,我们重新迭代一次nums
,从后到前再乘一次把缺少的补乘上不就可以了么。这个搞就是做了两次
public class ProductofArrayExceptSelf { public static int[] productExceptSelfIII(int[] nums) { if (nums == null || nums.length <= 1) return nums; int[] result = new int[nums.length]; result[0] = 1; for (int i = 1; i < nums.length; i++) { result[i] = result[i - 1] * nums[i - 1]; } int temp = 1; for (int i = nums.length - 1; i >= 0; i--) { result[i] *= temp; temp *= nums[i]; } return result; } public static void main(String[] args) { int[] nums1 = {1,2,3,4}; int[] r1 = productExceptSelf(nums1); int[] nums2 = {1,2,3,4}; int[] r2 = productExceptSelfII(nums2); int[] nums3 = {1,2,3,4}; int[] r3 = productExceptSelfIII(nums3); return ; }}
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- Product of Array Except Self
- 设计模式简介
- Deep Learning(深度学习)学习笔记整理系列之(一)
- Mysql脚本插入自动增长的数据
- KNN,k-近邻算法
- 欢迎使用Markdown编辑器写博客
- Product of Array Except Self
- 线程中的乐观与悲观锁的区别
- 对java集合不问底层实现的浅理解
- 使用MD5加密方式对登录密码加密(小demo)
- MongoDB数据库学习
- ffmpeg api升级到3.3 api变化
- 不重新编译PHP为PHP安装zlib扩展-学习收集
- s5pv210——定时器的知识和代码操作
- opencv 从putText()中获取像素的大小