{"id":39196,"date":"2024-11-26T11:44:04","date_gmt":"2024-11-26T03:44:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39196\/"},"modified":"2024-11-26T11:44:04","modified_gmt":"2024-11-26T03:44:04","slug":"java%e9%87%8c%e6%80%8e%e4%b9%88%e5%8e%bb%e9%99%a4%e6%95%b4%e6%95%b0%e6%95%b0%e7%bb%84%e9%87%8d%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39196\/","title":{"rendered":"java\u91cc\u600e\u4e48\u53bb\u9664\u6574\u6570\u6570\u7ec4\u91cd\u590d"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u53bb\u9664\u6574\u6570\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u6709\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 set\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a set\uff0c\u53bb\u9664\u91cd\u590d\u5143\u7d20\u518d\u8f6c\u6362\u56de\u6570\u7ec4\u3002\u4f7f\u7528 arrays.sort() \u548c\u53cc\u6307\u9488\uff1a\u5148\u5bf9\u6570\u7ec4\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528\u53cc\u6307\u9488\u626b\u63cf\u6570\u7ec4\uff0c\u8df3\u8fc7\u91cd\u590d\u5143\u7d20\uff0c\u5c06\u53bb\u91cd\u5143\u7d20\u586b\u5165\u65b0\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111605032474776.jpg\" class=\"aligncenter\" title=\"java\u91cc\u600e\u4e48\u53bb\u9664\u6574\u6570\u6570\u7ec4\u91cd\u590d\u63d2\u56fe\" alt=\"java\u91cc\u600e\u4e48\u53bb\u9664\u6574\u6570\u6570\u7ec4\u91cd\u590d\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u53bb\u9664\u6574\u6570\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u4f7f\u7528 Set<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 HashSet \u6216 TreeSet \u7b49 Set \u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u5929\u7136\u5177\u6709\u53bb\u9664\u91cd\u590d\u5143\u7d20\u7684\u7279\u6027\u3002<\/li>\n<li>\u5c06\u6574\u6570\u6570\u7ec4\u8f6c\u6362\u4e3a Set\uff0c\u8fd9\u6837\u4f1a\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002<\/li>\n<li>\u7136\u540e\u5c06\u5176\u8f6c\u6362\u56de\u6574\u6570\u6570\u7ec4\u5373\u53ef\u3002<\/li>\n<\/ul>\n<pre>import java.util.Arrays;\nimport java.util.HashSet;\nimport java.util.Set;\n\nclass Solution {\n    public int[] removeDuplicates(int[] nums) {\n        Set&lt;Integer&gt; set = new HashSet&lt;&gt;(Arrays.asList(nums));\n        return set.stream().mapToInt(i -&gt; i).toArray();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Arrays.sort() \u548c\u53cc\u6307\u9488<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u6837\u91cd\u590d\u5143\u7d20\u4f1a\u76f8\u90bb\u3002<\/li>\n<li>\u4f7f\u7528\u4e24\u4e2a\u6307\u9488 i \u548c j\u3002i \u6307\u5411\u5f53\u524d\u5143\u7d20\uff0cj \u6307\u5411\u53bb\u91cd\u540e\u7684\u6570\u7ec4\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7a7a\u4f4d\u7f6e\u3002<\/li>\n<li>\u5f53 i \u548c j \u6240\u6307\u5411\u7684\u5143\u7d20\u76f8\u7b49\u65f6\uff0c\u8df3\u8fc7 i \u76f4\u5230\u5b83\u6307\u5411\u4e0d\u540c\u7684\u5143\u7d20\u3002<\/li>\n<li>\u5f53 i \u548c j \u6240\u6307\u5411\u7684\u5143\u7d20\u4e0d\u540c\u65f6\uff0c\u5c06 j \u6240\u6307\u5411\u7684\u5143\u7d20\u66ff\u6362\u4e3a i \u6240\u6307\u5411\u7684\u5143\u7d20\uff0c\u5e76\u5411\u53f3\u79fb\u52a8 j\u3002<\/li>\n<\/ul>\n<pre>import java.util.Arrays;\n\nclass Solution {\n    public int[] removeDuplicates(int[] nums) {\n        if (nums.length == 0) {\n            return nums;\n        }\n        Arrays.sort(nums);\n        int i = 0, j = 1;\n        while (j &lt; nums.length) {\n            if (nums[i] == nums[j]) {\n                j++;\n            } else {\n                nums[j++] = nums[i++];\n            }\n        }\n        return Arrays.copyOfRange(nums, 0, j);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u91cc\u600e\u4e48\u53bb\u9664\u6574\u6570\u6570\u7ec4\u91cd\u590d\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\u53bb\u9664\u6574\u6570\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u6709\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 set\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a set\uff0c\u53bb\u9664\u91cd\u590d\u5143\u7d20\u518d\u8f6c\u6362\u56de\u6570\u7ec4\u3002\u4f7f\u7528 arrays.sort() \u548c\u53cc\u6307\u9488\uff1a\u5148\u5bf9\u6570\u7ec4\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528\u53cc\u6307\u9488\u626b\u63cf\u6570\u7ec4\uff0c\u8df3\u8fc7\u91cd\u590d\u5143\u7d20\uff0c\u5c06\u53bb\u91cd\u5143\u7d20\u586b\u5165\u65b0\u6570\u7ec4\u3002 Java \u4e2d\u53bb\u9664\u6574\u6570\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5 \u4f7f\u7528 Set \u4f7f\u7528 HashSet \u6216 TreeSet \u7b49 Set \u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u5929\u7136\u5177\u6709\u53bb\u9664\u91cd\u590d\u5143\u7d20\u7684\u7279\u6027\u3002 \u5c06\u6574\u6570\u6570\u7ec4\u8f6c\u6362\u4e3a Set\uff0c\u8fd9\u6837\u4f1a\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002 \u7136\u540e\u5c06\u5176\u8f6c\u6362\u56de\u6574\u6570\u6570\u7ec4\u5373\u53ef\u3002 import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Solution { public int[] removeDuplicates(int[] nums) { Set&lt;Integer&gt; set = new HashSet&lt;&gt;(Arrays.asList(nums)); return set.stream().mapToInt(i -&gt; i).toArray(); } } \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Arrays.sort() \u548c\u53cc\u6307\u9488 \u9996\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u6837\u91cd\u590d\u5143\u7d20\u4f1a\u76f8\u90bb\u3002 \u4f7f\u7528\u4e24\u4e2a\u6307\u9488 i \u548c j\u3002i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-39196","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=39196"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39196\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}