题目
-
- 判断矩阵经轮转后是否一致
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021061221583059.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2ODI4Mzk1,size_16,color_FFFFFF,t_70)
-
- 使数组元素相等的减少操作次数
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210612222129596.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2ODI4Mzk1,size_16,color_FFFFFF,t_70)
思路与算法
-
- 判断矩阵经轮转后是否一致:本题的前置题目是这道46:.旋转图像,在此基础上熟悉旋转后的位置定位原理即可。注意原地改变,不增加空间复杂度。
-
- 使数组元素相等的减少操作次数:类似于爬楼梯,升序排列,再依次记录阶梯的阶数,每次计算累加的阶数。核心思想为,楼梯要一步一步下。
代码实现
-
- 判断矩阵经轮转后是否一致
class Solution {
public:
bool findRotation(vector<vector<int>>& mat, vector<vector<int>>& target) {
int len = mat.size();
for (int n = 0; n < 4; ++n) {
for (int i = 0; i < len / 2; ++i) {
for (int j = 0; j < (len + 1) / 2; ++j) {
int tmp = mat[i][j];
mat[i][j] = mat[len - 1 - j][i];
mat[len - 1 - j][i] = mat[len - 1 - i][len - 1 - j];
mat[len - 1 - i][len - 1 - j] = mat[j][len - 1 - i];
mat[j][len - 1 - i] = tmp;
}
}
if (mat == target) return true;
}
return false;
}
};
-
- 使数组元素相等的减少操作次数
class Solution {
public:
int reductionOperations(vector<int>& nums) {
int tmp = 0;
int ans = 0;
sort(nums.begin(),nums.end());
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] != nums[i - 1]) {
tmp++;
}
ans += tmp;
}
return ans;
}
};
写在最后
- 最近状态奇差,尽快调整。