Could Anyone Help me to figure out the problem?


  • 0
    Y
    import java.util.HashMap;
    
    public class ValidSudoku {
    public boolean isValidSudoku(char [][] board){
    	int length=board[0].length;
    	int height=board.length;
    	if(!validLH(0,0,length,height,board)) return false;
    	for(int i=0;i<height;i+=3){
    		for(int j=0;j<length;j+=3){
    			if(!validLH(i,j,i+3,j+3,board)) return false;
    		}
    	}
    	return true;
    }
    
    
    
    
    public boolean validLH(int starti, int startj, int length,int height,char [] [] board){
    	HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
    	for(int i=starti;i<height;i++){
    		for(int j=startj;j<length;j++){
    			if(board[i][j]!='.'){
    				if(hm.containsKey(board[i][j])){
    					return false;
    				}
    				hm.put(board[i][j], 1);
    			}
    		}
    	}
    	hm.clear();
    	for(int i=startj;i<length;i++){
    		for(int j=starti;j<height;j++){
    			if(board[j][i]!='.'){
    				if(hm.containsKey(board[j][i])){
    					return false;
    				}
    				hm.put(board[j][i],1);
    			}
    		}
    	}
    	return true;
    }
    

    }


  • 0
    T

    Only you know what's your problem (that is the difference between the expected result and what you return). "It doesn't work!" is not a problem, it's lack of communication.

    validLH(0,0,9,9) will check all numbers, so if there are two cells that are equal anywhere on the board you return false. Click the link at the top of the problem description: Sudoku Puzzles - The Rules, it illustrates with pictures what you need to check for.

    Simplifying tips:

    • length and height is always 9, by definition.
    • you could use a HashSet<Character> instead of a Map with non-relevant (non-used) values.

  • 0
    Y
        public class Solution {
    public boolean isValidSudoku(char[][] board) {
         int length=board[0].length;
        int height=board.length;
        if(!validLH(0,0,length,height,board)) return false;
        for(int i=0;i<height;i+=3){
            for(int j=0;j<length;j+=3){
                if(!validLH2(i,j,i+3,j+3,board)) return false;
            }
        }
        return true;
    }
    
    
    
    public boolean validLH(int starti, int startj, int length,int height,char [] [] board){
        HashSet<Character> hm=new HashSet<Character>();
        for(int i=starti;i<height;i++){
            for(int j=startj;j<length;j++){
                if(board[i][j]!='.'){
                    if(!hm.add(board[i][j])){
                        return false;
                    }
                    hm.add(board[i][j]);
                }
            }
            hm.clear();
        }
       
        for(int i=startj;i<length;i++){
            for(int j=starti;j<height;j++){
                if(board[j][i]!='.'){
                    if(!hm.add(board[j][i])){
                        return false;
                    }
                    hm.add(board[j][i]);
                }
            }
            hm.clear();
        }
        return true;
    }
    public boolean validLH2(int starti, int startj, int height,int length,char [] [] board){
        HashSet<Character> hm=new HashSet<Character>();
        for(int i=starti;i<height;i++){
            for(int j=startj;j<length;j++){
                if(board[i][j]!='.'){
                    if(!hm.add(board[i][j])){
                        return false;
                    }
                    hm.add(board[i][j]);
                }
            }
            
        }
        return true;
    }
    

    }


  • 0
    Y

    I got right one. Thank you


Log in to reply
 

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