LeetCode 36. Valid Sudoku(数独游戏)
2016-05-20 10:38
465 查看
原题网址:https://leetcode.com/problems/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.
方法:按照行、列和格进行哈希统计。
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.
方法:按照行、列和格进行哈希统计。
public class Solution { public boolean isValidSudoku(char[][] board) { boolean[][] row = new boolean[9][9]; boolean[][] col = new boolean[9][9]; boolean[][][] grid = new boolean[3][3][9]; for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { char ch = board[i][j]; if (ch == '.') continue; if (row[i][ch-'1']) return false; else row[i][ch-'1']=true; if (col[j][ch-'1']) return false; else col[j][ch-'1']=true; if (grid[i/3][j/3][ch-'1']) return false; else grid[i/3][j/3][ch-'1']=true; } } return true; } }
相关文章推荐
- 找出一个只出现一次的字符
- Murano Weekly Meeting 2016.05.17
- 工具类与工具函数 —— fatal.h
- Mycat分布式事务的实现
- CSS实现曲面阴影效果
- 万能启动的最后的传说:UD三分区法部署教程!UD加UEFI!
- 手把手教你写框架之ShykyAnnotations - 原理篇
- Python FTP实现
- 关于父类私有属性在子类构造函数中super调用的解释
- 人机交互或是未来智能家居的决胜关键 AR(增强现实)或是智能家居的未来
- GitHub 排名前 100 的安卓、iOS项目简介
- MySQL 同步表
- Ethernet frame
- Java多线程之wait(),notify(),notifyAll()
- Is iOS 7 Multipeer Connectivity compatible with Android Wi-Fi Direct?
- 深入Java虚拟机学习笔记 - 内存分配和回收策略
- Java中TreeSet的用法
- android中 拍照 视频 录音 显示缩略图
- LeetCode 35. Search Insert Position(搜索插入点)
- 转 linux任务调度之crontab命令