{"id":1013,"date":"2025-12-23T06:05:35","date_gmt":"2025-12-22T22:05:35","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1013"},"modified":"2025-12-23T06:05:35","modified_gmt":"2025-12-22T22:05:35","slug":"leetcode-496-%e4%b8%8b%e4%b8%80%e4%b8%aa%e6%9b%b4%e5%a4%a7%e5%85%83%e7%b4%a0-i","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1013","title":{"rendered":"LeetCode 496 \u2013 \u4e0b\u4e00\u4e2a\u66f4\u5927\u5143\u7d20 I"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>nums1 \u4e2d\u6570\u5b57 x \u7684 \u4e0b\u4e00\u4e2a\u66f4\u5927\u5143\u7d20 \u662f\u6307 x \u5728 nums2 \u4e2d\u5bf9\u5e94\u4f4d\u7f6e \u53f3\u4fa7 \u7684 \u7b2c\u4e00\u4e2a \u6bd4 x \u5927\u7684\u5143\u7d20\u3002<br \/>\n\u7ed9\u4f60\u4e24\u4e2a \u6ca1\u6709\u91cd\u590d\u5143\u7d20 \u7684\u6570\u7ec4 nums1 \u548c nums2 \uff0c\u4e0b\u6807\u4ece 0 \u5f00\u59cb\u8ba1\u6570\uff0c\u5176\u4e2dnums1 \u662f nums2 \u7684\u5b50\u96c6\u3002<br \/>\n\u5bf9\u4e8e\u6bcf\u4e2a 0 &lt;= i &lt; nums1.length \uff0c\u627e\u51fa\u6ee1\u8db3 nums1[i] <span class=\"text-highlighted-inline\" style=\"background-color: #fffd38;\"> nums2[j] \u7684\u4e0b\u6807 j \uff0c\u5e76\u4e14\u5728 nums2 \u786e\u5b9a nums2[j] \u7684 \u4e0b\u4e00\u4e2a\u66f4\u5927\u5143\u7d20 \u3002\u5982\u679c\u4e0d\u5b58\u5728\u4e0b\u4e00\u4e2a\u66f4\u5927\u5143\u7d20\uff0c\u90a3\u4e48\u672c\u6b21\u67e5\u8be2\u7684\u7b54\u6848\u662f -1 \u3002<br \/>\n\u8fd4\u56de\u4e00\u4e2a\u957f\u5ea6\u4e3a nums1.length \u7684\u6570\u7ec4 ans \u4f5c\u4e3a\u7b54\u6848\uff0c\u6ee1\u8db3 ans[i] \u662f\u5982\u4e0a\u6240\u8ff0\u7684 \u4e0b\u4e00\u4e2a\u66f4\u5927\u5143\u7d20 \u3002<\/span><\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1anums1 = [4,1,2], nums2 = [1,3,4,2].<br \/>\n\u8f93\u51fa\uff1a[-1,3,-1]<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u5355\u8c03\u6808\u3002<br \/>\n\u5c06num2\u4e2d\u5143\u7d20\u4f9d\u6b21\u5165\u6808\uff0c\u4fdd\u6301\u4ece\u5e95\u5230\u9876\u5355\u8c03\u9012\u51cf\uff0c\u5728\u5143\u7d20\u51fa\u6808\u8fc7\u7a0b\u4e2d\u8bb0\u5f55\u53f3\u4fa7\u7b2c\u4e00\u4e2a\u66f4\u5927\u503c\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public int[] nextGreaterElement(int[] nums1, int[] nums2) {\n    Deque&lt;Integer&gt; stack = new LinkedList&lt;&gt;();\n    Map&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();\n    for (int x : nums2) {\n        while (!stack.isEmpty() &amp;&amp; stack.peek() &lt; x) {\n            map.put(stack.pop(), x);\n        }\n        stack.push(x);\n    }\n    int[] ans = new int[nums1.length];\n    for (int i = 0; i &lt; nums1.length; i++) {\n        Integer value = map.get(nums1[i]);\n        ans[i] = value != null ? value : -1;\n    }\n    return ans;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun nextGreaterElement(nums1: IntArray, nums2: IntArray): IntArray {\n    val map = HashMap&lt;Int, Int&gt;()\n    val stack = LinkedList&lt;Int&gt;()\n    for (x in nums2) {\n        while (stack.isNotEmpty() &amp;&amp; stack.peek() &lt; x) {\n            map[stack.pop()] = x\n        }\n        stack.push(x)\n    }\n    return IntArray(nums1.size) { i -&gt;\n        map[nums1[i]] ?: -1\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 nums1 \u4e2d\u6570\u5b57 x \u7684 \u4e0b\u4e00\u4e2a\u66f4\u5927\u5143\u7d20 \u662f\u6307 x \u5728 nums2 \u4e2d\u5bf9\u5e94\u4f4d\u7f6e \u53f3\u4fa7 \u7684 \u7b2c\u4e00\u4e2a  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,76,74],"class_list":["post-1013","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-76","tag-74"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1013"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1013\/revisions"}],"predecessor-version":[{"id":1014,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1013\/revisions\/1014"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}