【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.
题解:根据题目的意思,每行每列和每一个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的更多相关文章
- 【leetcode刷题笔记】Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode刷题笔记】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 刷题笔记 2. 有效的括号(Valid Parentheses)
tag: 栈(stack) 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须 ...
- 【leetcode刷题笔记】Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
随机推荐
- spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet
spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet 情况 ...
- jquery.validate.js 验证表单时,在IE当中未验证就直接提交的原因
jquery.validate.js 验证表单时,在IE当中未验证就直接提交的原因 今天利用了jquery.validate.js来验证表单,发现在火狐.谷歌浏览器当中都可以进行验证,但是在IE系列浏 ...
- C#代码用法
1.new的用法using System;using System.Collections.Generic;using System.Text;namespace yanz{public class ...
- Memcache针对不同场景数据应用缓存策略
Memcache主要的作用是为减轻大访问量对数据库的冲击,所以一般的逻辑是首先从memcache中读取数据,如果没有就从数据库中读取数据写入到memcache中,等下一次读取的时候就可以从memcac ...
- Guice 学习(七)常量和属性的注入( Constant and Property Inject)
1.常量注入方式 package com.guice.ConstantInjectDemo; import com.google.inject.Binder; import com.google.in ...
- jquery的json的遍历
jquery遍历解析json对象1: var json = [{dd:'SB',AA:'东东',re1:123},{cccc:'dd',lk:'1qw'}]; for(var i=0,l=json.l ...
- docker教程之从一头雾水到不一头雾水(2)
书接上文:docker教程之从一头雾水到不一头雾水(1) 运行镜像 先查看下本地有哪些镜像 [root@ichz ~]# docker images REPOSITORY TAG IMAGE ID C ...
- [译]GLUT教程 - 修改菜单
Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> Modifying Menus 肯定会有菜单需要被修改的状 ...
- JavaScript函数的中实参个数和形参个数的获取
首先先理解下什么是函数的形参和函数的实参,其实很好理解的,下面举例说明 如何获取形参的长度以及实参的长度 获取实参的长度 可以看到控制台输出的长度是3, 这里有疑问了,arguments是什么那? a ...
- flex hack 记录
IE从IE10开始. //共通 display: flex; flex-direction: column; align-items: flex-start;justify-content: cent ...