Re: [閒聊] 每日leetcode已回收
哩扣幫++
原本看這篇想說
比這個在幹嘛看不懂==
剛剛想說睡前也來寫一下
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
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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 216 之 1554 篇):