本文主要是介绍LeetCode_Valid Sudoku,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一.题目
Valid Sudoku
Total Accepted: 29804 Total Submissions: 109584My SubmissionsDetermine 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.
Discuss
二.解题技巧
三.实现代码
#include <iostream>#include <vector>using namespace std;class Solution
{
private:bool isExist(int &Input, int Number){if (Input & (1 << Number)){return true;}Input = Input | (1 << Number);return false;}public:bool isValidSudoku(vector<vector<char> > &board){int X_value[9] = {0};int Y_value[9] = {0};int Inside[9] = {0};for (int Index_y = 0; Index_y < 9; Index_y++){vector<char> &TmpRow = board[Index_y];for (int Index_x = 0; Index_x < 9; Index_x++){if (TmpRow[Index_x] == '.'){continue;}int TmpValue = TmpRow[Index_x] - '0';// is valid in Index_x rowif (isExist(X_value[Index_x], TmpValue)){return false;}// is valid in Index_y colif (isExist(Y_value[Index_y], TmpValue)){return false;}// is valid in 3*3 sub blockif (isExist(Inside[Index_x / 3 + Index_y / 3 * 3] , TmpValue)){return false;}}}return true;}
};
四.体会
版权所有,欢迎转载,转载请注明出处,谢谢
这篇关于LeetCode_Valid Sudoku的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!