Java solution


  • 0
    H

    public int[] productExceptSelf(int[] nums) {
    int[] s = new int[nums.length];
    long l = 1;
    int zeroNum = 0;
    int zeroFlag = -1;
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] != 0) {
    l *= nums[i];
    } else {
    zeroNum++;
    zeroFlag = i;
    }
    }
    if (zeroNum >= 2) {
    return s;
    }
    if (zeroNum == 1) {
    s[zeroFlag]=(int) l;
    return s;
    }
    for (int i = 0; i < nums.length; i++) {
    s[i]=(int) (l/nums[i]);
    }
    return s;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.