当前位置: 代码迷 >> 综合 >> Valid Sudoku - Leetcode
  详细解决方案

Valid Sudoku - Leetcode

热度:49   发布时间:2023-12-17 05:35:27.0

1 )  检查行; 2) 检查列; 3) 检查方格 => 用一个长度为9的bool数组判断是否有重复;

public class Solution {public boolean isValidSudoku(char[][] board) {boolean[] valid = new boolean[9];for(int i=0; i<board.length; i++){recoverValid(valid);for(int j=0; j<board.length; j++)if(checkValid(board[i][j], valid) == false)return false;recoverValid(valid);for(int j=0; j<board.length; j++)if(checkValid(board[j][i], valid) == false)return false;}for(int i=0; i<=board.length-3; i+=3){for(int j=0; j<=board.length-3; j+=3){recoverValid(valid);for(int m=0; m<3; m++)for(int n=0; n<3; n++){if(checkValid(board[i+m][j+n], valid) == false)return false;}}}return true;}private void recoverValid(boolean[] v){for(int i=0; i<v.length; i++)v[i]=false;}private boolean checkValid(char c, boolean[] b){if(c == '.')return true;else if(!b[c-'1']){b[c-'1']=true;return true;}else return false;}
}


  相关解决方案