[503] Next Greater Element II [Medium]

给一个循环数组,找到离当前元素最近的比它大的元素。

  1. Input: [1,2,1]
  2. Output: [2,-1,2]
  3. 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.

我的思路:直接暴力解。

  1. class Solution {
  2. public:
  3. vector<int> nextGreaterElements(vector<int>& nums) {
  4. vector<int> ans(nums.size(), -);
  5. for(auto i = ; i < nums.size(); ++i) {
  6. bool flag = false;
  7. for(auto j = i + ; j < nums.size(); ++j) {
  8. if (nums[j] > nums[i]) {
  9. ans[i] = nums[j];
  10. flag = true;
  11. break;
  12. }
  13. }
  14. if (flag == false) {
  15. for (auto j = ; j < i; ++j) {
  16. if (nums[j] > nums[i]) {
  17. ans[i] = nums[j];
  18. flag = true;
  19. break;
  20. }
  21. }
  22. }
  23. }
  24. return ans;
  25. }
  26. };

[150] Evaluate Reverse Polish Notation [Medium]

  1. ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  2. ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

注意一下弹出的顺序,不要写错了(- 和 / 对于操作数有顺序之分)

  1. class Solution {
  2. public:
  3. int evalRPN(vector<string>& tokens) {
  4. stack<int> stk;
  5. int x, y;
  6. for (auto token : tokens) {
  7. if (token == "+" || token == "-" || token == "*" || token == "/") {
  8. x = stk.top();
  9. stk.pop();
  10. y = stk.top();
  11. stk.pop();
  12. if (token == "+") x = x + y;
  13. if (token == "-") x = y - x;
  14. if (token == "*") x = x * y;
  15. if (token == "/") x = y / x;
  16. stk.push(x);
  17. } else {
  18. int t = stoi(token);
  19. stk.push(t);
  20. }
  21. }
  22. int ans = stk.top();
  23. return ans;
  24. }
  25. };

【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. wpf 查找控件

    public List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : Framewo ...

  2. wxDateTime用法和转换成wxString

    转载别人的.void datetest() { wxDateTime now=wxDateTime::Now(); wxString date1=now.Format(); wxString date ...

  3. Python 第三天学习整理①

    今天学习的内容包括以下几个方面:1.字符编码 2.文件操作 3.函数 1.字符编码 关于字符编码,关注以下几个点 1.1 什么是字符编码 字符编码:字符转化为数字的过程所遵循的标准 字符编码包括:un ...

  4. 10.ThreadLocal

    /** * - void set(Object value)设置当前线程的线程局部变量的值. * - public Object get()该方法返回当前线程所对应的线程局部变量. * - publi ...

  5. centos 6.5 安装 zookeeper

    从zookeeper官方网站下载安装包:zookeeper-3.4.9.tar.gz,解压安装 tar xvf zookeeper-3.4.9.tar.gz -C /usr/java cd /usr/ ...

  6. 【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 ...

  7. String path = request.getContextPath();报错

    String path = request.getContextPath();报错 1. 右击该项目 - Build Path - Configure Build Path , 在 Libraries ...

  8. Cisco基础(四):配置标准ACL、配置扩展ACL、配置标准命名ACL、配置扩展命名ACL

    一.配置标准ACL 目标: 络调通后,保证网络是通畅的.同时也很可能出现未经授权的非法访问.企业网络既要解决连连通的问题,还要解决网络安全的问题. 配置标准ACL实现拒绝PC1(IP地址为192.16 ...

  9. Echarts 甘特图教程

    Echarts甘特图教程  echarts官网实例: https://gallery.echartsjs.com/editor.html?c=xEYpsVs30s 效果:  代码: <html& ...

  10. dedecms SESSION变量覆盖导致SQL注入漏洞修补方案

    dedecms的/plus/advancedsearch.php中,直接从$_SESSION[$sqlhash]获取值作为$query带入SQL查询,这个漏洞的利用前提是session.auto_st ...