Skip to content

Commit 5403bce

Browse files
committed
20190219
1 parent 60092bd commit 5403bce

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
ListNode* separate(ListNode *h){
11+
auto l=h,r=h;
12+
ListNode* pre=nullptr;
13+
while(r&&r->next){
14+
r=r->next->next;
15+
pre=l;
16+
l=l->next;
17+
}
18+
if(pre)
19+
pre->next=nullptr;
20+
return l;
21+
}
22+
public:
23+
ListNode* sortList(ListNode* head) {
24+
if(!head||!head->next)
25+
return head;
26+
auto rp=separate(head);
27+
rp=sortList(rp);
28+
auto lp=sortList(head);
29+
auto h=ListNode(0);
30+
auto p=&h;
31+
while(lp&&rp){
32+
auto &smallP=(lp->val<rp->val)?lp:rp;
33+
p=p->next=smallP;
34+
smallP=smallP->next;
35+
}
36+
for(auto cur=lp?lp:rp;cur;cur=cur->next)
37+
p=p->next=cur;
38+
return h.next;
39+
}
40+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int evalRPN(vector<string>& tokens) {
4+
stack<int> nums;
5+
for(auto &s:tokens){
6+
if(s[s.size()-1]>='0'&&s[s.size()-1]<='9')
7+
nums.push(stoi(s));
8+
else{
9+
auto n2=nums.top();
10+
nums.pop();
11+
auto n1=nums.top();
12+
nums.pop();
13+
switch(s[0]){
14+
case '+':nums.push(n1+n2);break;
15+
case '-':nums.push(n1-n2);break;
16+
case '*':nums.push(n1*n2);break;
17+
case '/':nums.push(n1/n2);break;
18+
}
19+
}
20+
}
21+
return nums.top();
22+
}
23+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int maxProduct(vector<int>& nums) {
4+
vector<int> maxPro(nums.size(),nums[0]),minPro(nums.size(),nums[0]);
5+
auto ret=nums[0];
6+
for(auto i=1;i<nums.size();++i){
7+
maxPro[i]=max({nums[i],maxPro[i-1]*nums[i],minPro[i-1]*nums[i]});
8+
minPro[i]=min({nums[i],maxPro[i-1]*nums[i],minPro[i-1]*nums[i]});
9+
ret=max({ret,maxPro[i]});
10+
}
11+
return ret;
12+
}
13+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int maxProduct(vector<int>& nums) {
4+
auto maxPro=nums[0],minPro=nums[0],ret=nums[0];
5+
for(auto i=1;i<nums.size();++i){
6+
auto curMaxPro=max({nums[i],nums[i]*maxPro,nums[i]*minPro});
7+
auto curMinPro=min({nums[i],nums[i]*maxPro,nums[i]*minPro});
8+
ret=max({ret,curMaxPro});
9+
maxPro=curMaxPro;
10+
minPro=curMinPro;
11+
}
12+
return ret;
13+
}
14+
};

0 commit comments

Comments
 (0)