日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/2/28 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 496, 503, 556. Next Greater Element I, II, III | 496, 503, 556. 下一个更大元素 I,II,III(单调栈) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

496. Next Greater Element I

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

單調(diào)棧問(wèn)題,參考: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的數(shù)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的數(shù)交換swap(nums, i, preIndex);reverse(nums, i + 1, nums.length - 1); // i右邊到結(jié)尾的所有數(shù)翻轉(zhuǎn)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;} }

總結(jié)

以上是生活随笔為你收集整理的leetcode 496, 503, 556. Next Greater Element I, II, III | 496, 503, 556. 下一个更大元素 I,II,III(单调栈)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。