zoukankan      html  css  js  c++  java
  • [Swift]LeetCode766. 托普利茨矩阵 | Toeplitz Matrix

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址: https://www.cnblogs.com/strengthen/p/10533374.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.

    Now given an M x N matrix, return True if and only if the matrix is Toeplitz

    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 will be a 2D array of integers.
      2. matrix will have a number of rows and columns in range [1, 20].
      3. matrix[i][j] will be integers in range [0, 99].

    如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵

    给定一个 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]"上的元素不同。
    

    说明:

    1.  matrix 是一个包含整数的二维数组。
    2. matrix 的行数和列数均在 [1, 20]范围内。
    3. matrix[i][j] 包含的整数在 [0, 99]范围内。

    进阶:

    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 }
  • 相关阅读:
    晶体三极管及其放大电路之共集电极电路
    晶体三极管及其基本放大电路之共发射极电路
    晶体三极管及其基本放大电路概述
    晶体二极管及其基本应用电路
    印刷数字的识别
    pyqt5之简单窗口的创建
    cadence布线完成后的补充操作
    cadence钻孔文件及光绘文件的生成
    cadence电源和地平面的处理
    cadence电路板布线
  • 原文地址:https://www.cnblogs.com/strengthen/p/10533374.html
Copyright © 2011-2022 走看看