程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> [leetcode]Valid Sudoku

[leetcode]Valid Sudoku

編輯:C++入門知識

[leetcode]Valid Sudoku


問題描述:

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 '.'.

\

A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.



<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+tPrC66O6PC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">public class Valid_Sudoku { //java public boolean isValidSudoku(char[][] board) { int size = 9; int [] member = new int[size]; //record if is occur; //valid row for(int i = 0; i < 9; i++){ for(int k = 0; k < 9; k++) member[k] = 0; for(int j = 0; j < 9; j++){ if(board[i][j] == '.') continue; int pos = board[i][j]-'0'; if(member[pos-1] == 1) return false; else member[pos-1] = 1; } } //valid col for(int i = 0; i < 9; i++){ for(int k = 0; k < 9; k++) member[k] = 0; for(int j = 0; j < 9; j++){ if(board[j][i] == '.') continue; int pos = board[j][i]-'0'; if(member[pos-1] == 1) return false; else member[pos-1] = 1; } } //valid cube for(int ibegin = 0; ibegin < 9; ibegin = ibegin+3){ for(int jbegin = 0; jbegin < 9; jbegin = jbegin+3){ for(int k = 0; k < 9; k++) member[k] = 0; for(int i = ibegin; i < ibegin+3; i++){ for(int j = jbegin; j < jbegin+3; j++){ if(board[i][j] == '.') continue; int pos = board[i][j]-'0'; if(member[pos-1] == 1) return false; else member[pos-1] = 1; } } } } return true; } public static void main(String [] args){ String[] boardStr = {"......5..", ".........", ".........", "93..2.4..", "..7...3..", ".........", "...34....", ".....3...", ".....52.."}; char [][] board = new char [9][9]; for(int i =0; i< boardStr.length; i++){ for(int j = 0; j

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved