首页 > 代码库 > LeetCode "Valid Sudoku"
LeetCode "Valid Sudoku"
Here another memory for speed implementation:
class Solution {public: bool isValidSudoku(vector<vector<char> > &board) { size_t row_cnt = board.size(); size_t col_cnt = board[0].size(); vector<vector<unordered_set<char>>> subbox_rec; subbox_rec.resize(row_cnt/3); for (int i = 0; i < row_cnt/3; i++) subbox_rec[i].resize(col_cnt/3); vector<unordered_set<char>> row_rec; row_rec.resize(row_cnt); vector<unordered_set<char>> col_rec; col_rec.resize(col_cnt); for (int j = 0; j < row_cnt; j ++) for (int i = 0; i < col_cnt; i++) { char c = board[j][i]; if (c != ‘.‘) { // Row if (row_rec[j].find(c) == row_rec[j].end()) row_rec[j].insert(c); else return false; // Col if (col_rec[i].find(c) == col_rec[i].end()) col_rec[i].insert(c); else return false; // subbox unordered_set<char> &sb = subbox_rec[j / 3][i / 3]; if (sb.find(c) == sb.end()) sb.insert(c); else return false; } } return true; }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。