leetcode 之Valid Sudoku(七)

判断行、列、九宫格内数字是否重复。

按照行、列、九宫格进行检查即可。

bool validSudoku(const vector<vector<char>>& board)
      {
          bool used[9];
          for (int i = 0; i < 9; i++)
          {
              fill(used, used + 9, false);

              //检查行
              for (int j = 0; j < 9; j++)
              {
                  if(!check(board[i][j], used))
                      return false;
              }
              fill(used, used + 9, false);
              //检查列
              for (int j = 0; j < 9; j++)
              {
                  if (!check(board[j][i], used))
                      return false;
              }
          }
          //检查9个格子
          for (int i = 0; i < 3; i++)
          {
              for (int j = 0; j < 3; j++)
              {
                  fill(used, used + 9, false);

                  for (int r = 3 * i; r < 3 * i + 3; r++)
                  {
                      for (int c = 3 * j; c < 3 * j + 3; c++)
                      {
                          if (!check(board[i][j], used))
                              return false;
                      }
                  }
              }
          }

          return true;
      }
 bool check(char ch, bool used[9])
      {
          if (ch == ‘.‘) return true;
          if (used[ch - ‘1‘])return false;

          return used[ch - ‘1‘] = true;
      }

时间: 2024-10-01 04:16:46

leetcode 之Valid Sudoku(七)的相关文章

[LeetCode] 036. Valid Sudoku (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ Valid Sudoku 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 sud

【LeetCode】Valid Sudoku

Valid Sudoku 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 boar

leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

Valid Sudoku 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 boa

LeetCode 36 Valid Sudoku (C,C++,Java,Python)

Problem: 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 (

LeetCode 036 Valid Sudoku

题目要求:Valid Sudoku 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

LeetCode(38)-Valid Sudoku

题目: 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 '.'. ![这里写图片描述](http://img.blog.csdn.net/20160409183641502) A partially filled s

leetCode 36. Valid Sudoku(数独) 哈希

36. Valid Sudoku(合法数独) 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 S

Java for LeetCode 036 Valid Sudoku

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 '.'. 解题思路: 传说中的数独(九宫格)问题,老实遍历三个规则即可: JAVA实现: static public boolean isValidSudoku(cha