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
| /** | |
| * Definition for singly-linked list. | |
| * struct ListNode { | |
| * int val; | |
| * ListNode *next; | |
| * ListNode(int x) : val(x), next(NULL) {} | |
| * }; | |
| */ | |
| class Solution { | |
| public: |
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
| int get_biggest(int A[], int n) { | |
| int ret = 0; | |
| int left = 0; | |
| int right = n - 1; | |
| while (left <= right) { | |
| int mid = (left + right) >> 1; | |
| if (A[mid] >= A[0]) { | |
| ret = max(ret, mid); | |
| left = 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
| bool bsearch(int A[], int left, int right, int target) { | |
| while (left <= right) { | |
| int mid = (left + right) >> 1; | |
| if (A[mid] > target) | |
| right = mid - 1; | |
| else if (A[mid] < target) | |
| left = mid + 1; | |
| else | |
| return true; | |
| } |
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 removeDuplicates(int A[], int n) { | |
| int id = 0; | |
| int cur = 0; | |
| while (cur < n) { | |
| int cnt = 0; | |
| int val = A[cur]; | |
| while (cur < n && val == A[cur]) { | |
| cur++; |
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 1000 | |
| bool used[MAXN][MAXN]; | |
| int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; | |
| bool valid(int i, int j, int n, int m) { | |
| return i >= 0 && i < n && j >= 0 && j < m; | |
| } | |
| bool find(vector<vector<char> >& board, bool used[MAXN][MAXN], int n, int m, |
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
| void dfs(vector<int>& S, vector<vector<int> >& ret, vector<int>& cur, int kth) { | |
| if (kth == S.size()) { | |
| ret.push_back(cur); | |
| return; | |
| } | |
| dfs(S, ret, cur, kth + 1); | |
| cur.push_back(S[kth]); | |
| dfs(S, ret, cur, kth + 1); | |
| cur.pop_back(); | |
| } |
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
| void dfs(int n, int k, int kth, vector<vector<int> >& ret, vector<int>& cur) { | |
| if (k == cur.size()) { | |
| ret.push_back(cur); | |
| return; | |
| } | |
| if (kth == n + 1) | |
| return; | |
| if (k - cur.size() > n - kth + 1) | |
| return; | |
| dfs(n, k, kth + 1, ret, cur); |
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 sortColors(int A[], int n) { | |
| int pos0 = 0; | |
| int pos1 = n - 1; | |
| int pos2 = n - 1; | |
| while (pos0 < pos2) { | |
| if (A[pos0] == 0) | |
| pos0++; | |
| else if (A[pos0] == 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 minWindow(string S, string T) { | |
| if (S.empty() || T.empty()) | |
| return ""; | |
| tr1::unordered_map<char, int>::iterator iter; | |
| tr1::unordered_map<char, int> MT; | |
| for (int i = 0; i < T.length(); ++i) { | |
| iter = MT.find(T[i]); | |
| if (iter == MT.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
| bool big(vector<vector<int> > &matrix, int leftr, int leftc, int rightr, int rightc) { | |
| return (leftr > rightr) || (leftr == rightr && leftc > rightc); | |
| } | |
| void getmid(vector<vector<int> > &matrix, int leftr, int leftc, int rightr, int rightc, int& midr, int& midc) { | |
| int n = matrix.size(); | |
| int m = matrix[0].size(); | |
| int len = (rightr * m + rightc - leftr * m - leftc) / 2; | |
| int pos = leftr * m + leftc + len; | |
| midr = pos / m; |