Java for LeetCode 036 Valid Sudoku

來源:互聯網
上載者:User

標籤:

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character ‘.‘.

解題思路:

傳說中的數獨(九宮格)問題,老實遍曆三個規則即可:

JAVA實現:

static public boolean isValidSudoku(char[][] board) {for (int i = 0; i < board.length; i++) {HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();for (int j = 0; j < board[0].length; j++) {if (board[i][j] != ‘.‘) {if (hashmap.containsKey(board[i][j]))return false;hashmap.put(board[i][j], 1);}}}for (int j = 0; j < board[0].length; j++) {HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();for (int i = 0; i < board.length; i++) {if (board[i][j] != ‘.‘) {if (hashmap.containsKey(board[i][j]))return false;hashmap.put(board[i][j], 1);}}}    for (int i = 0; i < board.length; i += 3){       for (int j = 0; j < board[0].length; j += 3){           HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();for (int k = 0; k < 9; k++) {if (board[i + k / 3][j + k % 3] != ‘.‘) {if (hashmap.containsKey(board[i + k / 3][j + k % 3]))return false;hashmap.put(board[i + k / 3][j + k % 3], 1);}}}}return true;}

 

Java for LeetCode 036 Valid Sudoku

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.