496. Next Greater Element I

https://leetcode.com/problems/next-greater-element-i/

单调栈问题,参考:https://leetcode.com/problems/next-greater-element-i/discuss/97595/Java-10-lines-linear-time-complexity-O(n)-with-explanation

class Solution {public int[] nextGreaterElement(int[] nums1, int[] nums2) {HashMap<Integer, Integer> map = new HashMap<>();Stack<Integer> stack = new Stack<>();for (int n : nums2) {while (!stack.isEmpty() && stack.peek() < n) {map.put(stack.pop(), n);}stack.push(n);}int[] result = new int[nums1.length];for (int i = 0; i < nums1.length; i++) {result[i] = map.getOrDefault(nums1[i], -1);}return result;}
}

503. Next Greater Element II

https://leetcode.com/problems/next-greater-element-ii/

class Solution {public int[] nextGreaterElements(int[] nums) {int[] result = new int[nums.length];Arrays.fill(result, -1);Stack<Integer> valueStack = new Stack<>();Stack<Integer> indexStack = new Stack<>();for (int i = 0; i < nums.length * 2 - 1; i++) {int index = i % nums.length; // circularlywhile (!valueStack.isEmpty() && valueStack.peek() < nums[index]) {result[indexStack.pop()] = nums[index];valueStack.pop();}valueStack.push(nums[index]);indexStack.push(index);}return result;}
}

556. Next Greater Element III

https://leetcode.com/problems/next-greater-element-iii/

class Solution {public int nextGreaterElement(int n) {int[] nums = new int[String.valueOf(n).length()];int j = nums.length - 1;while (n != 0) {nums[j--] = n % 10;n /= 10;}// 找到右边最小的大于i的数Stack<Integer> valueStack = new Stack<>();Stack<Integer> indexStack = new Stack<>();int preIndex = -1;boolean valid = false;for (int i = nums.length - 1; i >= 0; i--) {while (!valueStack.isEmpty() && valueStack.peek() > nums[i]) {preIndex = indexStack.pop();valueStack.pop();valid = true;}valueStack.push(nums[i]);indexStack.push(i);if (valid) { // i与i右边最小的大于i的数交换swap(nums, i, preIndex);reverse(nums, i + 1, nums.length - 1); // i右边到结尾的所有数翻转break;}}if (!valid) return -1;long res = 0;for (int num : nums) {res *= 10;res += num;}return res <= Integer.MAX_VALUE ? (int) res : -1;}public void reverse(int[] arr, int i, int j) {for (int k = 0; k < (j - i + 1) / 2; k++) {swap(arr, i + k, j - k);}}public void swap(int[] arr, int i, int j) {int t = arr[i];arr[i] = arr[j];arr[j] = t;}
}

leetcode 496, 503, 556. Next Greater Element I, II, III | 496, 503, 556. 下一个更大元素 I,II,III(单调栈)相关推荐

  1. [Swift]LeetCode496. 下一个更大元素 I | Next Greater Element I

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  2. LeetCode 496. 下一个更大元素 I

    599. 两个列表的最小索引总和 添加链接描述 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和 ...

  3. Leetcode 503:下一个更大元素 II(超详细的解法!!!)

    给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...

  4. LeetCode 496. 下一个更大元素 I(哈希)

    1. 题目 给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x ...

  5. 496. 下一个更大元素 I/503. 下一个更大元素 II/739. 每日温度

    2020-05-10 1.题目描述 下一个更大元素 2.题解 1.直接进行暴力搜索 2.既然题目的标签是栈,就应该用到栈先进后出的特性,参考了官方的题解之后,我认为最能体现这种 特性的就是要找到下一个 ...

  6. LeetCode(496)——下一个更大元素 I(JavaScript)

    给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更大 ...

  7. Java实现 LeetCode 503 下一个更大元素 II

    503. 下一个更大元素 II 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大 ...

  8. Java实现 LeetCode 556 下一个更大元素 III(数组的翻转)

    556. 下一个更大元素 III 给定一个32位正整数 n,你需要找到最小的32位整数,其与 n 中存在的位数完全相同,并且其值大于n.如果不存在这样的32位整数,则返回-1. 示例 1: 输入: 1 ...

  9. Java实现 LeetCode 496 下一个更大元素 I

    496. 下一个更大元素 I 给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nu ...

最新文章

  1. 第六章 Web开发实战1——HTTP服务
  2. 晚上八点半,一起来聊聊
  3. Spring Cloud 入门 之 Feign 篇(三)
  4. BZOJ1010:[HNOI2008]玩具装箱TOY(斜率优化DP)
  5. HTML页面显示透视效果,《前端每日实战》第176号作品:用透视图表现 HTML、CSS 和 JS 的关系...
  6. [翻译] Canvas 不用写代码的动画
  7. Beyond Compare怎么查看合并文本后相同内容
  8. 几个问题帮你认识“NIC”----网卡
  9. Dubbo系列之Provider Service注册和暴露(三)
  10. 计算机有些应用无法卸载,有些软件卸载不了怎么办_电脑软件卸载不了的正确解决方法...
  11. 生成模型和判别模型学习
  12. px、pt、dpi、dip、分辨率、屏幕尺寸等等概念
  13. vue 数据大屏使用数字字体
  14. 对B/S架构和C/S架构的分析
  15. TP框架 数据大批量导入数据库
  16. 44-网上商城数据库-商品分类数据操作(一)
  17. EPLAN电气图纸,源文件是实际使用中的一套完整图纸,源文件中的部件、图框等都可以直接更新到库中
  18. 【转】BOSS系统简介
  19. (五)CelebA CelebA-HQ
  20. 浏览器渲染 卡顿排查

热门文章

  1. SPOJ - LCS Longest Common Substring(后缀自动机)
  2. URAL - 1099 Work Scheduling(一般图最大匹配-带花树模板)
  3. 计算机桌面ie图标无法删除,win7系统桌面ie图标无法删除怎么办
  4. keil 查看 stm32 io波形_如何系统地入门学习stm32?
  5. navicat创建数据库后显示图标灰色,右键后无法新建数据库
  6. WebRTC 的 log 系统实现分析
  7. EV3 直接命令 - 第 5 课 从 EV3 的传感器读取数据
  8. Ansible:Ansibl项目生产环境快速布局
  9. 【实战分享】漫谈 gRPC的选型
  10. 使用git remote提交代码