标签:str art 代码实现 通过 mes std back color java
给定一个01矩阵,对矩阵进行一下操作:
水平方向翻转图像,如:[1,1,0]水平翻转之后变为[0,1,1]
倒置图像的每个元素,即用0替代1,用1替代0。如:[0,1,1]变为[1,0,0]
测试样例:
输入:[ [1,1,0],[1,0,1],[0,0,0] ]
输出:[ [1,0,0],[0,1,0],[1,1,1] ]
代码实现:
1.C++实现
#pragma once
#include<vector>
using namespace std;
class FlipAndInverImageClass{
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
vector<vector<int>> result; vector<int> mid; for (int i = 0; i < A.size(); ++i) {
for (int j = A[i].size() - 1; j >= 0; --j) {
//通过与1异或得到取反,让j从末尾开始实现矩阵行的翻转
mid.push_back( A[i][j] ^ 1 );
}
result.push_back( mid );
mid.clear();
}
return result;
}
};
2.Java实现
public class HelloWorld {
public static void main(String[] args){
int[][] array = new int[][]{{1,1,0},{1,0,1},{0,0,0}};
SolutionClass solutionClass = new SolutionClass();
int[][] result = solutionClass.flipAndInverImage(array);
for(int i=0;i<result.length;++i){
for(int j=0;j<result[i].length;++j){
System.out.print(result[i][j]+" ");
}
System.out.println();
}
}
}
class SolutionClass{
public int[][] flipAndInverImage(int[][] A) {
for (int[] rows : A) {
int start = 0, end = rows.length-1;
while (start <= end) {
int temp = rows[start]^1;
rows[start] = rows[end]^1;
rows[end] = temp;
start++;
end--;
}
}
return A;
}
}
标签:str art 代码实现 通过 mes std back color java
原文地址:https://www.cnblogs.com/Tomorrow-will-be-better/p/11123680.html