在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given an Example 1: Input: matrix = [ [1,2,3,4], [5,1,2,3], [9,5,1,2] ] Output: True Explanation: In the above grid, the diagonals are: "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". In each diagonal all elements are the same, so the answer is True. Example 2: Input: matrix = [ [1,2], [2,2] ] Output: False Explanation: The diagonal "[1, 2]" has different elements. Note:
如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵。 给定一个 示例 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]"上的元素不同。 说明:
进阶:
Runtime: 28 ms
Memory Usage: 19 MB
1 class Solution { 2 func isToeplitzMatrix(_ matrix: [[Int]]) -> Bool { 3 for i in 0..<matrix.count - 1 4 { 5 for j in 0..<matrix[i].count - 1 6 { 7 if matrix[i][j] != matrix[i + 1][j + 1] 8 { 9 return false 10 } 11 } 12 } 13 return true 14 } 15 } 28ms 1 class Solution { 2 func isToeplitzMatrix(_ matrix: [[Int]]) -> Bool { 3 var temp: [Int] = matrix[0] 4 for i in 1..<matrix.count { 5 let smallMatrix = matrix[i] 6 for j in 0..<smallMatrix.count { 7 if j == 0 { 8 temp.insert(smallMatrix[j], at: 0) 9 } else { 10 if smallMatrix[j] != temp[j] { 11 return false 12 } 13 } 14 } 15 } 16 return true 17 } 18 } 32ms 1 class Solution { 2 func isToeplitzMatrix(_ matrix: [[Int]]) -> Bool { 3 var result = true 4 5 for i in 0..<matrix.count { 6 for j in 0..<matrix[0].count { 7 if i > 0 && j > 0 { 8 break; 9 } 10 var pointR = i + 1 11 var pointC = j + 1 12 13 let val1 = matrix[i][j]; 14 let count = matrix[0].count 15 16 while pointR < matrix.count { 17 if pointC < count { 18 let val2 = matrix[pointR][pointC] 19 if val1 != val2 { 20 result = false; 21 } 22 pointR += 1; 23 pointC += 1; 24 } else { 25 break; 26 } 27 } 28 } 29 } 30 return result; 31 } 32 } 68ms 1 class Solution { 2 func isToeplitzMatrix(_ matrix: [[Int]]) -> Bool { 3 for i in 0..<matrix[0].count{ 4 var val = -1 5 var rowIndex = 0 6 var columnIndex = i 7 8 while rowIndex < matrix.count && columnIndex < matrix[0].count{ 9 if val != -1 && val != matrix[rowIndex][columnIndex]{ 10 return false 11 } 12 val = matrix[rowIndex][columnIndex] 13 rowIndex += 1 14 columnIndex += 1 15 } 16 } 17 18 for i in 0..<matrix.count{ 19 var val = -1 20 21 var rowIndex = i 22 var columnIndex = 0 23 24 while rowIndex < matrix.count && columnIndex < matrix[0].count{ 25 if val != -1 && val != matrix[rowIndex][columnIndex]{ 26 return false 27 } 28 val = matrix[rowIndex][columnIndex] 29 rowIndex += 1 30 columnIndex += 1 31 } 32 } 33 return true 34 } 35 } 88ms 1 class Solution { 2 func isToeplitzMatrix(_ matrix: [[Int]]) -> Bool { 3 guard matrix.count > 0 else { 4 return false 5 } 6 if matrix.count == 1 { 7 return true 8 } 9 10 var row = matrix[0] 11 for i in 1..<matrix.count { 12 row = matrix[i-1] 13 if !matrix[i][1...].elementsEqual(row[0..<row.count - 1]) { 14 return false 15 } 16 } 17 return true 18 } 19 }
|
请发表评论