【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 ...
随机推荐
- map hashmap的使用
package map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * Map的实现 ...
- rabbitmq 发送的用户名是什么
消费的用户名也必须是什么 接收必须单独启动线程——否则mfc会崩溃 Sleep(30000); //30秒 只要你打开消费队列,只消费1个 队列中所有数据都丢失???????? 断线重连: 知道答案 ...
- Python技能树
本博客Python内容的索引,以后就照着它写了.
- 编译自己的jdk(使用openJDK源码编译jdk )
找到openjdk网站(http://hg.openjdk.java.net/) 选择需要编译的版本,浏览readme文件,有获取源码及编译步骤 CentOS-7-x86_64-DVD-1804.is ...
- boost pointer container
1. boost::ptr_vector #include <boost/ptr_container/ptr_vector.hpp> #include <iostream> i ...
- 【集群】Redis哨兵(Sentinel)模式
主从切换技术的方法是:当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费事费力,还会造成一段时间内服务不可用.这不是一种推荐的方式,更多时候,我们优先考虑哨兵模式. 一.哨兵 ...
- Linux文本处理三剑客之——grep
一Linux文本处理三剑客之——grep Linux文本处理三剑客都支持正则表达式 grep :文本过滤( 模式:pattern) 工具,包括grep, egrep, fgrep (不支持正则表达式) ...
- websocket 文件上传
<template> <div class="pad20"> <input id="file" ref="f ...
- 【Shiro】二、Apache Shiro配置
1.配置 使用配置获得SecurityManager,SecurityManager是核心,配置好并获取到SecurityManager,Shiro就算正式运行起来了. 两种方式:通过ini文件:通过 ...
- tcp和udp得区别
TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议 UDP 是User Datagram Protocol,即 用户 ...