36、Valid Sudoku
题目
代码如下:
1 class Solution { 2 public: 3 bool isValidSudoku(vector<vector<char> > &board) { 4 // Note: The Solution object is instantiated only once. 5 vector<vector<bool>> rows(9, vector<bool>(9,false)); 6 vector<vector<bool>> cols(9, vector<bool>(9,false)); 7 vector<vector<bool>> blocks(9, vector<bool>(9,false)); 8 9 for(int i = 0; i < 9; i++) 10 for(int j = 0; j < 9; j++) 11 { 12 if(board[i][j] == '.')continue; 13 int num = board[i][j] - '1'; 14 if(rows[i][num] || cols[j][num] || blocks[i - i%3 + j/3][num]) 15 return false; 16 rows[i][num] = cols[j][num] = blocks[i - i%3 + j/3][num] = true; 17 } 18 return true; 19 } 20 };
-------------------------------------------------------------------------------------------分割线---------------------------------------------------------------------------------
38、Count and Say
题目
找规律题目,直接上代码:
1 class Solution { 2 public: 3 string unguarded_convert(const string &say) 4 { 5 stringstream ss; 6 int count = 0; 7 char last = say[0]; 8 9 for (size_t i = 0; i <= say.size(); ++i) 10 { 11 if (say[i] == last) 12 { 13 ++count; 14 } 15 else 16 { 17 ss << count << last; 18 count = 1; 19 last = say[i]; 20 } 21 } 22 23 return ss.str(); 24 } 25 26 string countAndSay(int n) 27 { 28 if (n <= 0) return string(); 29 30 string say = "1"; 31 32 for (int i = 1; i < n; ++i) 33 { 34 say = unguarded_convert(say); 35 } 36 37 return say; 38 } 39 };