如何在一个数据库做两个网站,泉州模板网站建站,wordpress改变链接地址,鲜花网站建设的总体目标那么#xff0c;如果你要枚举这些填充模式的索引#xff0c;你会得到0,01,00,12,01,10,22,11,22,2所以#xff0c;你需要遍历两个指标的总和。也就是说#xff0c;总添加量。如您所见#xff0c;0,0总计0#xff0c;1,0和0,1总计1#xff0c;依此类推。给我们这样的东西…那么如果你要枚举这些填充模式的索引你会得到0,01,00,12,01,10,22,11,22,2所以你需要遍历两个指标的总和。也就是说总添加量。如您所见0,0总计01,0和0,1总计1依此类推。给我们这样的东西0 1 21 2 32 3 4要以这种对角线模式进行迭代我们可以执行以下操作// set up your matrix, any size and shape (MxN) is fine, but jagged arrays will breakint[][] matrix {{0,0,0},{0,0,0},{0,0,0}};// number is the value we will put in each position of the matrixint number 1;// iterate while number is less than or equal to the total number of positions// in the matrix. So, for a 3x3 matrix, 9. (this is why the code wont work for// jagged arrays)for (int i 0; number matrix.length * matrix[0].length; i) {// start each diagonal at the top row and from the rightint row 0;int col i;do {// make sure row and length are within the bounds of the matrixif (row matrix.length col matrix[row].length) {matrix[row][col] number;number;}// we decrement col while incrementing row in order to traverse down and leftrow;col--;} while (row 0);}请注意虽然此实现将适用于所有矩阵大小(和形状)但它不会尽可能高效。其中n是matrix.length(假设为方形矩阵)该实现是大O符号中的最优O(n ^ 2)类算法;然而它有效地执行2×n ^ 2次迭代而最优解只能执行n ^ 2。