LintCode

作者: VincentJianshu | 来源:发表于2018-07-19 19:53 被阅读0次

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;
    }
}

相关文章

网友评论

      本文标题:LintCode

      本文链接:https://www.haomeiwen.com/subject/lbhxmftx.html