代码拉取完成,页面将自动刷新
// Time: O(nlogn)
// Space: O(1)
// Using template.
class Solution {
public:
int minArea(vector<vector<char>>& image, int x, int y) {
using namespace std::placeholders; // for _1, _2, _3...
const auto searchColumns =
[](const vector<vector<char>>& image, bool has_one, const int mid) {
return has_one == any_of(image.cbegin(), image.cend(),
[=](const vector<char>& row) { return row[mid] == '1'; });
};
const auto searchRows =
[](const vector<vector<char>>& image, bool has_one, const int mid) {
return has_one == any_of(image[mid].cbegin(), image[mid].cend(),
[](const char& col) { return col == '1'; });
};
const int left = binarySearch(0, y - 1, bind(searchColumns, image, true, _1));
const int right = binarySearch(y + 1, image[0].size() - 1, bind(searchColumns, image, false, _1));
const int top = binarySearch(0, x - 1, bind(searchRows, image, true, _1));
const int bottom = binarySearch(x + 1, image.size() - 1, bind(searchRows, image, false, _1));
return (right - left) * (bottom - top);
}
private:
template<typename T>
int binarySearch(int left, int right, const T& find) {
while (left <= right) {
const int mid = left + (right - left) / 2;
if (find(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
};
// Using std::bind().
class Solution2 {
public:
int minArea(vector<vector<char>>& image, int x, int y) {
using namespace std::placeholders; // for _1, _2, _3...
const auto searchColumns =
[](const vector<vector<char>>& image, bool has_one, const int mid) {
return has_one == any_of(image.cbegin(), image.cend(),
[=](const vector<char>& row) { return row[mid] == '1'; });
};
const auto searchRows =
[](const vector<vector<char>>& image, bool has_one, const int mid) {
return has_one == any_of(image[mid].cbegin(), image[mid].cend(),
[](const char& col) { return col == '1'; });
};
function<bool(const int)> findLeft = bind(searchColumns, image, true, _1);
const int left = binarySearch(0, y - 1, findLeft);
function<bool(const int)> findRight = bind(searchColumns, image, false, _1);
const int right = binarySearch(y + 1, image[0].size() - 1, findRight);
function<bool(const int)> findTop = bind(searchRows, image, true, _1);
const int top = binarySearch(0, x - 1, findTop);
function<bool(const int)> findBottom = bind(searchRows, image, false, _1);
const int bottom = binarySearch(x + 1, image.size() - 1, findBottom);
return (right - left) * (bottom - top);
}
private:
int binarySearch(int left, int right, function<bool(const int)>& find) {
while (left <= right) {
const int mid = left + (right - left) / 2;
if (find(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
};
// Using lambda.
class Solution3 {
public:
int minArea(vector<vector<char>>& image, int x, int y) {
const auto searchColumns =
[](const vector<vector<char>>& image, bool has_one, const int mid) {
return has_one == any_of(image.cbegin(), image.cend(),
[=](const vector<char>& row) { return row[mid] == '1'; });
};
const auto searchRows =
[](const vector<vector<char>>& image, bool has_one, const int mid) {
return has_one == any_of(image[mid].cbegin(), image[mid].cend(),
[](const char& col) { return col == '1'; });
};
const int left = binarySearch(0, y - 1, searchColumns, image, true);
const int right = binarySearch(y + 1, image[0].size() - 1, searchColumns, image, false);
const int top = binarySearch(0, x - 1, searchRows, image, true);
const int bottom = binarySearch(x + 1, image.size() - 1, searchRows, image, false);
return (right - left) * (bottom - top);
}
private:
int binarySearch(int left, int right,
const function<bool(const vector<vector<char>>&, bool, const int)>& find,
const vector<vector<char>>& image,
bool has_one) {
while (left <= right) {
const int mid = left + (right - left) / 2;
if (find(image, has_one, mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。