ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

分类贪心

分类贪心

lc1975

按负数个数 分类贪心

class Solution {

public:

long long maxMatrixSum(vector<vector<int>>& matrix) {

long long total = 0;

int neg_cnt = 0;

int mn = INT_MAX;

for (auto& row : matrix) {

for (int x : row) {

if (x < 0) {

neg_cnt++;

x = -x;

}

mn = min(mn, x);

total += x;

}

}

if (neg_cnt % 2)

total -= mn * 2; //负数个数分类

return total;

}

};

返回列表