【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 ...
随机推荐
- shell条件测试举例
- 由hbase.client.scanner.caching参数引发的血案(转)
转自:http://blog.csdn.net/rzhzhz/article/details/7536285 环境描述 Hadoop 0.20.203.0Hbase 0.90.3Hive 0.80.1 ...
- windows 安装 jenkins笔记
Jenkins 所有镜像列表: http://mirrors.jenkins-ci.org/status.html 可在镜像网站上下载安装文件,比官方下载快些 jenkins 官网地址: https: ...
- Struts 2中的constant详解【转载】
通过对这些属性的配置,可以改变Struts 2 框架的一些默认行为,这些配置可以在struts.xml文件中完成,也可以在struts.properties文件中完成. struts.xml 1.&l ...
- Administrator 被禁用
Administrator 被禁用 问题: 重装系统后出现输入用户名和密码的情况,原因是Administrator被禁用. 解决方法: 1.开机后shift+重启 –>选择疑难解答 --> ...
- MySQL-事件总结
是什么?事件是一组SQL集合,简单说就是mysql中的定时器,时间到了就执行. 一:查询事件变量,如果查询不到变量,说明数据库版本过低,不支持事件. SHOW VARIABLES LIKE 'even ...
- leetcode-165周赛-1278-分割回文串③
题目描述: 动态规划:O(N^3) class Solution: def palindromePartition(self, s: str, k: int) -> int: def cost( ...
- comet4j js中写法
<script type="text/javascript" src="${ctxStatic}/common/js/comet4j.js">< ...
- Anaconda 安装 pytorch报错解决方法
一.安装Pytorch: # -c 指定用pytorch镜像源下载软件conda install pytorch torchvision cpuonly -c pytorch 报错: 二.配置: ch ...
- sql-主键、外键、索引
SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...