day 24: 36-valid-sudoku, 128-longest-consecutive-sequence

This commit is contained in:
2023-10-23 09:59:07 -07:00
parent edf7da5e9f
commit 8acacbbc59
6 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
#include "soln.hpp"
int main()
{
vector<vector<char>> board{
{'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'}};
bool answer = true;
Solution soln;
cout << "Validated sudoku correctly? " << soln.test(board, answer) << endl;
return 0;
}

57
36-valid-sudoku/soln.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include "soln.hpp"
bool Solution::isValidSudoku(vector<vector<char>> &board)
{
unordered_map<char, bool> appeared;
// check rows
for (int i = 0; i < 9; i++)
{
appeared.clear();
for (int j = 0; j < 9; j++)
{
char checker = board[i][j];
if (checker == '.')
continue;
if (appeared.find(checker) != appeared.end())
return false;
appeared[checker] = true;
}
}
// check columns
for (int i = 0; i < 9; i++)
{
appeared.clear();
for (int j = 0; j < 9; j++)
{
char checker = board[j][i];
if (checker == '.')
continue;
if (appeared.find(checker) != appeared.end())
return false;
appeared[checker] = true;
}
}
// check grids
for (int i = 0; i < 9; i++)
{
appeared.clear();
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
char checker = board[((i / 3) * 3) + j][((i * 3) % 9) + k];
if (checker == '.')
continue;
if (appeared.find(checker) != appeared.end())
return false;
appeared[checker] = true;
}
}
}
return true;
}
bool Solution::test(vector<vector<char>> &board, bool answer)
{
return isValidSudoku(board) == answer;
}

8
36-valid-sudoku/soln.hpp Normal file
View File

@@ -0,0 +1,8 @@
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool isValidSudoku(vector<vector<char>> &board);
bool test(vector<vector<char>> &board, bool answer);
};