标签:interview
public class SudokuVerifier { private boolean is1To9Row(int[][] a, int r) { int[] pos = new int[9]; for (int i = 0; i < 9; i++) { if (pos[a[r][i]] == 1) return false; pos[a[r][i]] = 1; } for (int i : pos) { if (i == 0) return false; } return true; } private boolean is1To9Column(int[][] a, int c) { int[] pos = new int[9]; for (int i = 0; i < 9; i++) { if (pos[a[i][c]] == 1) return false; pos[a[i][c]] = 1; } for (int i : pos) { if (i == 0) return false; } return true; } private boolean is1To9Sec(int[][] a, int x, int y) { int[] pos = new int[9]; for (int i = 0 ; i < 3 ; i ++) { for (int j = 0 ; j < 3 ; j ++) { if (pos[a[x + i][x + j]] == 1) return false; pos[a[x + i][x + j]] = 1; } } for (int i : pos) { if (i == 0) return false; } return true; } // O(n) private boolean isValid(int[][] a) { // Assumes... for (int i = 0 ; i < 9 ; i ++) { // Rows if (!is1To9Row(a, i)) return false; // Columns if (!is1To9Column(a, i)) return false; } // Secs for (int i = 0 ; i < 9 ; i += 3) { for (int j = 0 ; j < 9 ; j += 3) { if (!is1To9Sec(a, i, j)) return false; } } return true; } }
标签:interview
原文地址:http://7371901.blog.51cto.com/7361901/1586152