【leetcode刷题笔记】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 board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.



题解:根据题目的意思,每行每列和每一个3*3的九宫格里面1~9这9个数不能有重复的,那么就按行,列,和九宫格一一检查即可,要注意下标的计算和‘.‘符号的处理。

代码如下:

 1 public class Solution {
 2     public boolean isValidSudoku(char[][] board) {
 3         int length = board.length;
 4         if(length == 0)
 5             return true;
 6
 7         for(int i = 0;i < length;i++){
 8             boolean[] row_numbers = new boolean[10];
 9             boolean[] column_numbers = new boolean[10];
10             for(int j = 0;j < length;j++){
11                 //check if rows are valid
12                 if(board[i][j]!= ‘.‘ ){
13                     if(row_numbers[board[i][j] - ‘0‘])
14                         return false;
15                     row_numbers[board[i][j]-‘0‘] = true;
16                 }
17
18                 //check if colums are valid
19                 if(board[j][i]!= ‘.‘){
20                     if(column_numbers[board[j][i]-‘0‘])
21                         return false;
22                     column_numbers[board[j][i]-‘0‘] = true;
23                 }
24             }
25         }
26
27         //check if every 3*3 grid is valid
28         for(int i = 0;i < 3;i++){
29             for(int j = 0;j < 3;j++){
30                 boolean[] numbers = new boolean[10];
31                 for(int row = 3*i;row < 3*i+3;row++){
32                     for(int column = 3*j;column < 3*j+3;column++){
33                         if(board[row][column] != ‘.‘){
34                             if(numbers[board[row][column]-‘0‘])
35                                 return false;
36                             numbers[board[row][column]-‘0‘] = true;
37                         }
38                     }
39                 }
40             }
41         }
42
43         return true;
44     }
45 }

代码中行和列的检查在一次9*9的循环中解决了,可以省一点时间,最终耗时532ms。


时间: 2024-10-09 19:00:04

【leetcode刷题笔记】Valid Sudoku的相关文章

【leetcode刷题笔记】Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 题解:递归.在每个空位

【leetcode刷题笔记】Valid Number

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 题解:题目不难,就是有点麻烦,要注意的地方很多,总结一下: 前面和后面的空格要用s.trim()去掉: 前导的'+'和'-'号需要忽略:

【leetcode刷题笔记】Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leetcode刷题笔记】Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

【leetcode刷题笔记】Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in