class Solution {
public:
int maxProduct(vector<int>& nums) {
int pos = 0, neg = 0;
int res = INT_MIN;
for(auto x : nums)
{
if(x > 0)
{
if(pos > 0) pos *= x;
else pos = x;
neg *= x;
}
else if(x < 0)
{
int np = pos, ng = neg;
np = neg * x;
if(pos > 0) ng = pos * x;
else ng = x;
pos = np, neg = ng;
}
else pos = neg = 0;
res = max(res, x);
if(pos) res = max(res, pos);
}
return res;
}
};
class Solution {
public:
int findMin(vector<int>& nums) {
if(nums[0] < nums.back()) return nums[0];
int l = 0, r = nums.size() - 1;
while(l < r)
{
int mid = l + r >> 1;
if(nums[mid] >= nums[0]) l = mid + 1;
else r = mid;
}
return nums[l];
}
};
class Solution {
public:
int findMin(vector<int>& nums) {
int l = 0, r = nums.size() - 1;
while(l < r && nums[l] == nums[r]) r --;
if(nums[0] < nums[r]) return nums[0];
while(l < r)
{
int mid = l + r >> 1;
if(nums[mid] >= nums[0]) l = mid + 1;
else r = mid;
}
return nums[r];
}
};
class MinStack {
public:
/** initialize your data structure here. */
stack<int> stackValue;
stack<int> stackMin;
MinStack() {
}
void push(int x) {
stackValue.push(x);
if(stackMin.empty() || x <= stackMin.top())
stackMin.push(x);
}
void pop() {
int x = stackValue.top();
stackValue.pop();
if(x == stackMin.top()) stackMin.pop();
}
int top() {
return stackValue.top();
}
int getMin() {
return stackMin.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* a = headA, *b = headB;
while(a != b)
{
if(!a) a = headB;
else a = a->next;
if(!b) b = headA;
else b = b->next;
} //如果不想交 走a + b 后都为NULL
return a;
}
};