Leetcode 36. Valid Sudoku (Easy) (cpp)
来源:互联网 发布:JavaScript中slice 编辑:程序博客网 时间:2024/06/05 02:23
Leetcode 36. Valid Sudoku (Easy) (cpp)
Tag: Hash Table
Difficulty: Easy
/*36. Valid Sudoku (Easy)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) {for (int i = 0; i < 9; i++) {unordered_map<char, bool> mapping1;unordered_map<char, bool> mapping2;unordered_map<char, bool> mapping3;for (int j = 0; j < 9; j++) {if (board[i][j] != '.') {if (mapping1[board[i][j]] == true) { return false; } else { mapping1[board[i][j]] = true; } } if (board[j][i] != '.') { if (mapping2[board[j][i]] == true) { return false; } else { mapping2[board[j][i]] = true; } } if (board[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3] != '.') { if (mapping3[board[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3]] == true) { return false; } else { mapping3[board[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3]] = true; } } } } return true; }};class Solution {public:bool isValidSudoku(vector<vector<char>>& board) {int mapping1[9][9] = {0};int mapping2[9][9] = {0};int mapping3[9][9] = {0};for (int i = 0; i < 9; i++) {for (int j = 0; j < 9; j++) {if (board[i][j] != '.') {int num = board[i][j] - '0' - 1, k = i / 3 * 3 + j / 3;if (mapping1[i][num] || mapping2[j][num] || mapping3[k][num]) { return false; }mapping1[i][num] = mapping2[j][num] = mapping3[k][num] = 1;}}}return true;}};
0 0
- Leetcode 36. Valid Sudoku (Easy) (cpp)
- [Leetcode 36, easy] Valid Sudoku
- 36. Valid Sudoku [easy] (Python)
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
- LeetCode-36-Valid Sudoku(模拟/遍历)-Easy
- leetcode 日经贴,Cpp code -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
- 36. Valid Sudoku LeetCode
- leetcode 36. Valid Sudoku
- LeetCode *** 36. Valid Sudoku
- leetcode 36. Valid Sudoku
- [LeetCode]36. Valid Sudoku
- 初学 python 环境集成
- 严格次小生成树 并查集和数的综合应用
- 编码格式简介(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
- Intellij Idea 在spring-mybatis下因不编译resources配置文件,无法启动tomcat的问题
- 防止Document目录iCloud备份NSURLIsExcludedFromBackupKey
- Leetcode 36. Valid Sudoku (Easy) (cpp)
- NODEJS mongoose 增删改查
- c#设置对象属性——反射实体设置变量
- 入门基础——Git版本控制
- nodejs下的图形处理库gm在windows环境的安装
- Leetcode 349. Intersection of Two Arrays (Easy) (cpp)
- 动物世界
- python list 排序
- Opencv将zed获取的左右视图拆分为左右两幅图像并分别保存