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

看板Marginalman作者 (smart0eddie)時間1年前 (2024/07/19 12:58), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串527/1553 (看更多)
2024-07-19 1380. Lucky Numbers in a Matrix Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order. A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column. row 的 min col 的 max 所以就先每個row 找最小 每個 col 找最大 然後看有沒有相同的 class Solution { public: vector<int> luckyNumbers (vector<vector<int>>& matrix) { int row = matrix.size(); int col = matrix[0].size(); vector<int> minV(row, INT_MAX); vector<int> maxV(col, 0); // min max for (int r = 0; r < row; r++) { for (int c = 0; c < col; c++) { int n = matrix[r][c]; if (n < minV[r]) { minV[r] = n; } if (n > maxV[c]) { maxV[c] = n; } } } // get the lucky number vector<int> result; for (int n : minV) { if(find(maxV.begin(), maxV.end(), n) != maxV.end()) { result.push_back(n); } } return result; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 73.173.211.221 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1721365097.A.621.html
文章代碼(AID): #1ccV9fOX (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ccV9fOX (Marginalman)