zoukankan      html  css  js  c++  java
  • 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;
        }
    };
  • 相关阅读:
    反射
    IO流
    集合(下)
    集合(上)
    泛型
    异常
    常用类
    内部类
    将博客搬至CSDN
    DBMS_ERRLOG记录DML错误日志(二)
  • 原文地址:https://www.cnblogs.com/tonix/p/3914574.html
Copyright © 2011-2022 走看看