Leetcode-valid Sudoku

Source: Internet
Author: User

judge whether the rule of Sudoku is met. Sudoku rule: Each row can not have duplicate numbers, each of the 3x3 squares must not have duplicate numbers, but the problem may be empty is '. '.

(to develop good programming habits ah, or a little low-level error is not easy to find, wasting life!) )

public class Solution {public boolean Isvalidsudoku (char[][] board) {StringBuilder str = new StringBuilder (""); rowfor (int i=0; i<9; i++) {for (int j=0; j<9; J + +) {if (board[i][j]! = '. ') {str.append (board[i][j]);}} if (Isonce (str.tostring ())) {str.delete (0, Str.length ());} else {return false;}} colfor (int i=0; i<9; i++) {for (int j=0; j<9; J + +) {if (board[j][i]! = '. ') {str.append (board[j][i]);}} if (Isonce (str.tostring ())) {str.delete (0, Str.length ());} else {return false;}} 3x3for (int cubex=0; cubex<=6; cubex+=3) {for (int cubey=0; cubey<=6; cubey+=3) {if (!iscubeonce, board, Cubey)) {return false;}}}    return true; }public Static Boolean iscubeonce (char[][] charr, int i, int j) {StringBuilder str = new StringBuilder (""); for (int ii=i; ii<i+3; ii++) {for (int jj=j; jj<j+3; jj++) {if (CHARR[II][JJ]! = '. ') Str.append (CHARR[II][JJ]);}} if (Isonce (str.tostring ())) {return true;} else {return false;}} public static Boolean isonce (String s) {Java.util.set<character> set = new Java.util.hashset<> (), for (int i=0; i<s.length (); i++) {Set.add (S.charat (i));} Return Set.size () ==s.length ();}}

Leetcode-valid Sudoku

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.