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