【LeetCode】Stack
[503] Next Greater Element II [Medium]
给一个循环数组,找到离当前元素最近的比它大的元素。
Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number;
The second 1's next greater number needs to search circularly, which is also 2.
我的思路:直接暴力解。
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
vector<int> ans(nums.size(), -);
for(auto i = ; i < nums.size(); ++i) {
bool flag = false;
for(auto j = i + ; j < nums.size(); ++j) {
if (nums[j] > nums[i]) {
ans[i] = nums[j];
flag = true;
break;
}
}
if (flag == false) {
for (auto j = ; j < i; ++j) {
if (nums[j] > nums[i]) {
ans[i] = nums[j];
flag = true;
break;
}
}
}
}
return ans;
}
};
[150] Evaluate Reverse Polish Notation [Medium]
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
注意一下弹出的顺序,不要写错了(- 和 / 对于操作数有顺序之分)
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> stk;
int x, y;
for (auto token : tokens) {
if (token == "+" || token == "-" || token == "*" || token == "/") {
x = stk.top();
stk.pop();
y = stk.top();
stk.pop();
if (token == "+") x = x + y;
if (token == "-") x = y - x;
if (token == "*") x = x * y;
if (token == "/") x = y / x;
stk.push(x);
} else {
int t = stoi(token);
stk.push(t);
}
}
int ans = stk.top();
return ans;
}
};
【LeetCode】Stack的更多相关文章
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】853. Car Fleet 解题报告(Python)
[LeetCode]853. Car Fleet 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
随机推荐
- cpanle/Apache 强制http跳转到https
因为租的虚拟主机是使用Cpanel,按照网上找的文章,处理的步骤如下: 打开Cpanel面板-文件管理器-设置(在页面的右上角)-勾选显示隐藏文件(dotfiles)-save . 找到网站所在的目录 ...
- 阿里云异构计算团队亮相英伟达2018 GTC大会
摘要: 首届云原生计算国际会议(KubeCon + CloudNativeCon,China,2018)在上海举办,弹性计算研究员伯瑜介绍了基于虚拟化.容器化编排技术的云计算操作系统PouchCont ...
- hdu 3974 Assign the task (线段树+树的遍历)
Description There is a company that has N employees(numbered from 1 to N),every employee in the comp ...
- paper 141:some paper with ComputerCV、MachineLearning[转]
copy from:http://blog.csdn.net/zouxy09/article/details/8550952 一.特征提取Feature Extraction: · S ...
- 启发式分治:2019牛客多校第三场 G题 Removing Stones
问题可以转换为求有多少个区间数字的总和除2向下取整大于等于最大值.或者解释为有多少个区间数字的总和大于等于最大值的两倍(但是若区间数字总和为奇数,需要算作减1) 启发式分治: 首先按最大值位置分治,遍 ...
- EFK搜集MySQL慢日志
前提已经安装好EFK 1.在MySQL节点安装td-agent http://packages.treasuredata.com.s3.amazonaws.com/3/redhat/7/x86_64/ ...
- ylbtech-公司-滴滴出行:滴滴出行
ylbtech-公司-滴滴出行:滴滴出行 滴滴出行是涵盖出租车. 专车. 滴滴快车. 顺风车. 代驾及 大巴等多项业务在内的一站式出行平台,2015年9月9日由“滴滴打车”更名而来. 2月1日起, ...
- spring MVC 返回值信息和ResponseBody的响应json数据
spring mvc的界面返回: 如果我们定义的返回类型是String 那么我们返回的时候直接写入 我们的界面的名字就可以了 springmvc会自动去找到我们的界面,如果是void类型的返回那么 ...
- SPSS转换菜单:创建时间序列
SPSS转换菜单:创建时间序列 1.概念:"创建时间序列"对话框允许您基于现有数值型时间序列变量的函数创建新的变量.这些转换后的值在时间序列分析中非常有用. 2.操作:转换-创建时 ...
- 7.使用mysql_export监控mysql
ok,docker监控,宿主机CPU.磁盘.网络.内存监控我们都已讲过,是时候讲一波mysql监控了.本次mysql部署在客户端. 架构 客户端 MySql安装 ##下载mysql的repo源: [r ...