当前位置:网站首页>Sword finger offer special assault edition day 12
Sword finger offer special assault edition day 12
2022-07-28 02:26:00 【hys__ handsome】
The finger of the sword Offer II 036. Postfix expression
The title directly gives the suffix expression , Just count it directly . If you give infixes, you need to convert suffixes to calculate ( The result can be obtained directly through calculation in the process of conversion , If you don't calculate directly in the process of transferring suffixes, you can directly push the symbols onto the stack , Then the final sequence string from the bottom of the stack to the top of the stack is the suffix expression )
class Solution {
public:
void cmp(stack<int> &nums, string op) {
int a = nums.top(); nums.pop();
int b = nums.top(); nums.pop();
if(op == "+") nums.push(b+a);
else if(op == "-") nums.push(b-a);
else if(op == "*") nums.push(b*a);
else nums.push(b/a);
}
int evalRPN(vector<string>& tokens) {
stack<int> nums;
for(string str: tokens) {
if(str == "+" || str == "-" || str == "*" || str == "/") {
cmp(nums,str);
} else nums.push(stoi(str));
}
return nums.top();
}
};
The finger of the sword Offer II 037. Asteroid collision
Stack simulation , Only the left side is positive , Only when the right side is negative can there be a collision .
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int> stk;
for(auto num: asteroids) {
bool flag = true;
while(flag && num < 0 && !stk.empty() && stk.back() > 0) {
if(stk.back() > -num) flag = false;
else if(stk.back() == -num) {
// Both disappear at the same time
stk.pop_back();
flag = false;
} else stk.pop_back();
}
if(flag) stk.push_back(num);
}
return stk;
}
};
The finger of the sword Offer II 038. Daily temperature
Monotone stack optimization
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> res(n);
stack<int> sk;
for(int i = 0; i < n; i++) {
while(sk.size() && temperatures[sk.top()] < temperatures[i]) {
res[sk.top()] = i-sk.top();
sk.pop();
}
sk.push(i);
}
return res;
}
};
边栏推荐
- 产品解读丨MeterSphere UI测试模块的设计与分布式扩展
- 使用BigDecimal类型应该避免哪些问题?(荣耀典藏版)
- Understand the "next big trend" in the encryption industry - ventures Dao
- Codeworks round 807 (Div. 2) a-c problem solution
- Common problem types and methods of mathematical univariate differential proof problems in postgraduate entrance examination
- Sample imbalance - entry 0
- Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
- C # using ABP warehouse to access the database error record set
- synchronized详解
- MySQL create stored procedure ------ [hy000][1418] this function has none of deterministic, no SQL
猜你喜欢

「冒死上传」Proe/Creo产品结构设计-止口与扣位

Promise from getting started to mastering (Chapter 3: customize (handwriting) promise)

With elephant & nbsp; Eplato created by swap, analysis of the high premium behind it

Huawei app UI automation test post interview real questions, real interview experience.

Codeworks round 810 (Div. 2) a~c problem solution

go 学习02 基础知识

Promise from introduction to mastery (Chapter 2 understanding and use of promise)

重要安排-DX12引擎开发课程后续直播将在B站进行

In it, there is a million talent gap, and the salary rises, but it is not capped

The cooperation between starfish OS and metabell is just the beginning
随机推荐
[Yugong series] use of tabby integrated terminal in July 2022
Shell regular and metacharacters
Wechat applet pictures are scaled according to the screen scale
Aike AI frontier promotion (7.14)
Flex layout learning completed on PC side
Promise from introduction to mastery (Chapter 1 Introduction and basic use of promise)
小米网站主页面大模块——小模块+导航(浮动案例)
How to evaluate the effectiveness of R & D personnel? Software Engineer reports help you see everyone's contribution
11-Django-基础篇-数据库操作
Promise从入门到精通 (第1章 Promise的介绍和基本使用)
Plato Farm在Elephant Swap上铸造的ePLATO是什么?
Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements
Promise从入门到精通 (第2章 Promise的理解和使用)
LeetCode 热题 HOT 100 -> 1.两数之和
[database data recovery] data recovery case of insufficient disk space of SQL Server database
Soft test - database (2) relational model
Day6 函数和模块的使用
产品解读丨MeterSphere UI测试模块的设计与分布式扩展
Four common post data submission methods
新零售业态下,零售电商RPA助力重塑增长