Bad solution by JAVA


  • -1
    M

    public class Solution {
    public int[] productExceSelf(int[] nums) {
    int[] result = new int[nums.length];
    int product = 1, index = 0, count = 0;
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] != 0)
    product *= nums[i];
    else {
    count++;
    index = i;
    }
    }
    if (count == 0) {
    for (int i = 0; i < nums.length; i++) {
    result[i] = product / nums[i];
    }
    }
    else if (count == 1) {
    for (int i = 0; i < nums.length; i++) {
    if (i == index) {
    result[index] = product;
    }else {
    result[i] = 0;
    }
    }
    }
    else {
    for (int i = 0; i < nums.length; i++) {
    result[i] = 0;
    }
    }
    return result;
    }
    }


Log in to reply
 

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