C# simple solution


  • 0
    J
    public int[] ProductExceptSelf(int[] nums)
        {
            int all = 1;
            int zc = 0; //Zero Count
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] == 0)
                    zc++;
                else
                    all = all * nums[i];
            }
            if (zc == 0)
                return nums.Select(i => all / i).ToArray();
            else if (zc == 1)
                return nums.Select(i => (i == 0) ? all : 0).ToArray();
            else
                return new int[nums.Length];
        }

  • 0
    S

    Problem description says division is not allowed.


  • 0
    J

    Thanks for the notice.


Log in to reply
 

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