leetcode_middle_8_238. Product of Array Except Self
来源:互联网 发布:费洛蒙 牌子 知乎 编辑:程序博客网 时间:2024/06/10 15:53
题意:
给定一个长度大于1的数组,返回一个数组,要求返回的数组满足要求:数组的每个位的值等于给定数组除了这个位以外所有位的值的乘积。
分析:
由于题目的特性是除了本身,那么我们尽量让变化的是一个而不是n-1个。所以先取得所有数的乘积,除本身即可。然后发现逻辑的不严密,因为可能出现0,不能除0,针对这一问题进行分析:如果有很多0(多于一个)就输出的数组全是0了。如果只有一个0,除了本身是其他的乘积,其他都是0.
public class Solution { public int[] productExceptSelf(int[] nums) { int total = 1; int zero = 0; int t = -1; int[] output = new int[nums.length]; for(int i=0; i<nums.length; i++){ if(nums[i] == 0){ t = i; zero++; }else{ total *= nums[i]; } } if(zero == 1){ output[t] = total; }else if(zero == 0){ for(int i=0; i<nums.length; i++){ output[i] = total/nums[i]; } } return output; }}
0 0
- leetcode_middle_8_238. 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
- 召回率(Recall),精确率(Precision),平均正确率(Average_precision(AP) ),交除并(Intersection-over-Union(IoU))
- Sublime Text 3 快捷键总结
- centOS7.0 VPN SERVER 搭建
- PAT BASIC LEVEL 1035. 插入与归并(25)
- Ch4 应用编程接口
- leetcode_middle_8_238. Product of Array Except Self
- iOS超全开源框架、项目和学习资料汇总
- CSS入门教程(二)
- 人生没有下辈子
- 自定义cell
- jQuery--Ajax
- linux 中 execv, fork, wait 及 waitpid 的用法
- long,double高低位,非线程安全
- tableView的底层实现