package leetcode.array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Created by w84108989 on 2019/3/26. */ //如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵。 // // 给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True。 // // 示例 1: // // 输入: // matrix = [ // [1,2,3,4], // [5,1,2,3], // [9,5,1,2] // ] // 输出: True // 解释: // 在上述矩阵中, 其对角线为: // "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"。 // 各条对角线上的所有元素均相同, 因此答案是True。 // 示例 2: // // 输入: // matrix = [ // [1,2], // [2,2] // ] // 输出: False // 解释: // 对角线"[1, 2]"上的元素不同。 public class juzhen_766 { public static void main(String[] args) { int [][]a = {{1,2,3,4},{5,1,2,3},{9,5,1,2}}; int [][]b = {{1,2},{2,1}}; isToeplitzMatrix(a); System.out.println(isToeplitzMatrix(b)); } public static boolean isToeplitzMatrix(int[][] matrix) { int m = 0; int n = 0; for (int i = 0; i <matrix.length-1; i++) { for (int j = 0; j < matrix[0].length; j++) { int tmp = matrix[i][j]; m = i + 1; n = j + 1; while (m < matrix.length && n < matrix[0].length ) { if (tmp == matrix[m][n]){ m++; n++; }else { return false; } } } } return true; } }
leetcode_766
最新推荐文章于 2021-02-22 10:23:43 发布