对于一个矩阵,请设计一个算法,将元素按“之”字形打印。具体见样例。
给定一个整数矩阵mat,以及他的维数nxm,请返回一个数组,其中元素依次为打印的数字。
测试样例:
[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],4,3
返回:[1,2,3,6,5,4,7,8,9,12,11,10]
public int[] printMatrix(int[][] mat, int n, int m) { int[] arr = new int [n*m]; //hang记录你的行数,count记录的是你返回的字符数组的长度 int hang = 0; int count = 0; //偶数行顺序,奇数行倒序 for(int i = 0; i < mat.length; i++ ){ for(int j = 0; j < mat[i].length; j++){ //判断数奇数行还是偶数行 if(hang%2 == 0){ arr[count++] = mat[i][j]; }else{ arr[count++] = mat[i][m-1-j]; } } //写完了一行,行数需要+1 hang++; } return arr; }
class Printer { public: vector<int> printMatrix(vector<vector<int> > mat, int n, int m) { vector<int> result; for(int i=0;i<n;i++) { if(i&1) for(int j=m-1;j>=0;j--) result.push_back(mat[i][j]); else for(int j=0;j<m;j++) result.push_back(mat[i][j]); } return result; } };
import java.util.*;
public class Printer {
public int[] printMatrix(int[][] mat, int n, int m) {
// write code here
int[] temp = new int[n*m];
int count = 0;
for(int i=1;i<=n;i++){
if(i%2 !=0){
for(int j=1;j<=m;j++){
temp[count]=mat[i-1][j-1];
count++;
}
}else{
for(int j=m;j>=1;j--){
temp[count]=mat[i-1][j-1];
count++;
}
}
}
return temp;
}
}
行是奇数时从左到右,行为偶数时从右到左
import java.util.*; public class Printer { public int[] printMatrix(int[][] mat, int n, int m) { // write code here int a = 0; int b = 0; int[] c = new int[n*m]; int d = 0; while(a < n && d < n*m){ if(a % 2 == 0){ for(int i = b ; i < m ; i++){ c[d++] = mat[a][i]; b = m - 1; } } else{ for(int j = b ; j >= 0 ; j--){ c[d++] = mat[a][j]; b = 0; } } a++; } return c; } }
import java.util.*; public class Printer { public int[] printMatrix(int[][] mat, int n, int m) { int[] temp = new int[n*m];//用来存储最后输出数组数据 int cont = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { //下标加一用来判断逻辑上的奇偶行:1为奇数行,0为偶数行 if((i+1)%2!=0){ temp[cont] = mat[i][j]; cont++; }else{ temp[cont] = mat[i][m-1-j]; cont++; } } } return temp; } }
class Printer { public: vector<int> printMatrix(vector<vector<int> > mat, int n, int m) { // write code here vector<int> res; bool flag = true; for (int i=0; i<n; i++) { if (flag) { for (int j=0; j<m; j++) res.push_back(mat[i][j]); flag = !flag; } else { for (int j=m-1; j>=0; j--) res.push_back(mat[i][j]); flag = !flag; } } return res; } };
import java.util.*; public class Printer { public int[] printMatrix(int[][] mat, int n, int m) { // write code here、 int[] result=new int[m*n]; int index=0; for(int i=0;i<n;i++){ if(i%2==0){ for(int j=0;j<m;j++) result[index++]=mat[i][j]; }else{ for(int j=m-1;j>=0;j--) result[index++]=mat[i][j]; } } return result; } }
import java.util.*; public class Printer { public int[] printMatrix(int[][] mat, int n, int m) { // write code here int[] res = new int[n*m]; int k = 0; for(int i = 0;i < n;i++){ if((i & 1) == 0){ for(int j = 0; j < m;j++){ res[k++] = mat[i][j]; } }else{ for(int j = m - 1;j >= 0;j--){ res[k++] = mat[i][j]; } } } return res; } }
import java.util.*; public class Printer { public int[] printMatrix(int[][] mat, int n, int m) { int result[] = new int[n*m]; int count = 0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ int tmpj = (i%2)==0?j:m-1-j; result[count++] = mat[i][tmpj]; } } return result; } }
/** * 之字遍历 * @param mat * @param n * @param m * @return */ public static int[] printMatrix(int[][] mat, int n, int m) { int flag = 0; int k = 0; int[] res = new int[n*m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { res[k++] = flag == 0 ? mat[i][j] : mat[i][m-1-j]; } flag = 1 - flag; // 切换 } return res; }
classPrinter {public:vector<int> printMatrix(vector<vector<int> > mat, intn, intm) {// write code herevector<int> matrix;inti,j;for(i=0;i<n;i++){if(i%2==0){for(j=0;j<m;j++){matrix.push_back(mat[i][j]);}}else{for(j=m-1;j>=0;j--){matrix.push_back(mat[i][j]);}}}returnmatrix;}};
import java.util.*; public class Printer { public int[] printMatrix(int[][] mat, int n, int m) { // write code here int[] result = new int[n*m]; for (int i=0;i<n*m;i++){ if ((i/m)%2 == 0) result[i] = mat[i/m][i%m]; else result[i] = mat[i/m][m-i%m-1]; } return result; } }
import java.util.*; public class Printer { public int[] printMatrix(int[][] mat, int n, int m) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < mat.length; i ++ ) { if(i % 2 == 0) for (int j = 0; j < m; j ++ ) list.add(mat[i][j]); else for (int j = m - 1; j >= 0; j -- ) list.add(mat[i][j]); } int[] res = new int[n * m]; for (int i = 0; i < res.length; i ++ ) res[i] = list.get(i); return res; } }
class Printer { public: vector<int> printMatrix(vector<vector<int> > mat, int n, int m) { // write code here vector<int>ans; int dir = 1; for(int i=0;i<n;i++) { if(dir>0) { for(int j=0;j<m;j++)ans.push_back(mat[i][j]); } else { for(int j=m-1;j>=0;j--)ans.push_back(mat[i][j]); } dir = -1 * dir; } return ans; } };