classSolution { public: intlongestValidParentheses(string s) { stack<int>st; int res = 0; int len = s.length();
for (int i = 0; i < len; i++) { if (s[i] == ')') { if (!st.empty() && s[st.top()] == '(') { st.pop(); } else { st.push(i); } } else { st.push(i); } } if (st.empty()) { res = len; } else { int ri = len, le = 0; while (!st.empty()) { int k = st.top(); st.pop(); res = max(res, ri - k - 1); ri = k; } res = max(res, ri); } return res; } };
classSolution { public: intsearch(vector<int>& nums, int target) { int n = nums.size(); int ri = n - 1; int le = 0; while (le < ri) { int mid = (le + ri) >> 1; if (nums[mid]>nums[ri]) { le = mid + 1; } else { ri = mid; } } int t = le; le = 0, ri = n - 1; while (le < ri) { int mid = (le + ri + 1) >> 1; int p = (t + mid) % n; if (nums[p]>target) { ri = mid - 1; } else { le = mid; } } if (nums[(le + t) % n] == target)return (le + t) % n; elsereturn-1; } };
boolisValidSudoku(vector< vector < char> >& board,int x,int y) { for (int i = 0; i < 9; i++) { if (i == x)continue; if (board[i][y] == board[x][y])returnfalse; } for (int i = 0; i < 9; i++) { if (i == y)continue; if (board[x][i] == board[x][y])returnfalse; } int k = (x / 3)* 3 + y / 3; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (i == x&&j == y)continue; int k2 = (i / 3) * 3 + j / 3; if (k == k2) { if (board[x][y] == board[i][j]) { returnfalse; } } } } returntrue; }
classSolution { public: boolsolveSudoku(vector<vector<char>>& board) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (board[i][j] == '.') { for (int k = 1; k <= 9; k++) { board[i][j] = '0' + k; if (isValidSudoku(board,i,j) && solveSudoku(board)) { returntrue; } board[i][j] = '.'; } returnfalse; } } } returntrue; } };