[LeetCode 36] Valid Sudoku Solution
来源:互联网 发布:淘宝有哪几种营销方式 编辑:程序博客网 时间:2024/06/10 00:02
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.
Ideas: first of all, we need to know what is sudoku board, we could check the blog: Sudoku Puzzles - 九宫格(数独)游戏
Then, whether the soduku board is valid or not based on the following rules.
1) The width and length of the board should 9;
2) In the one row, there is only distinct number (between 1 to 9) or '.';
3) In one column, there is only distinct number (between 1 to 9) or '.';
4) In small 3 * 3 board, it should only contain the distinct number(between 1 to 9) or '.';
The tricks are as followings:(1) how to use vector, (2) use a vector to flag whether the digit are distinct. (3) how to get the small 3*3 subbox.
class Solution {public: bool isValidSudoku(vector<vector<char> > &board) { if(board.size() != 9) return false; //row is valid, column is valid, small board is valid ? //row is valid for(int r = 0; r < 9; r++){ vector<bool> flag (9, false); for(int c = 0; c < 9; c++){ if(board[r][c] == '.') continue; if(isDigit(board[r][c])){ if(flag[(board[r][c] - '0')]) return false; flag[(board[r][c] - '0')] = true; } } } // column is valid? for(int c = 0; c < 9; c++){ vector<bool> flag (9, false); for(int r = 0; r < 9; r++){ if(board[r][c] == '.') continue; if(isDigit(board[r][c])){ if(flag[(board[r][c] - '0')]) return false; flag[(board[r][c] - '0')] = true; } } } //small 3 * 3 is valid? for(int r = 0; r < 9; r += 3){ for(int c = 0; c < 9; c += 3){ //how to set in a 3 * 3 sub-board vector<bool> flag (9, false); for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ if(board[r + i][c + j] == '.') continue; if(isDigit(board[r + i][c + j])){ if(flag[(board[r + i][c + j] - '0')]) return false; flag[(board[r + i][c + j] - '0')] = true; } } } } } return true; }private: bool isDigit(char i){ if (i - '0' < 10 && i - '0' >0) return true ; else return false; }};
- [LeetCode 36] Valid Sudoku Solution
- [leetcode 36] Valid Sudoku
- leetcode 36: Valid Sudoku
- LeetCode(36) Valid Sudoku
- leetcode-36 Valid Sudoku
- Valid Sudoku - LeetCode 36
- LeetCode 36 Valid Sudoku
- LeetCode---(36) Valid Sudoku
- Leetcode 36 Valid Sudoku
- Leetcode[36]-Valid Sudoku
- leetcode 36: Valid Sudoku
- Leetcode# 36 Valid Sudoku
- Leetcode#36||Valid Sudoku
- leetcode-36 Valid Sudoku
- leetcode 36:Valid Sudoku
- LeetCode 36: Valid Sudoku
- [leetcode 36] Valid Sudoku
- 【LEETCODE】36-Valid Sudoku
- Contest of whu 2014.4 D G
- 线段树(一)HDU1166
- [LeetCode 24] Swap Nodes in Pairs
- 静默(延伸)
- [LeetCode 35] Search Insert Position Solution
- [LeetCode 36] Valid Sudoku Solution
- javaAPI-集合框架(泛型)
- [Codeforces]Coder-Strike 2014 - Round 1
- shell程序之逐行读取一文件中的参数且使用此参数每次执行5分钟
- Coder-Strike 2014 - Round 1
- c++ 调用 python 实例 涉及 类 多参数 列表作为参数
- Mint linux中调整屏幕亮度的方法
- LA 4986 hdu3756 Dome of Circus 三分答案
- javaAPI-集合