GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/algorithms/cpp/validSudoku/validSudoku.cpp at master · weimo/leetcode

Expand file tree

/

validSudoku.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
56 lines (50 loc) · 1.77 KB
/

validSudoku.cpp

Copy path

File metadata and controls

56 lines (50 loc) · 1.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Source : https://oj.leetcode.com/problems/valid-sudoku/
// Author : Hao Chen
// Date : 2014-07-17
/**********************************************************************************
*
* 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.
*
*
**********************************************************************************/
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
const int cnt = 9;
bool row_mask[cnt][cnt] = {false};
bool col_mask[cnt][cnt] = {false};
bool area_mask[cnt][cnt] = {false};
//check each rows and cols
for(int r=0; r<board.size(); r++){
for (int c=0; c<board[r].size(); c++){
if (!isdigit(board[r][c])) continue;
int idx = board[r][c] - '0' - 1;
//check the rows
if (row_mask[r][idx] == true){
return false;
}
row_mask[r][idx] = true;
//check the cols
if (col_mask[c][idx] == true) {
return false;
}
col_mask[c][idx] = true;
//check the areas
int area = (r/3) * 3 + (c/3);
if (area_mask[area][idx] == true) {
return false;
}
area_mask[area][idx] = true;
}
}
return true;
}
};
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.