LintCode

1042 Toeplitz Matrix

public class Solution {
    /**
     * @param matrix: the given matrix
     * @return: True if and only if the matrix is Toeplitz
     */
    public boolean isToeplitzMatrix(int[][] matrix) {
        // Write your code here
        int m = matrix.length;
        if (m == 0)
            return true;
        int n = matrix[0].length;
        for (int i = 0; i < m - 1; i++)
            if (!check(matrix,i,n))
                return false;
        return true;
    }
    
    private boolean check(int[][] matrix, int i, int n)
        for (int j = 0; j < n - 1; j++) {
            if (matrix[i][j] != matrix[i+1][j+1])
                return false;
        return true;
    }
}
  1. Flip Game
public class Solution {
    /**
     * @param s: the given string
     * @return: all the possible states of the string after one valid move
     */
    public List<String> generatePossibleNextMoves(String s) {
        // write your code here
        List<String> list = new LinkedList<String>();
        int len = s.length();
        if (len < 2)
            return list;
        for (int i = 0; i < len - 1; i++) 
            if (s.charAt(i) == '+' && s.charAt(i+1) == '+')
                list.add(s.substring(0, i) + "--" + s.substring(i+2, len));
        return list;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容