T1: 商品折扣后的最终价格
签到题,挺简单的。按照题意直接写就行。
class Solution {
public:
vector<int> finalPrices(vector<int>& prices) {
vector<int> ans;
int n = prices.size();
for(int i = 0; i < n; i++)
{
bool flag = 0;
int tmp;
for(int j = i+1; j < n; j++)
{
if(prices[j] <= prices[i])
{
flag = 1;
tmp = prices[j];
break;
}
}
flag == 1 ? ans.push_back(prices[i] - tmp) : ans.push_back(prices[i]);
}
return ans;
}
};
T2: 子矩形查询
跟上周有道题类似,也是实现一个目标类。按照题意写就行,同样没什么难度。
class SubrectangleQueries {
private:
int f[110][110];
public:
SubrectangleQueries(vector<vector<int>>& rectangle) {
for(int i = 0; i < rectangle.size(); i++)
{
for(int j = 0; j < rectangle[i].size(); j++)
{
f[i][j] = rectangle[i][j];
}
}
}
void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
for(int i = row1; i <= row2; i++)
for(int j = col1; j <= col2; j++)
{
f[i][j] = newValue;
}
}
int getValue(int row, int col) {
return f[row][col];
}
};