class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { vector<int> ans; int gop = 1, zeroCnt = 0; for(int i = 0; i < nums.size(); i++) { if(nums[i] != 0) { gop *= nums[i]; continue; } zeroCnt++; } if(zeroCnt > 1) { for(int i = 0; i < nums.size(); i++) { ans.push_back(0); } return ans; } else if(zeroCnt == 1) { for(int i = 0; i < nums.size(); i++) { if(nums[i] == 0) { ans.push_back(gop); continue; } ans.push_back(0); } } else { for(int i = 0; i < nums.size(); i++) { ans.push_back(gop/nums[i]); } } return ans; } };
class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { int n = nums.size(); vector<int> ans(n); int gop = 1; int zeroCnt = count(nums.begin(), nums.end(), 0); cout << zeroCnt; if(zeroCnt > 1) return ans; if(zeroCnt == 1) { int j; for(int i = 0; i < n; i++) { if(nums[i] == 0) { j = i; continue; } gop*=nums[i]; } ans[j] = gop; return ans; } for(int i = 0; i < n; i++) { gop*=nums[i]; } for(int i = 0; i < n; i++) { ans[i] = gop/nums[i]; } return ans; } };