C++ Code in 56ms


  • -1
    1

    fast than 94.83%

    class Solution {
    public:
        vector<int> productExceptSelf(vector<int>& nums) {
            int size = nums.size();
            // if (size < 2) return nums;
            vector<int> ret(size, 0);
            long x=1; long zero=0;
            for (int i=0; i<size; ++i) {
                if (nums[i]!=0) x*=nums[i];
                else if (++zero>1) {x=0;break;}
            }
            if (x != 0) {
                for (int i=0; i<size; ++i) {
                    if (nums[i]!=0) ret[i] = (zero <= 0) ? (x / nums[i]) : 0;
                    else ret[i] = x;
                }
            }
            return ret;
        }
    };

  • 0
    W

    it requires: without division


  • 0
    1

    @Wesley_Jiang sorry,I don't care it……


Log in to reply
 

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