LeetCode: Valid Sudoku

一次过。。

 1 class Solution {

 2 public:

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

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

 5         // DO NOT write int main() function

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

 7             bool flag[9] = {false};

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

 9                 if (board[i][j] != '.') {

10                     if (!flag[board[i][j]-'1']) flag[board[i][j]-'1'] = true;

11                     else return false;

12                 }

13             }

14         }

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

16             bool flag[9] = {false};

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

18                 if (board[i][j] != '.') {

19                     if (!flag[board[i][j]-'1']) flag[board[i][j]-'1'] = true;

20                     else return false;

21                 }

22             }

23         }

24         for (int k = 0; k < 9; k++) {

25             bool flag[9] = {false};

26             for (int i = 0; i < 3; i++) {

27                 for (int j = 0; j < 3; j++) {

28                     int x = k / 3 * 3 + i;

29                     int y = k % 3 * 3 + j;

30                     if (board[x][y] != '.') {

31                         if (!flag[board[x][y]-'1']) flag[board[x][y]-'1'] = true;

32                         else return false;

33                     }

34                 }

35             }

36         }

37         return true;

38     }

39 };

 C#

 1 public class Solution {

 2     public bool IsValidSudoku(char[,] board) {

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

 4             bool[] flag = new bool[9];

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

 6                 if (board[i, j] != '.') {

 7                     if (!flag[board[i, j]-'1']) flag[board[i, j]-'1'] = true;

 8                     else return false;

 9                 }

10             }

11         }

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

13             bool[] flag = new bool[9];

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

15                 if (board[i, j] != '.') {

16                     if (!flag[board[i, j]-'1']) flag[board[i, j]-'1'] = true;

17                     else return false;

18                 }

19             }

20         }

21         for (int k = 0; k < 9; k++) {

22             bool[] flag = new bool[9];

23             for (int i = 0; i < 3; i++) {

24                 for (int j = 0; j < 3; j++) {

25                     int x = k / 3 * 3 + i;

26                     int y = k % 3 * 3 + j;

27                     if (board[x, y] != '.') {

28                         if (!flag[board[x, y]-'1']) flag[board[x, y]-'1'] = true;

29                         else return false;

30                     }

31                 }

32             }

33         }

34         return true;

35     }

36 }
View Code

 

你可能感兴趣的:(LeetCode)