class Solution {
public:
bool search(vector<int>& nums,int target){int l =0, r = nums.size()-1;while(l <= r){while(l < r && nums[l]== nums[l +1]) l ++;while(l < r && nums[r]== nums[r -1]) r --;int mid = l + r >>1;//要用到mid 所以上面 l <= rif(nums[mid]== target)return true;if(nums[mid]>= nums[l]){if(target < nums[mid]&& target >= nums[l]) r = mid;else l = mid +1;}else{if(target > nums[mid]&& target <= nums[r]) l = mid +1;else r = mid;}}return false;}};
class Solution {
public:intlargestRectangleArea(vector<int>& heights){int n = heights.size(), res =0;
heights.push_back(-1);//在数组末尾添加高度-1
stack<int> stk;for(int i =0; i <= n; i ++){while(!stk.empty()&& heights[i]< heights[stk.top()]){int cur = stk.top();
stk.pop();if(stk.empty())
res =max(res, heights[cur]* i);else
res =max(res, heights[cur]*(i - stk.top()-1));}
stk.push(i);}return res;}};
class Solution {
public:intmaximalRectangle(vector<vector<char>>& matrix){//求出每一层的 heights[] 然后传给上一题的函数就可以了int n = matrix.size(), m, res =0;if(n ==0)return0;
m = matrix[0].size();
vector<int>heights(m +1,0);
heights[m]=-1;for(int i =0; i < n; i ++){for(int j =0; j < m; j ++)if(matrix[i][j]=='0')
heights[j]=0;else
heights[j]++;
stack<int> stk;for(int j =0; j <= m; j ++){while(!stk.empty()&& heights[j]< heights[stk.top()]){int cur = stk.top();
stk.pop();if(stk.empty())
res =max(res, heights[cur]* j);else
res =max(res, heights[cur]*(j - stk.top()-1));}
stk.push(j);}}return res;}};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode*partition(ListNode* head,int x){
ListNode* before = new ListNode(-1);
ListNode* after = new ListNode(-1);
ListNode* pb = before,*pa = after;for(ListNode* p = head; p; p = p->next){if(p->val < x){
pb->next = p;
pb = p;}else{
pa->next = p;
pa = p;}}
pb->next = after->next;
pa->next =0;return before->next;}};
class Solution {
public:voidmerge(vector<int>& nums1,int m, vector<int>& nums2,int n){int k = n + m -1;
m --, n --;while(~m &&~n){if(nums1[m]< nums2[n]) nums1[k --]= nums2[n --];else nums1[k --]= nums1[m --];}while(~n) nums1[k --]= nums2[n --];}};
class Solution {
public:
vector<int>grayCode(int n){
vector<int> res;for(int i =0; i <1<< n; i ++)
res.push_back(i ^(i >>1));return res;}};
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
vector<vector<int>>subsetsWithDup(vector<int>& nums){sort(nums.begin(), nums.end());dfs(nums,0);return res;}voiddfs(vector<int>&nums,int u){if(u == nums.size()){
res.push_back(path);return;}int k = u;while(k < nums.size()&& nums[k]== nums[u]) k ++;dfs(nums, k);for(int i = u; i < k; i ++){
path.push_back(nums[i]);dfs(nums, k);}while(path.size()&& path.back()== nums[u]) path.pop_back();}};