【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 ...
随机推荐
- wpf 查找控件
public List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : Framewo ...
- wxDateTime用法和转换成wxString
转载别人的.void datetest() { wxDateTime now=wxDateTime::Now(); wxString date1=now.Format(); wxString date ...
- Python 第三天学习整理①
今天学习的内容包括以下几个方面:1.字符编码 2.文件操作 3.函数 1.字符编码 关于字符编码,关注以下几个点 1.1 什么是字符编码 字符编码:字符转化为数字的过程所遵循的标准 字符编码包括:un ...
- 10.ThreadLocal
/** * - void set(Object value)设置当前线程的线程局部变量的值. * - public Object get()该方法返回当前线程所对应的线程局部变量. * - publi ...
- centos 6.5 安装 zookeeper
从zookeeper官方网站下载安装包:zookeeper-3.4.9.tar.gz,解压安装 tar xvf zookeeper-3.4.9.tar.gz -C /usr/java cd /usr/ ...
- 【leetcode】948. Bag of Tokens
题目如下: You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can ...
- String path = request.getContextPath();报错
String path = request.getContextPath();报错 1. 右击该项目 - Build Path - Configure Build Path , 在 Libraries ...
- Cisco基础(四):配置标准ACL、配置扩展ACL、配置标准命名ACL、配置扩展命名ACL
一.配置标准ACL 目标: 络调通后,保证网络是通畅的.同时也很可能出现未经授权的非法访问.企业网络既要解决连连通的问题,还要解决网络安全的问题. 配置标准ACL实现拒绝PC1(IP地址为192.16 ...
- Echarts 甘特图教程
Echarts甘特图教程 echarts官网实例: https://gallery.echartsjs.com/editor.html?c=xEYpsVs30s 效果: 代码: <html& ...
- dedecms SESSION变量覆盖导致SQL注入漏洞修补方案
dedecms的/plus/advancedsearch.php中,直接从$_SESSION[$sqlhash]获取值作为$query带入SQL查询,这个漏洞的利用前提是session.auto_st ...