1. 程式人生 > >Leetcode演算法Java全解答--36. 有效的數獨

Leetcode演算法Java全解答--36. 有效的數獨

Leetcode演算法Java全解答–36. 有效的數獨

文章目錄

題目

判斷一個 9x9 的數獨是否有效。只需要根據以下規則,驗證已經填入的數字是否有效即可。

數字 1-9 在每一行只能出現一次。
數字 1-9 在每一列只能出現一次。
數字 1-9 在每一個以粗實線分隔的 3x3 宮內只能出現一次。

上圖是一個部分填充的有效的數獨。

數獨部分空格內已填入了數字,空白格用 ‘.’ 表示。
說明:

一個有效的數獨(部分已被填充)不一定是可解的。
只需要根據以上規則,驗證已經填入的數字是否有效即可。
給定數獨序列只包含數字 1-9 和字元 ‘.’ 。
給定數獨永遠是 9x9 形式的。

示例 1:

輸入:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
輸出: true
示例 2:

輸入:
[
  ["8","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
輸出: false
解釋: 除了第一行的第一個數字從 5 改為 8 以外,空格內其他數字均與 示例1 相同。
     但由於位於左上角的 3x3 宮內有兩個 8 存在, 因此這個數獨是無效的。
說明:

一個有效的數獨(部分已被填充)不一定是可解的。
只需要根據以上規則,驗證已經填入的數字是否有效即可。
給定數獨序列只包含數字 1-9 和字元 '.' 。
給定數獨永遠是 9x9 形式的。

想法

// TODO

結果

// TODO

總結

// TODO

程式碼

我的答案

	public boolean isValidSudoku(char[][] board) {
		for (int i = 0; i < 9; i++) {
			HashSet<Character> row = new HashSet<>();
			HashSet<Character> column = new HashSet<>();
			HashSet<Character> cube = new HashSet<>();
			for (int j = 0; j < 9; j++) {
				// 檢查第i行,在橫座標位置
				if (board[i][j] != '.' && !row.add(board[i][j]))
					return false;
				// 檢查第i列,在縱座標位置
				if (board[j][i] != '.' && !column.add(board[j][i]))
					return false;
				// 行號+偏移量
				int RowIndex = 3 * (i / 3) + j / 3;
				// 列號+偏移量
				int ColIndex = 3 * (i % 3) + j % 3;
				//每個小九宮格,總共9個
				if (board[RowIndex][ColIndex] != '.' 
						&& !cube.add(board[RowIndex][ColIndex]))
					return false;
			}
		}
		return true;
	}

  

大佬們的答案

 // 使用點陣圖法,出現置為1
	public boolean isValidSudoku(char[][] board) {
		for (int i = 0; i < 9; i++) {
			int[] bit_map_row = new int[9];
			int[] bit_map_col = new int[9];
			int[] bit_map_cube = new int[9];
			// 注意值減去1,下標從0開始
			for (int j = 0; j < 9; j++) {
				if (board[i][j] != '.')
					if (bit_map_row[board[i][j] - '1'] == 1)
						return false;
					else
						bit_map_row[board[i][j] - '1'] = 1;
				if (board[j][i] != '.')
					if (bit_map_col[board[j][i] - '1'] == 1)
						return false;
					else
						bit_map_col[board[j][i] - '1'] = 1;
				int RowIndex = 3 * (i / 3) + j / 3;
				int ColIndex = 3 * (i % 3) + j % 3;
				int val = board[RowIndex][ColIndex];
				if (val != '.')
					if (bit_map_cube[val - '1'] == 1)
						return false;
					else
						bit_map_cube[val - '1'] = 1;
			}
		}
		return true;
	}

測試用例


其他

程式碼託管碼雲地址:https://gitee.com/lizhaoandroid/LeetCodeAll.git

檢視其他內容可以點選專欄或者我的部落格哈:https://blog.csdn.net/cmqwan

“大佬們的答案” 標籤來自leetcode,侵權請聯絡我進行刪改

如有疑問請聯絡,聯絡方式:QQ3060507060