题目:

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 '.'.

![这里写图片描述](http://img.blog.csdn.net/20160409183641502)
A partially filled sudoku which is valid.

A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

思路:

  • 首先这是一道数独题目,关于数独的性质,可以参考下面的链接

    数独规则

  • 考虑根据数独的各个条件来逐个解决,需要判断每行,每一列,以及每个小方格是否是1~9,出现一次,思路是建立一个list添加,判断是否出现重复,出现返回false;同样如果board == null,或者行数和列数不等于9,也返回false

  • -

代码:

public class Solution {
    public boolean isValidSudoku(char[][] board) {
        ArrayList<ArrayList<Character>> rows = new ArrayList<ArrayList<Character>>();
        ArrayList<ArrayList<Character>> cols = new ArrayList<ArrayList<Character>>();
        ArrayList<ArrayList<Character>> boxs = new ArrayList<ArrayList<Character>>();
        if(board == null || board.length != 9|| board[0].length != 9){
            return false;
        }
        for(int i = 0;i < 9;i++){
            rows.add(new ArrayList<Character>());
            cols.add(new ArrayList<Character>());
            boxs.add(new ArrayList<Character>());
        }
        for(int a = 0;a < board.length;a++){
            for(int b = 0;b < board[0].length;b++){
                if(board[a][b] == '.'){
                    continue;
                }
                ArrayList<Character> row = rows.get(a);
                if(row.contains(board[a][b])){
                    return false;
                }else{
                    row.add(board[a][b]);
                }
                ArrayList<Character> col = cols.get(b);
                if(col.contains(board[a][b])){
                    return false;
                }else{
                    col.add(board[a][b]);
                }
                ArrayList<Character> box = boxs.get(getNum(a,b));
                if(box.contains(board[a][b])){
                    return false;
                }else{
                    box.add(board[a][b]);
                }
            }
        }return true;
    }
    public int getNum(int i,int j){
        return (i/3)*3+j/3;
    }
}

LeetCode(38)-Valid Sudoku的更多相关文章

  1. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  2. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  3. LeetCode 36 Valid Sudoku

    Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...

  4. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  5. Java [leetcode 36]Valid Sudoku

    题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  6. [leetcode] 20. Valid Sudoku

    这道题目被放在的简单的类别里是有原因的,题目如下: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. ...

  7. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  8. leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  9. LeetCode 036 Valid Sudoku

    题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...

随机推荐

  1. FFmpeg的H.264解码器源代码简单分析:概述

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  2. Objc中为何某些类的属性要设置为copy而不是strong?

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 不知道大家是否注意,我们再使用一些第三方类的时候大多数情况下对 ...

  3. 【Netty源码学习】入门示例

    Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序.        也就是说,Netty ...

  4. 开源项目——小Q聊天机器人V1.5

    小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...

  5. Android初级教程理论知识(第二章布局&读写文件)

    常见布局 相对布局 RelativeLayout 组件默认左对齐.顶部对齐 设置组件在指定组件的右边 android:layout_toRightOf="@id/tv1" 设置在指 ...

  6. UNIX环境高级编程——信号说明列表

    $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGK ...

  7. 【转载】2015 Objective-C 三大新特性 | 干货

    Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的热情又一次高涨起来,在羡慕创业公司的朋友们大谈 Swift 新特性的同时,也有很多像我一样工作上依然 ...

  8. Chapter 2 User Authentication, Authorization, and Security(10):创建包含数据库

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/39473895,专题目录:http://blog.csdn.net/dba_huangzj ...

  9. Chapter 2 User Authentication, Authorization, and Security(5):使用固定服务器角色

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/38844999,专题目录:http://blog.csdn.net/dba_huangzj ...

  10. VC和GCC静态变量析构顺序不同

    VC和GCC静态变量析构顺序不同(金庆的专栏)静态变量析构顺序正常情况下是构造的反序.但是VC对DLL中的静态变量好像是需等待DLL卸载时再析构,表现为主程序中的静态变量先析构,DLL中的静态变量后析 ...