码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Valid Sudoku"

时间:2014-08-15 14:18:48      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   ar   div   amp   

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;
    }
};

LeetCode "Valid Sudoku",布布扣,bubuko.com

LeetCode "Valid Sudoku"

标签:style   blog   color   io   for   ar   div   amp   

原文地址:http://www.cnblogs.com/tonix/p/3914574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!