新博文跟本文算法一样,为了习惯 -> 新博文地址:[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.
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.
这道题不会,最后几乎全是copy这位大神的算法。菜比了,不会玩数独,也不知道这里所谓的valid是指开盘的摆法合法?还是指这盘数独有解?不过看了上面的算法,答案是指前者,是不是说只要开盘时的摆法合法,则必有解?如果不一定,那么,死局怎么能叫valid呢?
不管了,具体到这道题,这需要检查开局的摆法是否合法,这个相对比较简单,比较费脑的是block的下标略风骚。还是有必要记一下的,直接看代码吧。
public boolean isValidSudoku(char[][] board){ boolean [][] rows=new boolean[9][9]; boolean [][] cols=new boolean[9][9]; boolean [][] blocks=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; if (rows[i][c] || cols[j][c] || blocks[i - i % 3 + j / 3][c]) return false; rows[i][c] = cols[j][c] = blocks[i - i % 3 + j / 3][c] = true; } } return true; }
相关推荐
LeetCode Valid Parenthese解决方案
噪声 leetcode
leetcode 答案 Sudoku ref: UserInterface IUserInterfaceContract:前端与后端的交互接口 EventListener:处理每次输入时的接口 View:更新前端以及消息提示的接口 SudokuTextField:继承了JavaFX中的TextField接口...
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
sudoku example 做leetcode37的时候看到的解数独的非常棒的方法。在简单回溯的基础上还加上了限制条件,会更快。主要参考 . 自己在main里面加上了一点从txt里读写,这样可以自己敲进去题目然后得到答案,比只是...
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
vs code LeetCode 插件
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
leetcode中文版
大佬的leetcode刷题笔记(c++版本)
100个leetCode详细解答
LeetCode 刷题汇总1
LeetCode 选讲1
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
leetcode刷题, 直接用leetcode的分类方式.
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest Substring Without Repeating ...
leetcode高频面试笔试题150+道,亲身总结。
LeetCode 刷题笔记
LeetCode面试笔试题