# 36. Valid Sudoku

## 1.問題&#x20;

* 在數獨棋盤中擺位置
  * row中不可有重複數字
  * col中不可有重複數字
  * 3 \* 3的方格中不可有重複數字

![](/files/-LO33hOfyNg4xRC4PhYS)

## 2.想法 <a href="#id-2-xiang-fa" id="id-2-xiang-fa"></a>

* 提問
  * 時間複雜度與可用空間是否有限制?&#x20;
* function header, parameter
* test input
* 說明想法
* 測試計算複雜度

## **3.程式碼** <a href="#id-3-cheng-shi" id="id-3-cheng-shi"></a>

```
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        for (int i = 0; i < 3; i++){
            for (int j = 0; j < 3; j++){
                if (!isValidWindow(board, i * 3, j * 3)) {
                    return false;
                }
            }
        }
        for (int i = 0; i < 9; i++){
            if (!isValidRow(board, i)) {
               return false;
            }
            if (!isValidCol(board, i)) {
               return false;
            }
        }
        return true;
    }
private:
    bool isValidWindow(vector<vector<char>> board, int startX, int startY) {
        vector<int> window(10, 0);
        for (int i = startX; i < startX + 3; i++) {
            for (int j = startY; j < startY +3; j++) {
                int index = int(board[i][j] - '0');
                if(index <= 9 && index >= 1) {   
                    if (window[index]) {
                        return false;
                    } else {
                        window[index]++;
                    }
                }
            }
        }
        return true;
    }
    
    bool isValidRow(vector<vector<char>> board, int start) {
        vector<int> window(10, 0);
        for (int i = 0; i < 9; i++) {
            int index = int(board[start][i] - '0');
            if(index <= 9 && index >= 1) {   
                if (window[index]) {
                    return false;
                } else {
                    window[index]++;
                }
            }
        }
        
        return true;
    }
    
    bool isValidCol(vector<vector<char>> board, int start) {
        vector<int> window(10, 0);
        for (int i = 0; i < 9; i++) {
            int index = int(board[i][start] - '0');
            if(index <= 9 && index >= 1) {   
                if (window[index]) {
                    return false;
                } else {
                    window[index]++;
                }
            }
        }
        
        return true;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/36.-valid-sudoku.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
