leetcode:Product of Array Except Self

来源:互联网 发布:西门子840dsl编程手册 编辑:程序博客网 时间:2024/06/07 22:38


这道题目我记得是某一年腾讯的笔试题目来的,

给出一个数组,求出除了对应位置数字外的数组乘积




public class Solution {    public int[] productExceptSelf(int[] nums) {        if(nums == null || nums.length == 0){            return null;        }        int[] output = new int[nums.length];        Arrays.fill(output, 1);        int temp = 1;        output[0] = temp;        for(int i = 1, len = nums.length - 1; i <= len; ++i){            temp *= nums[i - 1];            output[i] = temp;        }        temp = 1;        for(int i = nums.length - 2; i >= 0; --i){            temp *= nums[i + 1];            output[i] *= temp;        }        return output;    }}


0 0
原创粉丝点击