{"id":37437,"date":"2024-11-26T14:31:29","date_gmt":"2024-11-26T06:31:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37437\/"},"modified":"2024-11-26T14:31:29","modified_gmt":"2024-11-26T06:31:29","slug":"java%e6%95%b0%e7%bb%84%e4%b8%ad%e6%80%8e%e4%b9%88%e5%88%a4%e6%96%ad%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37437\/","title":{"rendered":"java\u6570\u7ec4\u4e2d\u600e\u4e48\u5224\u65ad\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u6570\u7ec4\u3001\u4f7f\u7528 arrays.binarysearch()\uff08\u7528\u4e8e\u6709\u5e8f\u6570\u7ec4\uff09\u6216\u5c06\u5143\u7d20\u5b58\u50a8\u5230 hashset \u4e2d\u6765\u5224\u65ad java \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5176\u4e2d\uff0chashset \u5177\u6709 o(1) \u7684\u67e5\u627e\u590d\u6742\u5ea6\uff0c\u975e\u5e38\u9ad8\u6548\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111305423245553.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u4e2d\u600e\u4e48\u5224\u65ad\u5143\u7d20\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u4e2d\u600e\u4e48\u5224\u65ad\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad Java \u6570\u7ec4\u4e2d\u7684\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u6765\u5224\u65ad\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<p><strong>1. \u904d\u5386\u6570\u7ec4<\/strong><\/p>\n<p>\u4f7f\u7528 for \u5faa\u73af\u6216\u589e\u5f3a for \u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u6ee1\u8db3\u6761\u4ef6\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nboolean containsThree = false;\nfor (int num : arr) {\n    if (num == 3) {\n        containsThree = true;\n        break;\n    }\n}\n\nif (containsThree) {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20 3\");\n} else {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u5143\u7d20 3\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.binarySearch()<\/strong><\/p>\n<p>\u5982\u679c\u6570\u7ec4\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5\u6765\u67e5\u627e\u7279\u5b9a\u7684\u5143\u7d20\u3002\u6b64\u65b9\u6cd5\u8fd4\u56de\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u8fd4\u56de -1\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>Arrays.sort(arr); \/\/ \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\n\nint index = Arrays.binarySearch(arr, 3);\nif (index &gt;= 0) {\n    System.out.println(\"\u5143\u7d20 3 \u5728\u6570\u7ec4\u4e2d\u4f4d\u4e8e\u7d22\u5f15\uff1a\" + index);\n} else {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u5143\u7d20 3\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 HashSet<\/strong><\/p>\n<p>\u5c06\u6570\u7ec4\u5143\u7d20\u5b58\u50a8\u5230 HashSet \u4e2d\uff0c\u7136\u540e\u68c0\u67e5\u8be5\u96c6\u5408\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u7684\u5143\u7d20\u3002HashSet \u5177\u6709 O(1) \u7684\u67e5\u627e\u590d\u6742\u5ea6\uff0c\u56e0\u6b64\u6b64\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>Set&lt;Integer&gt; set = new HashSet&lt;&gt;();\nfor (int num : arr) {\n    set.add(num);\n}\n\nboolean containsThree = set.contains(3);\nif (containsThree) {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20 3\");\n} else {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u5143\u7d20 3\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u4e2d\u600e\u4e48\u5224\u65ad\u5143\u7d20\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>\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u6570\u7ec4\u3001\u4f7f\u7528 arrays.binarysearch()\uff08\u7528\u4e8e\u6709\u5e8f\u6570\u7ec4\uff09\u6216\u5c06\u5143\u7d20\u5b58\u50a8\u5230 hashset \u4e2d\u6765\u5224\u65ad java \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5176\u4e2d\uff0chashset \u5177\u6709 o(1) \u7684\u67e5\u627e\u590d\u6742\u5ea6\uff0c\u975e\u5e38\u9ad8\u6548\u3002 \u5982\u4f55\u5224\u65ad Java \u6570\u7ec4\u4e2d\u7684\u5143\u7d20 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u6765\u5224\u65ad\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a 1. \u904d\u5386\u6570\u7ec4 \u4f7f\u7528 for \u5faa\u73af\u6216\u589e\u5f3a for \u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u6ee1\u8db3\u6761\u4ef6\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u793a\u4f8b\uff1a int[] arr = {1, 2, 3, 4, 5}; boolean containsThree = false; for (int num : arr) { if (num == 3) { containsThree = true; break; } } if (containsThree) [&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-37437","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37437","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=37437"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37437\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}