{"id":39483,"date":"2024-11-26T13:17:52","date_gmt":"2024-11-26T05:17:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39483\/"},"modified":"2024-11-26T13:17:52","modified_gmt":"2024-11-26T05:17:52","slug":"%e6%80%8e%e4%b9%88%e7%94%a8java%e5%81%9a%e5%9c%a8%e6%95%b0%e7%bb%84%e4%b8%ad%e6%9f%a5%e6%89%be","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39483\/","title":{"rendered":"\u600e\u4e48\u7528Java\u505a\u5728\u6570\u7ec4\u4e2d\u67e5\u627e"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u67e5\u627e\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u987a\u5e8f\u67e5\u627e\uff08\u4ece\u5934\u4f9d\u6b21\u6bd4\u8f83\uff09\u548c\u4e8c\u5206\u67e5\u627e\uff08\u9002\u7528\u4e8e\u5df2\u6392\u5e8f\u6570\u7ec4\uff0c\u901a\u8fc7\u5206\u6cbb\u6cd5\u67e5\u627e\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111611334828389.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u7528Java\u505a\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u63d2\u56fe\" alt=\"\u600e\u4e48\u7528Java\u505a\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20<\/strong><\/p>\n<p><strong>\u6982\u89c8\uff1a<\/strong><\/p>\n<p>\u5728 Java \u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20\u662f\u4e00\u4e2a\u5e38\u89c1\u4efb\u52a1\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4e4b\u4e00\u67e5\u627e\u6570\u7ec4\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\uff1a<\/p>\n<ul>\n<li><strong>\u987a\u5e8f\u67e5\u627e<\/strong><\/li>\n<li><strong>\u4e8c\u5206\u67e5\u627e<\/strong><\/li>\n<\/ul>\n<p><strong>\u987a\u5e8f\u67e5\u627e\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u987a\u5e8f\u67e5\u627e\u4ece\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u9010\u4e2a\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u5230\u8fbe\u6570\u7ec4\u672b\u5c3e\u3002\u8fd9\u662f\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u6548\u7387\u4f4e\u4e0b\u3002<\/p>\n<p><strong>\u4e8c\u5206\u67e5\u627e\uff1a<\/strong><\/p>\n<p>\u4e8c\u5206\u67e5\u627e\u9002\u7528\u4e8e\u5df2\u6392\u5e8f\u7684\u6570\u7ec4\u3002\u5b83\u901a\u8fc7\u5c06\u6570\u7ec4\u5206\u6210\u4e24\u534a\u6765\u67e5\u627e\u76ee\u6807\u5143\u7d20\u3002\u5982\u679c\u76ee\u6807\u5143\u7d20\u5728\u540e\u534a\u90e8\u5206\uff0c\u5219\u4e22\u5f03\u524d\u534a\u90e8\u5206\uff1b\u5982\u679c\u76ee\u6807\u5143\u7d20\u5728\u524d\u534a\u5206\uff0c\u5219\u4e22\u5f03\u540e\u534a\u90e8\u5206\u3002\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u987a\u5e8f\u67e5\u627e\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(log n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<p><strong>\u987a\u5e8f\u67e5\u627e\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>public static int sequentialSearch(int[] arr, int target) {\n  for (int i = 0; i &lt; arr.length; i++) {\n    if (arr[i] == target) {\n      return i;\n    }\n  }\n  return -1;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8c\u5206\u67e5\u627e\uff1a<\/strong><\/p>\n<pre>public static int binarySearch(int[] arr, int target) {\n  int left = 0;\n  int right = arr.length - 1;\n\n  while (left &lt;= right) {\n    int mid = (left + right) \/ 2;\n\n    if (arr[mid] == target) {\n      return mid;\n    } else if (arr[mid] &lt; target) {\n      left = mid + 1;\n    } else {\n      right = mid - 1;\n    }\n  }\n  return -1;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u6570\u7ec4\u5f88\u5927\uff0c\u4e8c\u5206\u67e5\u627e\u5c06\u6bd4\u987a\u5e8f\u67e5\u627e\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u66f4\u5c11\u7684\u6bd4\u8f83\u6b21\u6570\u3002<\/li>\n<li>\u5982\u679c\u6570\u7ec4\u5f88\u5c0f\u6216\u672a\u6392\u5e8f\uff0c\u987a\u5e8f\u67e5\u627e\u5c06\u66f4\u7b80\u5355\u4e14\u66f4\u6709\u6548\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7528Java\u505a\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\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 \u4e2d\u67e5\u627e\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u987a\u5e8f\u67e5\u627e\uff08\u4ece\u5934\u4f9d\u6b21\u6bd4\u8f83\uff09\u548c\u4e8c\u5206\u67e5\u627e\uff08\u9002\u7528\u4e8e\u5df2\u6392\u5e8f\u6570\u7ec4\uff0c\u901a\u8fc7\u5206\u6cbb\u6cd5\u67e5\u627e\uff09\u3002 Java \u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20 \u6982\u89c8\uff1a \u5728 Java \u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20\u662f\u4e00\u4e2a\u5e38\u89c1\u4efb\u52a1\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4e4b\u4e00\u67e5\u627e\u6570\u7ec4\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\uff1a \u987a\u5e8f\u67e5\u627e \u4e8c\u5206\u67e5\u627e \u987a\u5e8f\u67e5\u627e\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u987a\u5e8f\u67e5\u627e\u4ece\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u9010\u4e2a\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u5230\u8fbe\u6570\u7ec4\u672b\u5c3e\u3002\u8fd9\u662f\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u6548\u7387\u4f4e\u4e0b\u3002 \u4e8c\u5206\u67e5\u627e\uff1a \u4e8c\u5206\u67e5\u627e\u9002\u7528\u4e8e\u5df2\u6392\u5e8f\u7684\u6570\u7ec4\u3002\u5b83\u901a\u8fc7\u5c06\u6570\u7ec4\u5206\u6210\u4e24\u534a\u6765\u67e5\u627e\u76ee\u6807\u5143\u7d20\u3002\u5982\u679c\u76ee\u6807\u5143\u7d20\u5728\u540e\u534a\u90e8\u5206\uff0c\u5219\u4e22\u5f03\u524d\u534a\u90e8\u5206\uff1b\u5982\u679c\u76ee\u6807\u5143\u7d20\u5728\u524d\u534a\u5206\uff0c\u5219\u4e22\u5f03\u540e\u534a\u90e8\u5206\u3002\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u987a\u5e8f\u67e5\u627e\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(log n)\uff0c\u5176\u4e2d n \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a \u987a\u5e8f\u67e5\u627e\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b public static int sequentialSearch(int[] arr, int target) { for (int i = 0; i &lt; arr.length; i++) { if (arr[i] == target) { return i; } } return -1; } \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u5206\u67e5\u627e\uff1a public [&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-39483","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39483","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=39483"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39483\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}