leetcode Sudoku java

package com.sogou.hadoop.test;

public class Sudoku {
/**验证该值是否合法*/
    public boolean isValidSudoku(char[][] board,int x,int y){
        int row,col;
        //same value in the same column
        for(row=0;row<9;row++){
            if((x!=row)&&(board[row][y]==board[x][y])){
                return false;
            }
        }
        // same value in the same row
        for(col=0;col<9;col++){
            if((y!=col)&&board[x][col]==board[x][y])
                return false;
        }
        // same value in the 3*3 block it belong to
        for(row=(x/3)*3;row<(x/3+1)*3;row++){
            for(col=(y/3)*3;col<(y/3+1)*3;col++){
                if((x!=row)&&y!=col&&board[row][col]==board[x][y])
                    return false;
            }
        }
        return true;

    }//递归调用该方法,没有检测已经填写的数字而是从头开始
    public boolean solveSudoku(char[][] board){
        for(int row=0;row<9;row++){
            for(int col=0;col<9;col++){
                if(‘.‘==board[row][col]){
                    for(int i=1;i<=9;i++){
                        board[row][col]=(char) (‘0‘+i);
                        if(isValidSudoku(board, row, col)){
                            if(solveSudoku(board))
                                return true;
                        }
                        board[row][col]=‘.‘;
                    }
                    return false;
                }
            }
        }
        return true;
    }
    public static void main(String[] args) {

    }
}

leetcode Sudoku java

时间: 2024-11-03 21:39:15

leetcode Sudoku java的相关文章

Linked List Cycle leetcode II java (寻找链表环的入口)

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl

LeetCode: Sudoku Solver [036]

[题目] Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. [题意] 解

leetcode中Java关于Json处理的依赖

leetcode的java代码提供的main函数中,往往有关于json的依赖...我找了许久才找到他们用的是这个json实现 <dependency> <groupId>com.eclipsesource.minimal-json</groupId> <artifactId>minimal-json</artifactId> <version>0.9.5</version> </dependency> 原文地址

一天三题LeetCode(C++&amp;JAVA)-1~3

转载请注明出处: http://blog.csdn.net/miaoyunzexiaobao 1.      Two Sum https://leetcode.com/problems/two-sum/ 给定一个数组,在其中找两个数,使两个数之和为给定的target.返回这两个数在数组中的位置.两个数字的位置分别用index1和index2表示,要求index1<index2.例: Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1,

【leetcode with java】32 Longest Valid Parentheses O(n)

这个题目leetcode上提示用动态规划,但是那样要O(n^2).我自己想出了一个O(n)的算法,并提交通过. [题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring

一天三题LeetCode(C++&amp;JAVA)-4~6

转载请注明出处: http://blog.csdn.net/miaoyunzexiaobao 4.     Median ofTwo Numbers https://leetcode.com/problems/median-of-two-sorted-arrays/ 给定两个有序数组A(长度为m)和B(长度为n),找到两个数组的中位数.若两个数组长度和为偶数,则返回中间两个数的平均值.例:A={1,5,14},B={3,7,30},则返回(5+7)/2=6.A={1,5,14},B={3,7},

leetcode Sudoku Solver python

# Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. class Sol

[LeetCode] Sudoku Solver(迭代)

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 方法:解此问题的关键是

[LeetCode] Sudoku Solver 解数独,递归,回溯

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. Hide Tags B