leetcode (Toeplitz Matrix)
阿新 • • 發佈:2018-12-06
Title:Toeplitz Matrix 766
Difficulty:Easy
原題leetcode地址:https://leetcode.com/problems/toeplitz-matrix/
1.本題的行下標減去列下標相等的數也是相等
時間複雜度:O(n^2),巢狀for迴圈,需要遍歷整個陣列。
空間複雜度:O(n^2),申請了一個map,空間應該是n^2的一半。
/** * 本題的行下標減去列下標相等的數也是相等 * @param matrix * @return */ public static boolean isToeplitzMatrix(int[][] matrix) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { if (!map.containsKey(i - j)) { map.put(i - j, matrix[i][j]); } else if (map.get(i - j) != matrix[i][j]) { return false; } } } return true; }