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

看板Marginalman作者 (6B)時間1年前 (2024/05/14 02:41), 1年前編輯推噓0(005)
留言5則, 1人參與, 1年前最新討論串216/1554 (看更多)
哩扣幫++ 原本看這篇想說 比這個在幹嘛看不懂== 剛剛想說睡前也來寫一下 class Solution { public: int matrixScore(vector<vector<int>>& grid) { int m = grid.size(); int n = grid[0].size(); int res = 0; for(int i = 0; i < m; i++){ if(grid[i][0] == 0){ for(int &j: grid[i]){ j = !j; } } } for(int j = 0; j < n; j++){ int cnt = 0; for(int i = 0; i< m; i++){ cnt += grid[i][j]; } cnt = max(cnt, m - cnt); res += cnt * pow(2, n -j -1); } return res; } }; 回來再看看懂了 那個count可以直接加ㄚ 然後那個base好酷 我要偷走了 只剩我還在pow了小朋友嗚嗚嗚嗚 ※ 引述 《ray90514》 之銘言: : grid[i][j] == grid[i][0] 可以依照開頭的bit與該位的bit得到最終有無翻轉的結果 : class Solution { : public: : int matrixScore(vector<vector<int>>& grid) { : int ans = 0; : int m = grid.size(), n = grid[0].size(); : for(int j = n - 1, base = 1; j >= 0; j--){ : int count = 0; : for(int i = 0; i < m; i++){ : count += grid[i][j] == grid[i][0]; : } : ans += max(m - count, count) * base; : base <<= 1; : } : return ans; : } : }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.121.194 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1715625687.A.14B.html

05/14 02:44, 1年前 , 1F
然後原來for那個要加& ==
05/14 02:44, 1F

05/14 02:48, 1年前 , 2F
對不起 我好笨==
05/14 02:48, 2F

05/14 02:48, 1年前 , 3F
現在才看懂 在跟前面那個比
05/14 02:48, 3F

05/14 02:48, 1年前 , 4F
我先跑一圈翻了 累死
05/14 02:48, 4F

05/14 02:51, 1年前 , 5F
睡覺嗚嗚嗚我好笨
05/14 02:51, 5F
※ 編輯: sixB (123.205.121.194 臺灣), 05/14/2024 02:52:28 ※ 編輯: sixB (123.205.121.194 臺灣), 05/14/2024 02:53:00
文章代碼(AID): #1cGbxN5B (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cGbxN5B (Marginalman)