36. Valid Sudoku

https://leetcode.com/problems/valid-sudoku/discuss/15472/Short+Simple-Java-using-Strings

 

 

 

 1 //SLOW
 2 class Solution {
 3     public boolean isValidSudoku(char[][] board) {
 4         for(int i = 0; i < board.length; i++) {
 5             HashMap<Character, Integer> map = new HashMap<>();
 6             for(int j = 0; j < board[0].length; j++) {
 7                 if(map.containsKey(board[i][j])) {
 8                     return false;
 9                 }else if(board[i][j] != '.'){
10                     map.put(board[i][j], 1);
11                 }
12             }
13         }
14         
15         for(int i = 0; i < board.length; i++) {
16             HashMap<Character, Integer> map = new HashMap<>();
17             for(int j = 0; j < board[0].length; j++) {
18                 if(map.containsKey(board[j][i])) {
19                     return false;
20                 }else if(board[j][i] != '.'){
21                     map.put(board[j][i], 1);
22                 }
23             }
24         }
25         
26         for(int i = 0; i < board.length; i += 3) {
27             for(int j = 0; j < board[0].length; j += 3) {
28                 HashMap<Character, Integer> map = new HashMap<>();
29                 if(map.containsKey(board[i][j])) {
30                     return false;
31                 }else if(board[i][j] != '.'){
32                     map.put(board[i][j], 1);
33                 }
34                 if(map.containsKey(board[i][j+1])) {
35                     return false;
36                 }else if(board[i][j+1] != '.'){
37                     map.put(board[i][j+1], 1);
38                 }
39                 if(map.containsKey(board[i][j+2])) {
40                     return false;
41                 }else if(board[i][j+2] != '.'){
42                     map.put(board[i][j+2], 1);
43                 }
44                 if(map.containsKey(board[i+1][j])) {
45                     return false;
46                 }else if(board[i+1][j] != '.'){
47                     map.put(board[i+1][j], 1);
48                 }
49                 if(map.containsKey(board[i+1][j+1])) {
50                     return false;
51                 }else if(board[i+1][j+1] != '.'){
52                     map.put(board[i+1][j+1], 1);
53                 }
54                 if(map.containsKey(board[i+1][j+2])) {
55                     return false;
56                 }else if(board[i+1][j+2] != '.'){
57                     map.put(board[i+1][j+2], 1);
58                 }
59                 if(map.containsKey(board[i+2][j])) {
60                     return false;
61                 }else if(board[i+2][j] != '.'){
62                     map.put(board[i+2][j], 1);
63                 }
64                 if(map.containsKey(board[i+2][j+1])) {
65                     return false;
66                 }else if(board[i+2][j+1] != '.'){
67                     map.put(board[i+2][j+1], 1);
68                 }
69                 if(map.containsKey(board[i+2][j+2])) {
70                     return false;
71                 }else if(board[i+2][j+2] != '.'){
72                     map.put(board[i+2][j+2], 1);
73                 }
74             }
75         }
76         return true;
77     }
78 }

 

posted @ 2018-09-10 09:34  jasoncool1  阅读(96)  评论(0编辑  收藏  举报