Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (廷廷)時間1年前 (2024/02/01 16:29), 編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串643/719 (看更多)
多寫一題 先把0的位置存起來然後再看要乘回去還怎樣的 我看解答有一個左邊乘過去然後再從右邊乘過來的 比我快一倍 但我看不懂 算惹 238. Product of Array Except Self class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { vector<int> zero_pos; int acc=1; int n=nums.size(); vector<int> ans(n,0); for(int i=0; i<n; i++){ if(nums[i]==0){ zero_pos.push_back(i); } else{ acc*=nums[i]; } } if(zero_pos.size()==1){ ans[zero_pos[0]]=acc; return ans; } else if(zero_pos.size()>1){ return ans; } for(int i=0; i<n; i++){ ans[i]=acc/nums[i]; } return ans; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.14.7 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1706776153.A.2DC.html

02/01 16:42, 1年前 , 1F
那個你模擬一次就知道了
02/01 16:42, 1F
文章代碼(AID): #1bkrPPBS (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bkrPPBS (Marginalman)