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个数不能有重复的,那么就按行,列,和九宫格一一检查即可,要注意下标的计算和'.'符号的处理。

代码如下:

 public class Solution {
public boolean isValidSudoku(char[][] board) {
int length = board.length;
if(length == 0)
return true; for(int i = 0;i < length;i++){
boolean[] row_numbers = new boolean[10];
boolean[] column_numbers = new boolean[10];
for(int j = 0;j < length;j++){
//check if rows are valid
if(board[i][j]!= '.' ){
if(row_numbers[board[i][j] - '0'])
return false;
row_numbers[board[i][j]-'0'] = true;
} //check if colums are valid
if(board[j][i]!= '.'){
if(column_numbers[board[j][i]-'0'])
return false;
column_numbers[board[j][i]-'0'] = true;
}
}
} //check if every 3*3 grid is valid
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
boolean[] numbers = new boolean[10];
for(int row = 3*i;row < 3*i+3;row++){
for(int column = 3*j;column < 3*j+3;column++){
if(board[row][column] != '.'){
if(numbers[board[row][column]-'0'])
return false;
numbers[board[row][column]-'0'] = true;
}
}
}
}
} return true;
}
}

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


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

  1. 【leetcode刷题笔记】Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  3. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  4. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  5. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  6. 【leetcode刷题笔记】Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. 【leetcode刷题笔记】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. LeetCode 刷题笔记 2. 有效的括号(Valid Parentheses)

    tag: 栈(stack) 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须 ...

  9. 【leetcode刷题笔记】Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

随机推荐

  1. Nginx之红黑树

    /*  * Copyright (C) Igor Sysoev  * Copyright (C) Nginx, Inc.  */ #ifndef _NGX_RBTREE_H_INCLUDED_ #de ...

  2. HTML5 的四个亮点

    1.XDM  cross-document-messaging  跨文档消息传递. 2.原生拖放功能. 3.新媒体元素 audio.video. 4.历史状态管理.

  3. CSRF--花式绕过Referer技巧

    CSRF遇到Referer绕过的情况,有条件限制,不一定所有的Refere验证就可以绕过 1.Refere为空条件下 解决方案: 利用ftp://,http://,https://,file://,j ...

  4. spring download

    http://maven.springframework.org/release/org/springframework/spring/

  5. POJ 1654 area 解题

    Description You are going to compute the area of a special kind of polygon. One vertex of the polygo ...

  6. php使用imagick模块实现图片缩放、裁剪、压缩示例

    PHP 使用Imagick模块 缩放,裁剪,压缩图片 包括gif图片 缩放 裁剪 复制代码代码如下: /**  * 图片裁剪  * 裁剪规则:  *   1. 高度为空或为零   按宽度缩放 高度自适 ...

  7. k8s调度-指定node

    1.给node加标签 kubectl label nodes k8s-slave2 slave= 2.查看标签 [root@k8s_master centos7]# kubectl describe ...

  8. golang struct 定义中json``解析说明

    在代码学习过程中,发现struct定义中可以包含`json:"name"`的声明,所以在网上找了一些资料研究了一下 package main import ( "enco ...

  9. Linux中的关机

    我是用普通用户登录,在终端下输入shutdown命令,结果显示 command not found.这就奇怪了,难道我的linux不支持这个命令?man了一下shutdown,大篇幅的说明告诉我,我的 ...

  10. K-Piggy-Bank

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...