A urgent question i dont understand really!!! Plz help me!!!


  • -1
    S

    The following code produces wrong answers:

    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            for(int i=0; i<9; i++) {
                if(!isNotDuplicate(board, 0, i, 8, i+1)) return false;
            }
            for(int i=0; i<9; i++) {
                if(!isNotDuplicate(board, i, 0, i+1, 8)) return false;
            }
            for(int i=0; i<9; i+=3) {
                for(int j=0; j<9; j+=3) {
                    if(!isNotDuplicate(board, i, j, i+3, j+3)) return false;
                }
            }
            return true;
        }
    
        public boolean isNotDuplicate(char[][] board, int x1, int y1, int x2, int y2) {
            Set<Character> set = new HashSet<Character>();
            for(int i=y1; i<y2; i++) {
                for(int j=x1; j<x2; j++) {
                    if (board[i][j]!='.') if(!set.add(board[i][j])) return false;
                }
            }
            return true;
        }
    }
    

    But change into the following format it is accepted:

    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            for(int i=0; i<9; i++) {
                if(!isNotDuplicate(board, 0, i, 8, i)) return false;
            }
            for(int i=0; i<9; i++) {
                if(!isNotDuplicate(board, i, 0, i, 8)) return false;
            }
            for(int i=0; i<9; i+=3) {
                for(int j=0; j<9; j+=3) {
                    if(!isNotDuplicate(board, i, j, i+2, j+2)) return false;
                }
            }
            return true;
        }
    
        public boolean isNotDuplicate(char[][] board, int x1, int y1, int x2, int y2) {
            Set<Character> set = new HashSet<Character>();
            for(int i=y1; i<=y2; i++) {
                for(int j=x1; j<=x2; j++) {
                    if (board[i][j]!='.') if(!set.add(board[i][j])) return false;
                }
            }
            return true;
        }
    }
    

    I DONT KNOW WHY!!!!


  • 0
    Q

    It's pretty obvious. In first solution you shoud replace '8' with '9' since you're using <


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.