Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 851f47f

Browse files
新增一些题解
1 parent a1b95a0 commit 851f47f

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

‎150.逆波兰表达式求值.cpp‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @lc app=leetcode.cn id=150 lang=cpp
3+
*
4+
* [150] 逆波兰表达式求值
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int evalRPN(vector<string>& tokens) {
11+
stack<int> nums;
12+
for (auto& t : tokens) {
13+
if (t == "+" || t == "-" || t == "*" || t == "/") {
14+
int a = nums.top();
15+
nums.pop();
16+
int b = nums.top();
17+
nums.pop();
18+
if (t == "+") {
19+
nums.push(b + a);
20+
}
21+
if (t == "-") {
22+
nums.push(b - a);
23+
}
24+
if (t == "*") {
25+
nums.push(b * a);
26+
}
27+
if (t == "/") {
28+
nums.push(b / a);
29+
}
30+
} else {
31+
nums.push(stoi(t));
32+
}
33+
}
34+
// printf("nums.size() = %d\n", nums.size());
35+
return nums.top();
36+
}
37+
};
38+
// @lc code=end
39+

‎152.乘积最大子数组.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode.cn id=152 lang=cpp
3+
*
4+
* [152] 乘积最大子数组
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int maxProduct(vector<int>& nums) {
11+
int n = nums.size();
12+
int max = INT_MIN;
13+
int product = 1;
14+
// 区间一定出现在最左或者最右
15+
// 两边有 1. 两正或者两负, 可以扩大区间
16+
// 2. 一正一负, 也能扩大一边的区间
17+
for (int i = 0; i < n; i++) {
18+
product *= nums[i];
19+
max = product > max ? product : max;
20+
if (product == 0)
21+
product = 1;
22+
}
23+
product = 1;
24+
for (int i = n - 1; i >= 0; i--) {
25+
product *= nums[i];
26+
max = product > max ? product : max;
27+
if (product == 0)
28+
product = 1;
29+
}
30+
return max;
31+
}
32+
};
33+
// @lc code=end
34+

‎155.最小栈.cpp‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @lc app=leetcode.cn id=155 lang=cpp
3+
*
4+
* [155] 最小栈
5+
*/
6+
7+
// @lc code=start
8+
class MinStack {
9+
public:
10+
MinStack() {
11+
12+
}
13+
14+
void push(int val) {
15+
if (minv.empty()) {
16+
minv.push(val);
17+
} else {
18+
minv.push(min(val, minv.top()));
19+
}
20+
data.push(val);
21+
}
22+
23+
void pop() {
24+
data.pop();
25+
minv.pop();
26+
}
27+
28+
int top() {
29+
return data.top();
30+
}
31+
32+
int getMin() {
33+
return minv.top();
34+
}
35+
36+
private:
37+
// 我们只需要设计一个数据结构,使得每个元素 a 与其相应的最小值 m 时刻保持一一对应
38+
stack<int> data;
39+
stack<int> minv;
40+
};
41+
42+
/**
43+
* Your MinStack object will be instantiated and called as such:
44+
* MinStack* obj = new MinStack();
45+
* obj->push(val);
46+
* obj->pop();
47+
* int param_3 = obj->top();
48+
* int param_4 = obj->getMin();
49+
*/
50+
// @lc code=end
51+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /