zoukankan      html  css  js  c++  java
  • 【LEETCODE】45、766. Toeplitz Matrix

    package y2019.Algorithm.array;
    
    /**
     * @ProjectName: cutter-point
     * @Package: y2019.Algorithm.array
     * @ClassName: IsToeplitzMatrix
     * @Author: xiaof
     * @Description: 766. Toeplitz Matrix
     * 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.
     *
     * 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.
     *
     * @Date: 2019/7/4 19:47
     * @Version: 1.0
     */
    public class IsToeplitzMatrix {
    
        public boolean solution(int[][] matrix) {
            //就是比较斜线上是否是通一个数据
            //每一斜线
            //每次可以和下一行的斜线比较,这样依次比较
            for(int i = 0; i < matrix.length - 1; ++i) {
                for(int j = 0; j < matrix[i].length - 1; ++j) {
                    if(matrix[i][j] != matrix[i + 1][j + 1]) {
                        return false;
                    }
                }
            }
            return true;
        }
    
    
        public boolean isToeplitzMatrix(int[][] matrix) {
            for (int i = 0; i < matrix.length - 1; i++) {
                for (int j = 0; j < matrix[i].length - 1; j++) {
                    if (matrix[i][j] != matrix[i + 1][j + 1]) return false;
                }
            }
            return true;
        }
    
        public static void main(String args[]) {
    
            int[][] matrix = {{1,2,3,4},{5,1,2,3},{9,5,1,2}};
    
            IsToeplitzMatrix fuc = new IsToeplitzMatrix();
            System.out.println(fuc.isToeplitzMatrix(matrix));
        }
    
    }
  • 相关阅读:
    Android:真机调试遇到的问题(INSTALL_FAILED_CANCELLED_BY_USER和INSTALL_FAILED_INSUFFICIENT_STORAGE)
    tips:Java中的switch的选择因子
    tips:可变参数列表
    tips:Java中while的判断条件
    tips:Java的Random类和Random函数
    tips:Java基本数据类型大小比较
    怎样找到微信小程序功能呢?
    ajax和jquery
    json及JavaBean转json
    JavaBean转xml
  • 原文地址:https://www.cnblogs.com/cutter-point/p/11135112.html
Copyright © 2011-2022 走看看