389. Valid Sudoku【LintCode java】
Description
Determine whether a Sudoku is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character ..
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
Clarification
Example
The following partially filed sudoku is valid.

解题:判断数独是否有效。如果每一行、每一列、每一个小方块里的数不重复,就算有效。当然,前提必须是1~9之间。此题空白部分是用“ . ”表示的,要注意一下。具体做法为,检查行、列、方块,把这些数拿出来,放在一个一维数组中,判断这个一维数组里的数,满不满足相应的条件。代码如下:
public class Solution {
/**
* @param board: the board
* @return: whether the Sudoku is valid
*/
public boolean isValidSudoku(char[][] board) {
// write your code here
char[]temp = new char[9];
for(int i = 0; i < 9; i++){
//检查行
for(int j = 0; j < 9; j++){
temp[j] = board[i][j];
}
if(judge(temp) == false){
return false;
}
//检查列
for(int j = 0; j < 9; j++){
temp[j] = board[j][i];
}
if(judge(temp) == false){
return false;
}
}
//小格子
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
int b = 0;
for(int k = 0 + i * 3; k < 3 + i * 3; k++){
for(int m = 0 + j * 3; m < 3 + j * 3; m++){
temp[b++] = board[k][m];
}
}
if(judge(temp) == false)
return false;
}
}
return true;
}
public boolean judge(char[]temp){
for(int i =0; i < 9; i++){
if(temp[i] > '9' || temp[i] < '1' ){
if(temp[i] != '.')
return false;
}
}
for(int i = 1; i < 9; i++){
for(int j = 0; j < i; j++){
if(temp[i] == temp[j] ){
if(temp[i] != '.')
return false;
}
}
}
return true;
}
}
389. Valid Sudoku【LintCode java】的更多相关文章
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
- 376. Binary Tree Path Sum【LintCode java】
Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
随机推荐
- MacBook常用快捷键
MacBook常用快捷键: 1. 窗口操作: cmd+n 新建一个窗口/文件. cmd+m 窗口最小化. cmd+w 关闭当前窗口/文件. 2. 程序操作: cmd+q 退出当前程序,后台不运行该程序 ...
- HDU 1735 字数统计(模拟+一点点贪心的思想)
题目戳我 字数统计 Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Java中,如何跳出当前的多重嵌套循环
在最外层循环前加一个标记如A,然后用break A;可以跳出多重循环.(Java中支持带标签的break和continue语句)
- Vue--- 一点车项目 6个小时实际看了10天(完结)
一个项目 环境安装 使用了 cli 脚手架 Koa2 workpackage 其他小的不计 前端Vue组件搭建 数据的简单测试交互 数据库的设计 创建.连接接数据库 前台[表单/分类] ...
- vue-router笔记
1.vue-router 安装 在安装webpack模块时就安装了 eg: vue init webpack demo (安装webpack模块并取名为demo) 在安装模块时没有安装的话 ...
- 【POJ 3368】Frequent values(RMQ)
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- Linux下Git远程仓库的使用详解
Git远程仓库Github 提示:Github网站作为远程代码仓库时的操作和本地代码仓库一样的,只是仓库位置不同而已! 准备Git源代码仓库 https://github.com/ 准备经理的文件 D ...
- 【读书笔记 - Effective Java】02. 遇到多个构造器参数时要考虑用构建器
类有多个可选参数的解决方案: 1. 重叠构造器模式可行,但是当有许多参数的时候,客户端代码会很难编写,并且仍然较难以阅读. 2. JavaBeans模式,调用一个无参构造器来创造对象,然后调用sett ...
- IoC和AOP扩展
一.构造注入 二.使用p命名空间注入属性值 三.注入不同数据类型 <?xml version="1.0" encoding="UTF-8"?> &l ...
- 对于gitHub的总结随笔
作用:用于项目的版本管理 密切相关的是 git 操作 1.本地的文件上传到github上 ...