[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的更多相关文章

  1. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  2. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  3. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  4. 【LeetCode】853. Car Fleet 解题报告(Python)

    [LeetCode]853. Car Fleet 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  5. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  6. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  7. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  8. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  9. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

随机推荐

  1. gcc 交叉工具链中工具使用(arm-linux-xxx)

    在Ubuntu系统中使用 gcc 系列工具是在PC机上使用 arm-linux-gcc 编译的目标 是在 arm CPU上使用 一.安装交叉编译工具链 1. 编译工具怎么获取 1)从官网 http:/ ...

  2. Nginx的应用之虚拟主机

    开始前请确保selinux关闭,否则当配置完虚拟主机后,尽管权限或者网站目录都正确,访问的结果也是403 nginx的虚拟主机有三种方式: 一.基于域名的虚拟主机 (1)创建对应的web站点目录以及程 ...

  3. Nginx实现rewrite重写

    目录 Rewrite基本概述 Rewrite标记Flag Rewrite规则实践 Rewrite场景示例 Rewrite规则补充 rewrite优先级实战 Rewrite基本概述 什么是rewrite ...

  4. http请求访问响应慢问题解决的基本思路

    第一步,检查网络 ping命令检查网络域名解析是否正常,ping服务器的延迟是否过大,如果过大可以检查Ip是否冲突,或者交换机网线是否正常插好,通过nmon还可以查看网络流量,一般用的千兆交换机理论速 ...

  5. python convert csv to xlsx

    搬运:http://stackoverflow.com/questions/17684610/python-convert-csv-to-xlsx import os import glob impo ...

  6. centos 升级python

    1.下载python3.6.1的包 wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz 2.解压 tar -zxvf Pytho ...

  7. 数据结构---Java---HashMap---JDK1.7

    源码解读 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Clone ...

  8. 揭秘阿里云WAF背后神秘的AI智能防御体系

    背景 应用安全领域,各类攻击长久以来都危害着互联网上的应用,在web应用安全风险中,各类注入.跨站等攻击仍然占据着较前的位置.WAF(Web应用防火墙)正是为防御和阻断这类攻击而存在,也正是这些针对W ...

  9. Base64和3DES算法

    Base64加密算法 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,可用于在HTTP环境下传递较长的标识信息.它的优点是算法效率高,编码出来的结果比较简短,同时也具有不可读性. ...

  10. Database基础(一):构建MySQL服务器、 数据库基本管理 、MySQL 数据类型、表结构的调整

    一.构建MySQL服务器 目标: 本案例要求熟悉MySQL官方安装包的使用,快速构建一台数据库服务器: 安装MySQL-server.MySQl-client软件包 修改数据库用户root的密码 确认 ...