Product of Array Except Self

来源:互联网 发布:ghost网络克隆软件 编辑:程序博客网 时间:2024/05/17 22:58
public class Solution {    public int[] productExceptSelf(int[] nums) {        int[] left = new int[nums.length];        left[0] = nums[0];        int[] right = new int[nums.length];        right[nums.length - 1] = nums[nums.length - 1];        for (int i = 1; i < nums.length; i++) {            left[i] = left[i - 1]* nums[i];        }        for (int i = nums.length - 2; i >= 0; i--) {            right[i] = right[i + 1]*nums[i];        }                int[] result = new int[nums.length];        result[0] = right[1];        result[nums.length - 1] = left[nums.length - 2];        for (int i = 1; i < nums.length - 1; i++) {            result[i] = left[i - 1]*right[i + 1];         }                return result;    }}
j今天写代码没感觉啊,费了好大劲才写完
0 0