1619. 删除某些元素后的数组均值
代码实现(STL)
class Solution {
public:
double trimMean(vector<int>& arr) {
sort(arr.begin(), arr.end());
return accumulate(arr.begin() + (int)(0.05 * arr.size()), arr.begin() + (int)(0.95* arr.size()), 0) / (double)(arr.size() * 0.9);
}
};
2166. 设计位集
代码实现
class Bitset {
private:
string str;
string flipped;
int cnt;
public:
Bitset(int size) {
for (int i = 0; i < size; i++) {
str += '0';
flipped += '1';
}
cnt = size;
}
void fix(int idx) {
if (str[idx] == '0') cnt--;
str[idx] = '1';
flipped[idx] = '0';
}
void unfix(int idx) {
if (str[idx] == '1') cnt++;
str[idx] = '0';
flipped[idx] = '1';
}
void flip() {
cnt = str.size() - cnt;
string tmp = flipped;
flipped = str;
str = tmp;
}
bool all() {
return !cnt;
}
bool one() {
return cnt < str.size();
}
int count() {
return str.size() - cnt;
}
string toString() {
return str;
}
};
764. 最大加号标志
代码实现(前缀和)
class Solution {
public:
int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
vector<vector<int>> grid(vector(n, vector<int>(n, 1)));
for (auto &arr : mines) {
grid[arr[0]][arr[1]] = 0;
}
auto left = grid;
for (int i = 0; i < n; i++) {
for (int j = 1; j < n; j++) {
if (left[i][j]) left[i][j] += left[i][j - 1];
}
}
auto right = grid;
for (int i = 0; i < n; i++) {
for (int j = n - 2; j >= 0; j--) {
if (right[i][j]) right[i][j] += right[i][j + 1];
}
}
auto top = grid;
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
if (top[i][j]) top[i][j] += top[i - 1][j];
}
}
auto bottom = grid;
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < n; j++) {
if (bottom[i][j]) bottom[i][j] += bottom[i + 1][j];
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ans = max(ans, min(min(left[i][j], right[i][j]),
min(top[i][j], bottom[i][j])));
}
}
return ans;
}
};