This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| void nextPermutation(vector<int> &num) { | |
| if (!next_permutation(num.begin(), num.end())) | |
| sort(num.begin(), num.end()); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int longestValidParentheses(string s) { | |
| int n = s.length(); | |
| int ans = 0; | |
| int sum = 0; | |
| for (int i = 0; i < n; ++i) { | |
| sum = 0; | |
| if (ans >= n - i) | |
| break; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int search(int A[], int n, int target) { | |
| int ans = -1; | |
| int left = 0, right = n - 1; | |
| while (left <= right) { | |
| int mid = (left + right) >> 1; | |
| if (A[mid] == target) { | |
| ans = mid; | |
| break; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<int> searchRange(int A[], int n, int target) { | |
| int left = 0, right = n - 1; | |
| int ans1 = n; | |
| while (left <= right) { | |
| int mid = (left + right) >> 1; | |
| if (A[mid] == target) { | |
| ans1 = min(ans1, mid); | |
| right = mid - 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int searchInsert(int A[], int n, int target) { | |
| int ans = n; | |
| int left = 0, right = n - 1; | |
| while (left <= right) { | |
| int mid = (left + right) >> 1; | |
| if (A[mid] >= target) { | |
| ans = min(ans, mid); | |
| right = mid - 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| string countAndSay(int n) { | |
| string ans = "1"; | |
| while (--n) { | |
| string tmp = ans; | |
| ans.clear(); | |
| int cnt; | |
| char num; | |
| char buf[1024]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<vector<int> > combinationSum(vector<int> &num, int target) { | |
| vector<vector<int> > ans; | |
| map<vector<int>, bool> hash; | |
| if (target == 0 || num.empty()) | |
| return ans; | |
| vector<int> tmp; | |
| sort(num.begin(), num.end()); | |
| for (int i = 0; i < num.size(); ++i) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<vector<int> > combinationSum2(vector<int> &num, int target) { | |
| vector<vector<int> > ans; | |
| map<vector<int>, bool> hash; | |
| if (target == 0 || num.empty()) | |
| return ans; | |
| vector<int> tmp; | |
| sort(num.begin(), num.end()); | |
| for (int i = 0; i < num.size(); ++i) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int firstMissingPositive(int A[], int n) { | |
| for (int i = 0; i < n; ++i) { | |
| int id = i; | |
| while (A[id] != id + 1 && A[id] >= 1 && A[id] <= n && A[id] != A[A[id] - 1]) | |
| swap(A[id], A[A[id] - 1]); | |
| } | |
| for (int i = 0; i < n; ++i) | |
| if (A[i] != i + 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define MAXN 100000 | |
| int _left[MAXN]; | |
| int _right[MAXN]; | |
| class Solution { | |
| public: | |
| int trap(int A[], int n) { | |
| for (int i = 0; i < n; ++i) { | |
| if (i == 0) | |
| _left[i] = A[i]; |