[leetcode]Valid Sudoku

简单题。先把横的竖的扫一下,然后按照每个3*3的小格扫一遍。

import java.util.HashSet;



public class Solution {

    public boolean isValidSudoku(char[][] board) {

        // Start typing your Java solution below

        // DO NOT write main() function

        if (board.length == 0) return false;

        if (board.length % 3 != 0) return false;

        if (board[0].length != board.length) return false;

        // check length of each row

        for (int i = 0; i < board.length; i++) {

            for (int j = 1; j < board[0].length; j++) {

        		if (board[j].length != board[0].length) return false;

        	}

        }

        

        int len = board.length;

        for (int i = 0; i < len; i++) {

        	HashSet<Character> set1 = new HashSet<Character>();

        	HashSet<Character> set2 = new HashSet<Character>();

        	for (int j = 0; j < len; j++) {

        		char c1 = board[i][j];

        		if (c1 == '.') {}

        		else if (c1 >= '1' && c1 <= '9') {

        			if (set1.contains(c1)) {

        				return false;

        			}

        			set1.add(c1);

        		}

        		else { return false; }

        		char c2 = board[j][i];

        		if (c2 == '.') {}

        		else if (c2 >= '1' && c2 <= '9') {

        			if (set2.contains(c2)) {

        				return false;

        			}

        			set2.add(c2);

        		}

        		else { return false; }

        	}

        }

        

        int len_3 = len / 3;

        

        for (int i = 0; i < len_3; i++) {

        	for (int j = 0; j < len_3; j++) {

        		HashSet<Character> set = new HashSet<Character>();

        		for (int x = 0; x < 3; x++) {

        			for (int y = 0; y < 3; y++) {

        				

        				char c = board[i*3+x][j*3+y];

                		if (c == '.') {}

                		else if (c >= '1' && c <= '9') {

                			if (set.contains(c)) {

                				return false;

                			}

                			set.add(c);

                		}

                		else { return false; }

        			}

        		}

        	}

        }

        return true;

    }

}

看了一下参考,其实一边扫描就可以了。任何一个格子扫完都能把三个判断条件同时往前推进。参考:http://discuss.leetcode.com/questions/215/valid-sudoku

class Solution {

public:

    bool isValidSudoku(vector<vector<char> > &board) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        vector<vector<bool> > rows(9, vector<bool>(9, false));

        vector<vector<bool> > cols(9, vector<bool>(9, false));

        vector<vector<bool> > blocks(9, vector<bool>(9, false));



        for (int i = 0; i < 9; ++i) {

            for (int j = 0; j < 9; ++j) {

                if (board[i][j] == '.') continue;

                int c = board[i][j] - '1';

                if (rows[i][c] || cols[j][c] || blocks[i - i % 3 + j / 3][c])

                    return false;

                rows[i][c] = cols[j][c] = blocks[i - i % 3 + j / 3][c] = true;

            }

        }

        return true;

    }

};

第二刷:

Annie的做法也是只扫一遍,而且用了bit来存,省空间。

你可能感兴趣的:(LeetCode)