Tuesday, November 17, 2015

[leetcode]Product of Array Except Self

建议还是写些example 玩下
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class Solution {
    public int[] productExceptSelf(int[] 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 rightMul = 1;
     for (int i = nums.length-2; i >= 0; i--){
         rightMul = nums[i+1]*rightMul;
      result[i] *= rightMul;
     }
     return result;
    }
}

No comments:

Post a Comment