题目:

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. SQL 数据库语言分析总结(三)

    这次介绍通过mysql-WorkBench这个工具来管理操作数据库. 创建和删除数据库 1.点击创建数据库按钮 2.选中后右键,出现drop schema一项,这个用来删除. 设置默认数据库 选中右键 ...

  2. 插件开发之360 DroidPlugin源码分析(四)Activity预注册占坑

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52258434 在了解系统的activity,service,broa ...

  3. Spring MVC Junit4 单元测试 JunitTest

    @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/config/spring3/ap ...

  4. 插件开发之360 DroidPlugin源码分析(二)Hook机制

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52124397 前言:新插件的开发,可以说是为插件开发者带来了福音,虽然还很多坑要填补, ...

  5. 插件前奏-android黑科技 hook介绍

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52091833 Android hook相关学习 参考:http://www.cydia ...

  6. Android列表视图ListView和ListActivity-android学习之旅(二十四)

    ListView简介 ListView是android中常用的一种控件,创建ListView有两种方式: 1.在xml中使用ListView控件创建. 2.使用activity继承ListActivi ...

  7. iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...

  8. Freemarker中如何遍历List

     Freemarker中如何遍历List(附源码) 关键词(Keyword):Freemarker,Freemarker遍历list 在Freemarker应用中经常会遍历List获取需要的数据, ...

  9. ROS_Kinetic_14 ROS工具roswtf的基本使用方法等

    ROS_Kinetic_14 ROS工具roswtf的基本使用方法 官网教程:http://wiki.ros.org/cn/ROS/Tutorials/Getting%20started%20with ...

  10. JAVA之旅(十六)——String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较

    JAVA之旅(十六)--String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较 过节耽误了几天,我们继续JAVA之旅 一.String概述 String时 ...