https://www.lintcode.com/problem/same-diagonal-elements/description
给定一个 n × n n\times n n×n矩阵,问其所有左上到右下的对角线是不是数值都相等。
遍历矩阵,直到发现 A [ i ] [ j ] ≠ A [ i − 1 ] [ j − 1 ] A[i][j]\ne A[i-1][j-1] A[i][j]=A[i−1][j−1]的时候就返回false。代码如下:
public class Solution { /** * @param matrix: a matrix * @return: return true if same. */ public boolean judgeSame(int[][] matrix) { // write your code here. for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { if (i > 0 && j > 0 && matrix[i - 1][j - 1] != matrix[i][j]) { return false; } } } return true; } }时间复杂度 O ( n 2 ) O(n^2) O(n2),空间 O ( 1 ) O(1) O(1)。
