LeetCode第[36]题(Java):Valid Sudoku

时间:2022-10-03 13:20:02

题目:有效的数独表

难度:Medium

题目内容

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.

LeetCode第[36]题(Java):Valid Sudoku
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.

翻译:简单翻译下就是,给出一张数独表,判断是否有效。

不需要判断是否有解,只需判断它的三个标准内已经出现的数字是否唯一即可

我的思路:本题其实不难,不过题目的意思很难懂,仔细理解了就好了。

    在三个标准内判断是否唯一,那么就需要三个数组,并且每一个数组都应该是二维的,第一维表示是第几个长条(方形),第二维度表示是长条(方形)内的第几个元素。

    三个标准分别是,行、列、块。

    对数独表的每一个元素进行循环,每一个元素都在三个标准内都有对应的元素,每次循环都对此三个标准内对应的值设置为true(已经使用),如果发现对应的值已经被设置为true,则立即返回flase表明此表是无效的。

    当前值减去“1”则能表示第二维度的值

问题在于如何用元素在数独表的位置【i】【j】表示三个标准第一维度的值:

行:【i】

列:【j】

块:  行数与列数不一样,行数对三的商决定了是从第几个三开始往后数,列数决定了数几个所以为【i/3*3 + j/3】

MyCode

     public boolean isValidSudoku(char[][] board) {

        boolean[][] row = new boolean[9][9];
boolean[][] column = new boolean[9][9];
boolean[][] block = new boolean[9][9]; for(int i = 0;i<9;i++){
for(int j=0;j<9;j++){
int c = board[i][j] - '1';
if(board[i][j]=='.'){
continue;
}
int loc = i/3*3 + j/3;
if(row[i][c]||column[j][c]||block[loc][c]){
return false;
}
row[i][c] = column[j][c] = block[loc][c] = true;
}
}
return true;
}

我的算法复杂度:O(N2

编码过程中出现的问题

1、一开始想用一维数组,然后每个元素都是set,用set来判断,但是发现操作起来比较麻烦。当已经知道要放入的元素的范围,且范围不大的时候应该用数组代替Set。

2、想第二维度如何取值的时候想了很久。当元素为char或者String的数字,用来计数(判唯一)的时候,可以考虑减去“<最小值>”,转为相对值。此处最小值为1。

答案代码

略,和我的一样。

LeetCode第[36]题(Java):Valid Sudoku的更多相关文章

  1. LeetCode第&lbrack;18&rsqb;题&lpar;Java&rpar;:4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  2. LeetCode第&lbrack;1&rsqb;题&lpar;Java&rpar;:Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. LeetCode第&lbrack;46&rsqb;题&lpar;Java&rpar;:Permutations&lpar;求所有全排列&rpar; 含扩展——第&lbrack;47&rsqb;题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. LeetCode第&lbrack;1&rsqb;题&lpar;Java&rpar;:Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode之&OpenCurlyDoubleQuote;散列表”:Valid Sudoku

    题目链接 题目要求: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boar ...

  6. 【Leetcode】【Easy】Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  7. leetcode第36题--Sudoku Solver

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  8. LeetCode第&lbrack;20&rsqb;题&lpar;Java&rpar;:Valid Parentheses

    题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...

  9. LeetCode第&lbrack;4&rsqb;题&lpar;Java&rpar;:Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

随机推荐

  1. 【CodeForces 577C】Vasya and Petya’s Game

    链接 某个数x属于[1,n],至少询问哪些数“x是否是它的倍数”才能判断x.找出所有质因数和质因数的幂即可. #include<cstdio> #include<algorithm& ...

  2. Android成长日记-使用PagerAdapter实现页面切换

    Tip:此方式可以实现页面切换 1. 创建view1.xml,view2.xml,view3.xml,main.xml 在main.xml中创建 <android.support.v4.view ...

  3. codeforces 476C&period;Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  4. html 4&period;01速查手册

    来自 W3School 的 HTML 快速参考.可以打印它,以备日常使用. HTML Basic Document <html> <head> <title>Doc ...

  5. Spring&lpar;一&rpar;——总体介绍

           spring框架,是进行对象管理,对象关联,解耦的一个中间层框架.SSH(Struts+Spring+hibernate)三大Spring在中间就起着一个承上启下的作用.好,首先我们先来 ...

  6. 《Programming WPF》翻译 第5章 1&period;不使用样式

    原文:<Programming WPF>翻译 第5章 1.不使用样式 作为一个样式如何使其在WPF使用的例子,,让我们看一下TTT简单的实现,如示例5-1. 示例5-1 <!-- W ...

  7. JS with

    <script type="text/javascript"> function Dog(){ this.type="dog"; this.tail ...

  8. DevExpress VCL 的 cxDBTreeList 的使用方法

    DevExpress VCL 的 cxDBTreeList 的使用方法:(假设控件名为: WBSTree) 1.控件WBSTree 通过绑定  DataSet 获取数据记录(Nodes),通过 Col ...

  9. python中不同文件中函数和类的调用

    最近在学习Python的时候,遇到了一个不同文件中类无法调用的问题,搜了很多,发现很多人针对 这个问题都说的相当含糊,让我费了好大劲才把这个东东搞明白.记录一下,权且温习. 调用分两种,一种是同种文件 ...

  10. 华为机试-iNOC产品部-杨辉三角的变形

    题目描述 1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 11 4 10 16 19 16 10 4 1以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数,左上角数 ...