{"id":36856,"date":"2024-11-26T11:20:41","date_gmt":"2024-11-26T03:20:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36856\/"},"modified":"2024-11-26T11:20:41","modified_gmt":"2024-11-26T03:20:41","slug":"java%e6%80%8e%e4%b9%88%e6%95%b0%e7%bb%84%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36856\/","title":{"rendered":"java\u600e\u4e48\u6570\u7ec4\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\uff1a\u4f7f\u7528 set\uff1a\u521b\u5efa set \u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u8f6c\u6362\u4e3a\u65b0\u6570\u7ec4\u3002\u4f7f\u7528 sort \u548c\u4e8c\u5206\u67e5\u627e\uff1a\u6392\u5e8f\u6570\u7ec4\uff0c\u67e5\u627e\u6bcf\u4e2a\u5143\u7d20\u51fa\u73b0\u4f4d\u7f6e\uff0c\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002\u4f7f\u7528 hashmap\uff1a\u4ee5\u5143\u7d20\u4e3a\u952e\uff0c\u51fa\u73b0\u6b21\u6570\u4e3a\u503c\uff0c\u53bb\u9664\u51fa\u73b0\u6b21\u6570\u5927\u4e8e 1 \u7684\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111220302856619.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u6570\u7ec4\u53bb\u91cd\u63d2\u56fe\" alt=\"java\u600e\u4e48\u6570\u7ec4\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u53bb\u91cd<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6570\u7ec4\u53bb\u91cd\u662f\u6307\u4ece\u6570\u7ec4\u4e2d\u79fb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u4ec5\u4fdd\u7559\u552f\u4e00\u5143\u7d20\u3002\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u53bb\u91cd\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Set<\/strong><\/p>\n<p>Set \u662f\u4e00\u79cd\u96c6\u5408\uff0c\u5b83\u4e0d\u4f1a\u4fdd\u7559\u91cd\u590d\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u5143\u7d20\u8f6c\u6362\u4e3a Set\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n\/\/ \u521b\u5efa\u4e00\u4e2a Set\nSet&lt;Integer&gt; set = new HashSet&lt;&gt;(Arrays.asList(arr));\n\n\/\/ \u5c06 Set \u8f6c\u6362\u4e3a\u6570\u7ec4\nint[] result = set.stream().mapToInt(Integer::intValue).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Sort \u548c\u4e8c\u5206\u67e5\u627e<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u67e5\u627e\u6bcf\u4e2a\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u51fa\u73b0\u4f4d\u7f6e\u3002\u91cd\u590d\u5143\u7d20\u7684\u4f4d\u7f6e\u5dee\u5c06\u5927\u4e8e 1\uff0c\u56e0\u6b64\u53ef\u4ee5\u6839\u636e\u6b64\u5dee\u503c\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<pre>Arrays.sort(arr);\n\nint[] result = new int[arr.length];\nint index = 0;\n\nfor (int i = 0; i &lt; arr.length; i++) {\n    int first = binarySearch(arr, i, arr.length - 1, arr[i]);\n    int last = binarySearch(arr, i, arr.length - 1, arr[i], true);\n    \n    if (last - first &lt;= 1) {\n        result[index++] = arr[i];\n    }\n    \n    i = last;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 HashMap<\/strong><\/p>\n<p>HashMap \u662f\u4e00\u79cd\u96c6\u5408\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u4f5c\u4e3a\u952e\uff0c\u5176\u51fa\u73b0\u6b21\u6570\u4f5c\u4e3a\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386 HashMap\uff0c\u53ea\u4fdd\u7559\u51fa\u73b0\u6b21\u6570\u4e3a 1 \u7684\u5143\u7d20\u3002<\/p>\n<pre>HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();\n\nfor (int num : arr) {\n    map.put(num, map.getOrDefault(num, 0) + 1);\n}\n\nint[] result = new int[map.size()];\nint index = 0;\n\nfor (Integer key : map.keySet()) {\n    if (map.get(key) == 1) {\n        result[index++] = key;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u6570\u7ec4\u53bb\u91cd\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>java \u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\uff1a\u4f7f\u7528 set\uff1a\u521b\u5efa set \u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u8f6c\u6362\u4e3a\u65b0\u6570\u7ec4\u3002\u4f7f\u7528 sort \u548c\u4e8c\u5206\u67e5\u627e\uff1a\u6392\u5e8f\u6570\u7ec4\uff0c\u67e5\u627e\u6bcf\u4e2a\u5143\u7d20\u51fa\u73b0\u4f4d\u7f6e\uff0c\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002\u4f7f\u7528 hashmap\uff1a\u4ee5\u5143\u7d20\u4e3a\u952e\uff0c\u51fa\u73b0\u6b21\u6570\u4e3a\u503c\uff0c\u53bb\u9664\u51fa\u73b0\u6b21\u6570\u5927\u4e8e 1 \u7684\u5143\u7d20\u3002 Java \u6570\u7ec4\u53bb\u91cd \u5728 Java \u4e2d\uff0c\u6570\u7ec4\u53bb\u91cd\u662f\u6307\u4ece\u6570\u7ec4\u4e2d\u79fb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u4ec5\u4fdd\u7559\u552f\u4e00\u5143\u7d20\u3002\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u53bb\u91cd\uff1a 1. \u4f7f\u7528 Set Set \u662f\u4e00\u79cd\u96c6\u5408\uff0c\u5b83\u4e0d\u4f1a\u4fdd\u7559\u91cd\u590d\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u5143\u7d20\u8f6c\u6362\u4e3a Set\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3, 4, 5, 1, 2, 3}; \/\/ \u521b\u5efa\u4e00\u4e2a Set Set&lt;Integer&gt; set = new HashSet&lt;&gt;(Arrays.asList(arr)); \/\/ \u5c06 Set \u8f6c\u6362\u4e3a\u6570\u7ec4 int[] result = set.stream().mapToInt(Integer::intValue).toArray(); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 Sort \u548c\u4e8c\u5206\u67e5\u627e \u6211\u4eec\u53ef\u4ee5\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u67e5\u627e\u6bcf\u4e2a\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u51fa\u73b0\u4f4d\u7f6e\u3002\u91cd\u590d\u5143\u7d20\u7684\u4f4d\u7f6e\u5dee\u5c06\u5927\u4e8e [&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-36856","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36856","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=36856"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36856\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}