{"id":64639,"date":"2025-05-03T14:38:06","date_gmt":"2025-05-03T06:38:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64639\/"},"modified":"2025-05-03T14:38:06","modified_gmt":"2025-05-03T06:38:06","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e6%8a%8a%e9%87%8d%e5%a4%8d%e7%9a%84%e6%89%be%e5%87%ba%e6%9d%a5-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64639\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u628a\u91cd\u590d\u7684\u627e\u51fa\u6765"},"content":{"rendered":"<blockquote><p>\n  \u67e5\u627e java \u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u76f4\u63a5\u6bd4\u8f83\uff1a\u904d\u5386\u6570\u7ec4\u5e76\u4f9d\u6b21\u6bd4\u8f83\u6bcf\u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 o(n^2)\u3002\u54c8\u5e0c\u8868\uff1a\u4f7f\u7528\u54c8\u5e0c\u8868\u5b58\u50a8\u5143\u7d20\u548c\u8ba1\u6570\uff0c\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6 o(n)\u3002\u96c6\u5408\uff1a\u4f7f\u7528\u96c6\u5408\u5b58\u50a8\u5143\u7d20\uff0c\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6 o(n)\u3002arrays.sort()\uff1a\u5bf9\u6570\u7ec4\u6392\u5e8f\u540e\u67e5\u627e\u76f8\u90bb\u91cd\u590d\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 o(n log n)\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110404423466274.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u628a\u91cd\u590d\u7684\u627e\u51fa\u6765\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u628a\u91cd\u590d\u7684\u627e\u51fa\u6765\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u67e5\u627e Java \u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u6bd4\u8f83<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u76f4\u63a5\u6bd4\u8f83\u5143\u7d20\u6765\u67e5\u627e\u91cd\u590d\u5143\u7d20\uff1a<\/p>\n<pre>for (int i = 0; i &lt; array.length; i++) {\n    for (int j = i + 1; j &lt; array.length; j++) {\n        if (array[i] == array[j]) {\n            \/\/ \u627e\u5230\u91cd\u590d\u5143\u7d20\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong>O(n^2)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u54c8\u5e0c\u8868<\/strong><\/p>\n<p>\u4f7f\u7528\u54c8\u5e0c\u8868\u53ef\u4ee5\u4f18\u5316\u67e5\u627e\u91cd\u590d\u5143\u7d20\uff1a<\/p>\n<pre>Map&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();\nfor (int num : array) {\n    if (map.containsKey(num)) {\n        \/\/ \u627e\u5230\u91cd\u590d\u5143\u7d20\n    } else {\n        map.put(num, 1);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong>O(n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>\u96c6\u5408<\/strong><\/p>\n<p>\u5982\u679c\u4e0d\u9700\u8981\u8bb0\u5f55\u91cd\u590d\u5143\u7d20\u7684\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6765\u67e5\u627e\u91cd\u590d\u5143\u7d20\uff1a<\/p>\n<pre>Set&lt;Integer&gt; set = new HashSet&lt;&gt;();\nfor (int num : array) {\n    if (!set.add(num)) {\n        \/\/ \u627e\u5230\u91cd\u590d\u5143\u7d20\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong>O(n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>Arrays.sort()<\/strong><\/p>\n<p>\u5982\u679c\u6570\u7ec4\u5143\u7d20\u4e3a\u53ef\u6bd4\u8f83\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u6392\u5e8f\uff0c\u7136\u540e\u904d\u5386\u6570\u7ec4\u67e5\u627e\u76f8\u90bb\u5143\u7d20\u662f\u5426\u91cd\u590d\uff1a<\/p>\n<pre>Arrays.sort(array);\nfor (int i = 1; i &lt; array.length; i++) {\n    if (array[i] == array[i - 1]) {\n        \/\/ \u627e\u5230\u91cd\u590d\u5143\u7d20\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong>O(n log n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u628a\u91cd\u590d\u7684\u627e\u51fa\u6765\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u627e java \u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u76f4\u63a5\u6bd4\u8f83\uff1a\u904d\u5386\u6570\u7ec4\u5e76\u4f9d\u6b21\u6bd4\u8f83\u6bcf\u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 o(n^2)\u3002\u54c8\u5e0c\u8868\uff1a\u4f7f\u7528\u54c8\u5e0c\u8868\u5b58\u50a8\u5143\u7d20\u548c\u8ba1\u6570\uff0c\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6 o(n)\u3002\u96c6\u5408\uff1a\u4f7f\u7528\u96c6\u5408\u5b58\u50a8\u5143\u7d20\uff0c\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6 o(n)\u3002arrays.sort()\uff1a\u5bf9\u6570\u7ec4\u6392\u5e8f\u540e\u67e5\u627e\u76f8\u90bb\u91cd\u590d\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 o(n log n)\u3002 \u5982\u4f55\u67e5\u627e Java \u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20 \u76f4\u63a5\u6bd4\u8f83 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u76f4\u63a5\u6bd4\u8f83\u5143\u7d20\u6765\u67e5\u627e\u91cd\u590d\u5143\u7d20\uff1a for (int i = 0; i &lt; array.length; i++) { for (int j = i + 1; j &lt; array.length; j++) { if (array[i] == array[j]) { \/\/ \u627e\u5230\u91cd\u590d\u5143\u7d20 } } } \u767b\u5f55\u540e\u590d\u5236 \u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n^2)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u54c8\u5e0c\u8868 \u4f7f\u7528\u54c8\u5e0c\u8868\u53ef\u4ee5\u4f18\u5316\u67e5\u627e\u91cd\u590d\u5143\u7d20\uff1a Map&lt;Integer, Integer&gt; [&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-64639","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64639","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=64639"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64639\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}