规范格式

This commit is contained in:
YDZ
2020-08-07 15:50:06 +08:00
parent 854a339abc
commit 4e11f4028a
1438 changed files with 907 additions and 924 deletions

View File

@ -0,0 +1,85 @@
package leetcode
import "strconv"
// 解法一 暴力遍历,时间复杂度 O(n^3)
func isValidSudoku(board [][]byte) bool {
// 判断行 row
for i := 0; i < 9; i++ {
tmp := [10]int{}
for j := 0; j < 9; j++ {
cellVal := board[i][j : j+1]
if string(cellVal) != "." {
index, _ := strconv.Atoi(string(cellVal))
if index > 9 || index < 1 {
return false
}
if tmp[index] == 1 {
return false
}
tmp[index] = 1
}
}
}
// 判断列 column
for i := 0; i < 9; i++ {
tmp := [10]int{}
for j := 0; j < 9; j++ {
cellVal := board[j][i]
if string(cellVal) != "." {
index, _ := strconv.Atoi(string(cellVal))
if index > 9 || index < 1 {
return false
}
if tmp[index] == 1 {
return false
}
tmp[index] = 1
}
}
}
// 判断 9宫格 3X3 cell
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
tmp := [10]int{}
for ii := i * 3; ii < i*3+3; ii++ {
for jj := j * 3; jj < j*3+3; jj++ {
cellVal := board[ii][jj]
if string(cellVal) != "." {
index, _ := strconv.Atoi(string(cellVal))
if tmp[index] == 1 {
return false
}
tmp[index] = 1
}
}
}
}
}
return true
}
// 解法二 添加缓存,时间复杂度 O(n^2)
func isValidSudoku1(board [][]byte) bool {
rowbuf, colbuf, boxbuf := make([][]bool, 9), make([][]bool, 9), make([][]bool, 9)
for i := 0; i < 9; i++ {
rowbuf[i] = make([]bool, 9)
colbuf[i] = make([]bool, 9)
boxbuf[i] = make([]bool, 9)
}
// 遍历一次,添加缓存
for r := 0; r < 9; r++ {
for c := 0; c < 9; c++ {
if board[r][c] != '.' {
num := board[r][c] - '0' - byte(1)
if rowbuf[r][num] || colbuf[c][num] || boxbuf[r/3*3+c/3][num] {
return false
}
rowbuf[r][num] = true
colbuf[c][num] = true
boxbuf[r/3*3+c/3][num] = true // r,c 转换到box方格中
}
}
}
return true
}

View File

@ -0,0 +1,79 @@
package leetcode
import (
"fmt"
"testing"
)
type question36 struct {
para36
ans36
}
// para 是参数
// one 代表第一个参数
type para36 struct {
s [][]byte
}
// ans 是答案
// one 代表第一个答案
type ans36 struct {
s bool
}
func Test_Problem36(t *testing.T) {
qs := []question36{
question36{
para36{[][]byte{
[]byte{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
[]byte{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
[]byte{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
[]byte{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
[]byte{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
[]byte{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
[]byte{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
[]byte{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
[]byte{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
ans36{true},
},
question36{
para36{[][]byte{
[]byte{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
[]byte{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
[]byte{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
[]byte{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
[]byte{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
[]byte{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
[]byte{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
[]byte{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
[]byte{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
ans36{false},
},
question36{
para36{[][]byte{
[]byte{'.', '8', '7', '6', '5', '4', '3', '2', '1'},
[]byte{'2', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'3', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'4', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'5', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'6', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'7', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'8', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'9', '.', '.', '.', '.', '.', '.', '.', '.'}}},
ans36{true},
},
}
fmt.Printf("------------------------Leetcode Problem 36------------------------\n")
for _, q := range qs {
_, p := q.ans36, q.para36
fmt.Printf("【input】:%v 【output】:%v\n", p, isValidSudoku1(p.s))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,75 @@
# [36. Valid Sudoku](https://leetcode.com/problems/valid-sudoku/)
## 题目:
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**:
1. Each row must contain the digits `1-9` without repetition.
2. Each column must contain the digits `1-9` without repetition.
3. Each of the 9 `3x3` sub-boxes of the grid must contain the digits `1-9` without repetition.
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character `'.'`.
**Example 1:**
Input:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true
**Example 2:**
Input:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
**Note:**
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
- The given board contain only digits `1-9` and the character `'.'`.
- The given board size is always `9x9`.
## 题目大意
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
1. 数字 1-9 在每一行只能出现一次。
2. 数字 1-9 在每一列只能出现一次。
3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。
## 解题思路
- 给出一个数独的棋盘,要求判断这个棋盘当前是否满足数独的要求:即行列是否都只包含 1-9每个九宫格里面是否也只包含 1-9 。
- 注意这题和第 37 题是不同的,这一题是判断当前棋盘状态是否满足数独的要求,而第 37 题是要求求解数独。本题中的棋盘有些是无解的,但是棋盘状态是满足题意的。