Product of Array Except Self
unknown
c_cpp
13 days ago
1.1 kB
1
Indexable
Never
class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { int product=1; bool zeroFlag=false,numFlag=false,secondZeroFlag=false; for(int i:nums) { if(i==0 && !zeroFlag) { zeroFlag=true; continue; } else if(i==0 && zeroFlag) { secondZeroFlag=true; continue; } else numFlag=true; product*=i; } vector<int> output; if(secondZeroFlag || !numFlag) { for(int i:nums) output.push_back(0); } else if(zeroFlag) { for(int i:nums) { if(i==0) output.push_back(product); else output.push_back(0); } } else{ for(int i:nums) { output.push_back(product/i); } } return output; } };
Leave a Comment